package cn.sxt.TestClass;import java.lang.reflect.Field;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.ResultSetMetaData;import java.sql.SQLException;import java.util.ArrayList;import java.util.List;import cn.sxt.model.Dept;import cn.sxt.util.DBUtil;public class TestUser { public static void main(String[] args) { String sql = "select * from dept"; List list = TestUser.rows2beans(sql, Dept.class); for (Object d : list) { // Dept dept = (Dept)d; System.out.println(d); } } public static List rows2beans(String sql, Class cls) { List list = new ArrayList<>(); // 连接数据库 Connection conn = DBUtil.getConnection(); PreparedStatement ps = null; ResultSet rs = null; try { // 向数据库发送sql语句 ps = conn.prepareStatement(sql); // 执行sql语句返回结果集 rs = ps.executeQuery(); // ResultSetMetaData可用于获取关于 ResultSet 对象中列的类型和属性信息的对象。 ResultSetMetaData metaData = rs.getMetaData(); // 获取查询的列数 int count = metaData.getColumnCount(); while (rs.next()) { // 创建反射实例 Object obj = cls.newInstance(); for (int i = 0; i < count; i++) { // 获取数据库列名 String fileName = metaData.getColumnName(i + 1).toLowerCase(); // 通过列名获取类中的属性的描述 Field field = cls.getDeclaredField(fileName); // 根据set方法名获取set方法对应的描述类 Method m = cls.getDeclaredMethod(getSetMethodName(fileName), field.getType()); // 判断接收的数据类型 Object object = rs.getObject(fileName); if (object != null) { if (field.getType().getName().equals("int") || field.getType().getName().equals("Object.lang.Integer")) { m.invoke(obj, rs.getInt(fileName)); } else if (field.getType().getName().equals("long") || field.getType().getName().equals("Object.lang.Long")) { m.invoke(obj, rs.getLong(fileName)); } else if (field.getType().getName().equals("short") || field.getType().getName().equals("Object.lang.Short")) { m.invoke(obj, rs.getShort(fileName)); } else if (field.getType().getName().equals("double") || field.getType().getName().equals("Object.lang.Double")) { m.invoke(obj, rs.getDouble(fileName)); } else if (field.getType().getName().equals("float") || field.getType().getName().equals("Object.lang.Float")) { m.invoke(obj, rs.getFloat(fileName)); } else if (field.getType().getName().equals("byte") || field.getType().getName().equals("Object.lang.Byte")) { m.invoke(obj, rs.getByte(fileName)); } else { m.invoke(obj, object); } } } list.add(obj); } } catch (SQLException e) { e.printStackTrace(); } catch (NoSuchFieldException e) { e.printStackTrace(); } catch (SecurityException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InvocationTargetException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InstantiationException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { DBUtil.close(conn, ps, rs); } return list; } public static String getSetMethodName(String fileName) { return "set" + fileName.substring(0, 1).toUpperCase() + fileName.substring(1); }}
package cn.sxt.util;import java.io.IOException;import java.io.InputStream;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import java.util.Properties;public class DBUtil { private static String driver = ""; private static String url = ""; private static String user = ""; private static String password = ""; static { InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("db.propertise"); Properties pt = new Properties(); try { pt.load(in); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } driver = pt.getProperty("driver"); url = pt.getProperty("url"); user = pt.getProperty("user"); password = pt.getProperty("password"); try { Class.forName(driver); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /* * public static void main(String[] args) { System.out.println(driver); * System.out.println(url); System.out.println(user); * System.out.println(password); * * } */ //获取数据库连接 public static Connection getConnection() { Connection conn = null; try { conn = DriverManager.getConnection(url, user, password); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return conn; } public static void close(Connection conn, Statement ps) { if (ps != null) { try { ps.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if (conn != null) { try { conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } public static void close(Connection conn, Statement ps, ResultSet rs) { if (rs != null) { try { rs.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if (ps != null) { try { ps.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if (conn != null) { try { conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
posted on 2019-04-23 20:54 阅读( ...) 评论( ...)