• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
KYS_01
博客园    首页    新随笔    联系   管理    订阅  订阅
PrepaerStatement实现不同表的通用查询
public <T> List<T> QueryFinal(Class<T> clazz, String sql, Object... args){
        Connection connection = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
              // 1.获取数据库连接
            connection = Util.getConnection();
             // 2.预编译sql语句,得到PreparedStatement对象 
            ps = connection.prepareStatement(sql);
            // 3.填充占位符
            for (int i = 0; i < args.length; i++) {
                ps.setObject(i + 1, args[i]);
            }
             // 4.执行executeQuery(),得到结果集:ResultSet 
            rs = ps.executeQuery();
             // 5.得到结果集的元数据:ResultSetMetaData
            ResultSetMetaData rsmd = rs.getMetaData();
             // 6.1通过ResultSetMetaData得到columnCount,columnLabel;通过ResultSet得到列值 
            int columnCount = rsmd.getColumnCount();
            ArrayList<T> arrayList = new ArrayList<T>();
            while (rs.next()) {
                T t = clazz.newInstance();
                // 遍历每一个列
                for (int i = 0; i < columnCount; i++) {
                     // 获取列值 
                    Object columnValue = rs.getObject(i + 1);
                    // 获取列的别名:列的别名,使用类的属性名充当 
                    String columnLabel = rsmd.getColumnLabel(i+1);
                      // 6.2使用反射,给对象的相应属性赋值 
                    Field field = clazz.getDeclaredField(columnLabel);
                    field.setAccessible(true);
                    field.set(t, columnValue);
                }
                arrayList.add(t);
                
            }
            return arrayList;
        } catch (InstantiationException e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        } catch (SecurityException e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        } catch (Exception e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        }
        finally {
            Util.closeResource(connection, ps, rs);
      //关闭资源
        }
        
        return null;
    }
    @Test
    public void QueryFinalTest() {
        String sql = "select id,name from customers where id<?";
        List<Customer> list = QueryFinal(Customer.class,sql,10);
        list.forEach(System.out::println);
    }

 

posted on 2020-03-16 21:12  KYS_01  阅读(245)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3