请问一下,用反射我这个该怎么弄啊

逆风的猫 2019-07-29 02:27:48
代码如下 public class JdbcTest { /** * @param args */ public static void main(String[] args) { String sql="SELECT ID,NAME,SEX,GRADE from persontable where id= ?"; Peoper p1=get(Peoper.class,sql,1); System.out.println(p1); } public static <T>T get(Class<T> clazz,String sql,Object ... args){ T entity=null; Connection connection=null; //创造对象 PreparedStatement preparedStatement = null; //数据集 ResultSet resultSet = null; try { //获取连接 connection=ToosConnection.getConnection(); preparedStatement = connection.prepareStatement(sql); for(int i=0;i<args.length;i++){ preparedStatement.setObject(i+1, args[i]); } resultSet=preparedStatement.executeQuery(); if(resultSet.next()){ Map<String, Object> hm=new HashMap<String, Object>(); ResultSetMetaData rsm=resultSet.getMetaData(); int columnCount=rsm.getColumnCount(); entity=clazz.newInstance(); for(int i=0;i<columnCount;i++){ String columnLabel=rsm.getColumnLabel(i+1); Object columnValue=resultSet.getObject(columnLabel); hm.put(columnLabel, columnValue); //Toolsjdbc.setPropertity(entity, columnLabel, columnValue); //System.out.println(columnLabel+columnValue); }// for(Map.Entry<String, Object> entry:hm.entrySet()){ String propertyName=entry.getKey(); Object properVal=entry.getValue(); System.out.println(propertyName+properVal); Toolsjdbc.setPropertity(entity, propertyName, properVal); } } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InstantiationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } ToosConnection.realse(preparedStatement, connection, resultSet); return entity; } } 。。。。。 public class Peoper { public String id; public String name; public String sex; public String grade; public Peoper() { super(); // TODO Auto-generated constructor stub } public Peoper(String id, String name, String sex, String grade) { super(); this.id = id; this.name = name; this.sex = sex; this.grade = grade; } } 。。。。。 public class Toolsjdbc { public static void setPropertity(Object object,String propername,Object value){ //获取字节码文件 Class<? extends Object> c=object.getClass(); //获取对象的成员变量 Field f = null; try { f = c.getDeclaredField(propername); f.setAccessible(true); //给对象的成员变量赋值指定的值 f.set(object, value); } catch (SecurityException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (NoSuchFieldException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } 。。。。。 public class ToosConnection { public static Connection getConnection(){ //1.配置properties文件 Properties prop=new Properties(); //2,加载文件 try { prop.load(new FileInputStream("src/prp.properties")); } catch (FileNotFoundException e1) { e1.printStackTrace(); } catch (IOException e1) { e1.printStackTrace(); } String user=prop.getProperty("user"); String password=prop.getProperty("password"); String jdbcDriver=prop.getProperty("jdbcDriver"); String connection=prop.getProperty("connection"); //3加载驱动器 try { Class.forName(jdbcDriver); } catch (ClassNotFoundException e1) { e1.printStackTrace(); } //4通过drivermanaer的getconnection() Connection dm=null; try { dm = DriverManager.getConnection(connection, user, password); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return dm; } //关闭数据库连接 public static void realse(Statement statement,Connection connection,ResultSet resultSet){ if(statement!=null){ try { statement.close(); } catch (SQLException e) { e.printStackTrace(); } } if(connection!=null){ try { connection.close(); } catch (SQLException e) { e.printStackTrace(); } } if(resultSet!=null){ try { resultSet.close(); } catch (SQLException e) { e.printStackTrace(); } } } }
...全文
46 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
逆风的猫 2019-07-29
  • 打赏
  • 举报
回复
引用 1 楼 qq_36427590的回复:
是不是大小写问题?我测试过没毛病啊
是大小写的问题sql与对象属性大小写不一样谢谢
逆风的猫 2019-07-29
  • 打赏
  • 举报
回复
引用 2 楼 qq_36427590的回复:
console显示的是大写的"NAME",但是Peoper的字段名是小写,应该是rsm=resulSet.getMetaData()大小写出问题?反正反射那里是没问题的
嗯,是大小写的问题已经解决了谢谢
Ezirio 2019-07-29
  • 打赏
  • 举报
回复
console显示的是大写的"NAME",但是Peoper的字段名是小写,应该是rsm=resulSet.getMetaData()大小写出问题?反正反射那里是没问题的
Ezirio 2019-07-29
  • 打赏
  • 举报
回复
是不是大小写问题?我测试过没毛病啊

58,454

社区成员

发帖
与我相关
我的任务
社区描述
Java Eclipse
社区管理员
  • Eclipse
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧