JDBCUtils

/**
 * JDBC 工具类
 * @author pengYi
 *
 */
public class JDBCUtils {
     
     public static Connection connection = null;
     public static PreparedStatement preparedStatement = null;
     public static ResultSet resultSet = null;
         
     /**
      * 连接数据库
      * @return
      */
     public static Connection getConnection(){
         String url = "jdbc:mysql://localhost:3306/chartroom";
         String user = "root";
         String password = "root";
         try {
             Class.forName("com.mysql.jdbc.Driver");
             connection = DriverManager.getConnection(url, user, password);
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
         return connection;
     }
     /**
      * 关闭资源
      */
     public static void close(){
         try{
             if(connection != null){
                connection.close();
                connection = null;
             }
             if(preparedStatement != null){
                 preparedStatement.close();
                 preparedStatement = null;
             }
             if(resultSet!= null){
                 resultSet.close();
                 resultSet = null;
             }
         }catch(SQLException e){
             e.printStackTrace();
         }
         
     }
     /**
      * 将结果集转换成实体对象集合
      * @param res 结果集
      * @param c 实体对象映射类
      * @return
     * @throws SQLException
     * @throws IllegalAccessException
     * @throws InstantiationException
      */
     public static List Populate(ResultSet rs,Class cc) throws SQLException, InstantiationException, IllegalAccessException{
         
         //结果集 中列的名称和类型的信息
         ResultSetMetaData rsm = rs.getMetaData();
         int colNumber = rsm.getColumnCount();
         List list = new ArrayList();
         Field[] fields = cc.getDeclaredFields();
         
         //遍历每条记录
         while(rs.next()){
             //实例化对象
             Object obj = cc.newInstance();
             //取出每一个字段进行赋值
             for(int i=1;i<=colNumber;i++){
                 Object value = rs.getObject(i);
                 //匹配实体类中对应的属性
                 for(int j = 0;j<fields.length;j++){
                     Field f = fields[j];
                     if(f.getName().equals(rsm.getColumnName(i))){
                         boolean flag = f.isAccessible();
                         f.setAccessible(true);
                         f.set(obj, value);
                         f.setAccessible(flag);
                         break;
                     }
                 }
                  
             }
             list.add(obj);
         }
         return list;
     }
}

测试

public class Test{
     
    public static void main(String[] args) throws InstantiationException, IllegalAccessException {
        Connection conn = JDBCUtils.getConnection();      //JDBCUtils 自己定义的一个类
        PreparedStatement pre = null;
        ResultSet res = null;
        String sql = "select * from user where username=?";
        try {
            pre = conn.prepareStatement(sql);
            pre.setString(1,"wqq");
            res = pre.executeQuery();
            //调用将结果集转换成实体对象方法
            List list = JDBCUtils.Populate(res, User.class);
            //循环遍历结果
            for(int i=0;i<list.size();i++){
                User user = (User) list.get(i);
                System.out.println("[username = "+ user.getUsername()+",passwd = "+ user.getPassword()+"]");
            }
         
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值