码迷,mamicode.com
首页 > 数据库 > 详细

JDBC学习笔记(16):利用反射技术奖查询结果封装为对象

时间:2015-04-03 20:46:47      阅读:193      评论:0      收藏:0      [点我收藏+]

标签:

 1 package com.xxyh.jdbc;
 2 import java.lang.reflect.InvocationTargetException;
 3 import java.lang.reflect.Method;
 4 import java.sql.Connection;
 5 import java.sql.PreparedStatement;
 6 import java.sql.ResultSet;
 7 import java.sql.ResultSetMetaData;
 8 import java.sql.SQLException;
 9 import com.xxyh.jdbc.domain.User;
10 public class ORMTest {
11     
12     public static void main(String[] args) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, SQLException {
13         User user = getUser("select id as Id, name as Name, birthday as Birthday, money as Money from user where id=1");
14         System.out.println(user);
15     }
16     
17     static User getUser(String sql) throws SQLException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
18         Connection conn = null;
19         PreparedStatement ps = null;
20         ResultSet rs = null;
21         try {
22             conn = JdbcUtils.getConnection();
23             ps = conn.prepareStatement(sql);
24             rs = ps.executeQuery();
25             
26             ResultSetMetaData rsmd = rs.getMetaData();
27             int count = rsmd.getColumnCount();
28             String[] colNames = new String[count];
29             for (int i = 1; i <= count; i++) 
30                 colNames[i-1] = rsmd.getColumnLabel(i);
31              
32             User user = null;
33             
34             if (rs.next()) {
35                 user = new User();
36                 for (int i = 0; i < colNames.length; i++) {
37                     String colName = colNames[i];
38                     String methodName = "set" + colName;
39                     Method[] methods = user.getClass().getMethods();
40                     for (Method method : methods) {
41                         if (methodName.equals(method.getName())) {
42                             method.invoke(user, rs.getObject(colName));
43                         }
44                     }
45                 }
46             }
47             return user; 
48         } finally {
49             JdbcUtils.close(rs, ps, conn);
50         }
51         
52     }
53 }
【运行结果】:
[1,zhangs,1985-01-01,360.0]
 
注意:输出结果显示如上情形是由于User的toString方法经过重写:
@Override
public String toString() {
    return "[" + getId() + "," + getName() + "," + getBirthday() + "," + getMoney() + "]";
}

通过使用泛型提高程序的通用性,getObject方法与具体的类无关:

 1 package com.xxyh.jdbc;
 2 import java.lang.reflect.InvocationTargetException;
 3 import java.lang.reflect.Method;
 4 import java.sql.Connection;
 5 import java.sql.PreparedStatement;
 6 import java.sql.ResultSet;
 7 import java.sql.ResultSetMetaData;
 8 import java.sql.SQLException;
 9 import com.xxyh.jdbc.domain.User;
10 public class ORMTest {
11     
12     public static void main(String[] args) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, SQLException, InstantiationException {
13         User user = (User) getObject("select id as Id, name as Name, birthday as Birthday, money as Money from user where id=2",User.class);
14         System.out.println(user);
15     }
16     
17     static Object getObject(String sql, Class<?> clazz) throws SQLException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException {
18         Connection conn = null;
19         PreparedStatement ps = null;
20         ResultSet rs = null;
21         try {
22             conn = JdbcUtils.getConnection();
23             ps = conn.prepareStatement(sql);
24             rs = ps.executeQuery();
25             
26             ResultSetMetaData rsmd = rs.getMetaData();
27             int count = rsmd.getColumnCount();
28             String[] colNames = new String[count];
29             for (int i = 1; i <= count; i++) 
30                 colNames[i-1] = rsmd.getColumnLabel(i);
31              
32             Object object = null;
33             Method[] methods = clazz.getMethods();
34             if (rs.next()) {
35                 object = clazz.newInstance();// 创建一个对象
36                 for (int i = 0; i < colNames.length; i++) {
37                     String colName = colNames[i];
38                     String methodName = "set" + colName;
39                     for (Method method : methods) {
40                         if (methodName.equals(method.getName())) {
41                             method.invoke(object, rs.getObject(colName));
42                         }
43                     }
44                 }
45             }
46             return object; 
47         } finally {
48             JdbcUtils.close(rs, ps, conn);
49         }
50         
51     }
52 }
【运行结果】:
[2,lisi,1986-01-01,900.0]  

JDBC学习笔记(16):利用反射技术奖查询结果封装为对象

标签:

原文地址:http://www.cnblogs.com/xiaoxiaoyihan/p/4390839.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!