请问一下,用反射我这个该怎么弄啊
代码如下
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();
}
}
}
}


...全文
相关推荐
Java 反射创建get set方法及反射方法的调用
本文将详细介绍如何利用Java反射来创建get和set方法,并演示如何通过反射机制调用这些方法。#### Java反射概述Java反射机制提供了一种方式,可以在运行时获取类的信息,并创建和操作对象。
Unity实现镜面反射
在镜面反射的Shader中,关键步骤包括:1. 计算法线贴图:法线贴图存储了物体表面的微小凹凸信息,帮助Shader模拟更精细的反射效果。2. 反射向量:根据视点位置和表面法线计算反射方向。3.
透反射matlab_透反射分离_
透反射matlab_透反射分离_"这个标题指的是利用MATLAB编程语言进行透反射现象的模拟和分析,特别是针对波浪与结构物交互作用时的入射波、反射波的分离和计算。
matlab计算反射阵单元相位(460652)_matlab反射阵相位_matlab反射阵_反射阵matlab_反射阵
首先,我们要了解反射阵的基本原理。反射阵是由多个天线单元组成的阵列,每个单元可以独立地调整相位,以实现特定的方向性或波束形状。
反射:C ++ 14反射库
在C++编程语言中,反射是一种允许程序在运行时检查自身结构和行为的能力。随着C++17标准的发布,虽然官方并没有直接提供反射机制,但开发者社区已经开始探索和实现各种反射库,以弥补这一功能的缺失。"


Eclipse
