Chinaunix首页 | 论坛 | 博客
  • 博客访问: 736525
  • 博文数量: 178
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1507
  • 用 户 组: 普通用户
  • 注册时间: 2014-04-27 23:20
文章分类

全部博文(178)

文章存档

2015年(58)

2014年(121)

我的朋友

分类: Java

2014-11-23 11:45:52

将jdbc结果集转换成对象列表 
估计hibernate就是用得这种方式进行转换的。 
实体对象 

点击(此处)折叠或打开

  1. package test;
  2. //实体对象,该对象的属性与数据库中的字段相同,当然可以改变具体看需求
  3. public class Person {
  4.     private int id;
  5.     private int age;
  6.     private String name;
  7.     public int getId() {
  8.         return id;
  9.     }
  10.     public void setId(int id) {
  11.         this.id = id;
  12.     }
  13.     public int getAge() {
  14.         return age;
  15.     }
  16.     public void setAge(int age) {
  17.         this.age = age;
  18.     }
  19.     public String getName() {
  20.         return name;
  21.     }
  22.     public void setName(String name) {
  23.         this.name = name;
  24.     }
  25.     
  26. }

点击(此处)折叠或打开

  1. package test;

  2. import java.lang.reflect.Field;
  3. import java.sql.Connection;
  4. import java.sql.PreparedStatement;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.util.List;

  8. public class Main {
  9.     //用于测试的方法
  10.     public static void main(String[] args) throws InstantiationException, IllegalAccessException, IllegalArgumentException, ClassNotFoundException {
  11.         Connection conn = DbUtils.getConn();
  12.         ResultSet rs = null;
  13.         PreparedStatement psmt = null;
  14.         System.out.println(conn);
  15.         try {
  16.             psmt = conn.prepareStatement("select * from person");
  17.             rs = psmt.executeQuery();
  18.             List list = DbUtils.populate(rs, Person.class);
  19.             for(int i = 0 ; i<list.size() ; i++){
  20.                 Person per = (Person) list.get(i);
  21.                 System.out.println("person : id = "+per.getId()+" name = "+per.getName()+" age = "+per.getAge());
  22.             }
  23.         } catch (SQLException e) {
  24.             e.printStackTrace();
  25.         }finally{
  26.             if(rs!=null){
  27.                 try {
  28.                     rs.close();
  29.                 } catch (SQLException e) {
  30.                     e.printStackTrace();
  31.                 }
  32.                 rs=null;
  33.             }
  34.             if(psmt!=null){
  35.                 try {
  36.                     psmt.close();
  37.                 } catch (SQLException e) {
  38.                     e.printStackTrace();
  39.                 }
  40.                 psmt=null;
  41.             }
  42.             if(conn!=null){
  43.                 try {
  44.                     conn.close();
  45.                 } catch (SQLException e) {
  46.                     e.printStackTrace();
  47.                 }
  48.                 conn=null;
  49.             }
  50.         }
  51.         
  52.     }

  53. }

具体的工具类 

点击(此处)折叠或打开

  1. package test;

  2. import java.lang.reflect.Field;
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.ResultSet;
  6. import java.sql.ResultSetMetaData;
  7. import java.sql.SQLException;
  8. import java.util.ArrayList;
  9. import java.util.List;

  10. public class DbUtils {
  11.     private static String url = "jdbc:mysql://localhost:3306/test";
  12.     private static String username = "root";
  13.     private static String password = "";
  14.     private static String driverClass = "com.mysql.jdbc.Driver";
  15.     //没什么好说的,获取数据库连接
  16.     public static Connection getConn(){
  17.         Connection conn = null;
  18.         try {
  19.             Class.forName(driverClass);
  20.             conn = DriverManager.getConnection(url,username,password);
  21.         } catch (ClassNotFoundException e) {
  22.             e.printStackTrace();
  23.         } catch (SQLException e) {
  24.             e.printStackTrace();
  25.         }
  26.         
  27.         return conn;
  28.     }
  29.     /*
  30.      * 将rs结果转换成对象列表
  31.      * @param rs jdbc结果集
  32.      * @param clazz 对象的映射类
  33.      * return 封装了对象的结果列表
  34.      */
  35.     public static List populate(ResultSet rs , Class clazz) throws SQLException, InstantiationException, IllegalAccessException{
  36.         //结果集的元素对象
  37.         ResultSetMetaData rsmd = rs.getMetaData();
  38.         //获取结果集的元素个数
  39.          int colCount = rsmd.getColumnCount();
  40. //         System.out.println("#");
  41. //         for(int i = 1;i<=colCount;i++){
  42. //             System.out.println(rsmd.getColumnName(i));
  43. //             System.out.println(rsmd.getColumnClassName(i));
  44. //             System.out.println("#");
  45. //         }
  46.          //返回结果的列表集合
  47.          List list = new ArrayList();
  48.          //业务对象的属性数组
  49.          Field[] fields = clazz.getDeclaredFields();
  50.          while(rs.next()){//对每一条记录进行操作
  51.              Object obj = clazz.newInstance();//构造业务对象实体
  52.              //将每一个字段取出进行赋值
  53.              for(int i = 1;i<=colCount;i++){
  54.                  Object value = rs.getObject(i);
  55.                  //寻找该列对应的对象属性
  56.                  for(int j=0;j<fields.length;j++){
  57.                      Field f = fields[j];
  58.                      //如果匹配进行赋值
  59.                      if(f.getName().equalsIgnoreCase(rsmd.getColumnName(i))){
  60.                          boolean flag = f.isAccessible();
  61.                          f.setAccessible(true);
  62.                          f.set(obj, value);
  63.                          f.setAccessible(flag);
  64.                      }
  65.                  }
  66.              }
  67.              list.add(obj);
  68.          }
  69.         return list;
  70.     }
  71. }

阅读(10681) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~