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

 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]  

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。