dtcms插件 Kafka WorldCloud wpf pointers foreach vue传值 admin框架 微信pc版无法获取二维码 oracle查看数据库 c语言求和 eclipse显示左边目录 java 注解 python变量类型 python传参 java数据库连接 java正则表达 java重写和重载 javaforeach java语言代码大全 java的方法 java集合类型 linuxtail命令 圣骑士装备 战地2单机地图 摩斯电码翻译器 volist 隐藏虚拟键 kms神龙版 电脑密码查看器 迅雷去广告版 凯恩与林奇2下载 英雄联盟崩溃 音乐狂app 字符串分割成数组 英雄联盟设置 疯狂的站长 迅雷单机游戏下载 苹果手机耳机没声音 抽出滤镜下载
当前位置: 首页 > 学习教程  > 编程语言

JDBC工具类模拟数据的查询并封装

2021/1/13 19:45:18 文章标签: 测试文章如有侵权请发送至邮箱809451989@qq.com投诉后文章立即删除

JDBC工具类//实现jdbc的模拟查询和封装的实现public <T> List<T> selectQuery(String sql,Class<T> c,Object...args){try {Class.forName("com.mysql.cj.jdbc.Driver");con DriverManager.getConnection("jdbc:mysql:///db1?useSSLfalse&a…

JDBC工具类

 //实现jdbc的模拟查询和封装的实现
    public <T> List<T> selectQuery(String sql,Class<T> c,Object...args){
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql:///db1?useSSL=false&serverTimezone=UTC&characterEncoding=utf8", "root", "123456");
            PreparedStatement ps=con.prepareStatement(sql);
            for (int i = 0; i < args.length; i++) {
                ps.setObject(i+1,args[i]);
            }
            ResultSet rs = ps.executeQuery();
            ResultSetMetaData md = rs.getMetaData();
            int columnCount = md.getColumnCount();
            ArrayList<T> ts = new ArrayList<>();
            while (rs.next()){
                T t=c.newInstance();
                for (int i = 1; i <= columnCount; i++) {
                    Object value = rs.getObject(i);
                    String columnName = md.getColumnName(i);
                    Field f = c.getDeclaredField(columnName);
                    f.setAccessible(true);
                    f.set(t,value);
                }
                ts.add(t);
            }
            return ts;
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
        return null;
    }
//实现jdbc的模拟增删改和封装的实现
    public int update(String sql,Object...args){
        try {
            Connection con = DriverManager.getConnection("jdbc:mysql:///db1?useSSL=false&serverTimezone=UTC&characterEncoding=utf8", "root", "123456");
            PreparedStatement ps = con.prepareStatement(sql);
            for (int i = 0; i < args.length; i++) {
                ps.setObject(i+1,args[i]);
            }
            int i = ps.executeUpdate();
            return i;
        }  catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }
public <T>T selectQueryForObject(String sql,Class<T> c,Object...args){
        try {
            PreparedStatement ps=con.prepareStatement(sql);
            for (int i = 0; i < args.length; i++) {
                ps.setObject(i+1,args[i]);
            }
            ResultSet rs = ps.executeQuery();
            ResultSetMetaData md = rs.getMetaData();
            int columnCount = md.getColumnCount();
            T t=null;
            if (rs.next()){

                if ((sql.toLowerCase().contains("count")||sql.toLowerCase().contains("sum")||
                     sql.toLowerCase().contains("min")|| sql.toLowerCase().contains("max")||
                     sql.toLowerCase().contains("avg"))&& columnCount==1){
                    return (T) rs.getObject(1);
                }
                //查单行
                t=c.newInstance();
                for (int i = 1; i <= columnCount; i++) {
                    Object value = rs.getObject(i);
                    String columnName = md.getColumnName(i);
                    Field f = c.getDeclaredField(columnName);
                    f.setAccessible(true);
                    f.set(t,value);
                }
            }
            return t;
        }  catch (SQLException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
        return null;
    }
public Map<String ,Object> Queryformap(String sql,Class c,Object...args) throws Exception {
        PreparedStatement ps=con.prepareStatement(sql);
        for (int i = 0; i < args.length; i++) {
            ps.setObject(i+1,args[i]);
        }
        ResultSet rs = ps.executeQuery();
        ResultSetMetaData md = rs.getMetaData();
        int columnCount = md.getColumnCount();
        HashMap<String, Object> msp = new HashMap<>();
        if (rs.next()){
            for (int i = 1; i <= columnCount; i++) {
                Object object = rs.getObject(i);
                String columnName = md.getColumnName(i);
                msp.put(columnName,object);
            }
            return msp;
        }
        return null;
    }
public List<Map<String ,Object>> QueryForlist(String sql,Object...args){
        try {

            //3.获取执行sql的对象
            PreparedStatement pstm = con.prepareStatement(sql);
            //4.给占位符赋值
            for (int i = 0; i < args.length; i++) {
                pstm.setObject(i+1,args[i]);
            }
            //5.执行sql
            ResultSet rs = pstm.executeQuery();
            //6.处理结果集--拆开rs
            //元数据--可以获取结果集中的列数,列名,列类型
            ResultSetMetaData md = rs.getMetaData();
            //获取总列数
            int columnCount = md.getColumnCount();
            ArrayList<Map<String,Object>> list = new ArrayList<>();
            while (rs.next()){//外层循环遍历每一行
                Map<String,Object> map = new HashMap();
                for (int i = 1; i <= columnCount; i++) {//外层循环遍历每一行的每一个单元格
                    Object value = rs.getObject(i);
                    String columnName = md.getColumnName(i).toLowerCase();//获取每一个列的列名--和T中的属性名一致的
                    map.put(columnName,value);

                }
                list.add(map);
            }
            return list;

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

本文链接: http://www.dtmao.cc/news_show_600183.shtml

附件下载

相关教程

    暂无相关的数据...

共有条评论 网友评论

验证码: 看不清楚?