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

逆风的猫 逆风的猫 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
代码如下 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(); } } } }
...全文

相关推荐

评论 4
收藏 已收藏
分享
返回顶部
逆风的猫
Eclipse 58,439 成员 49,457 内容