T t = null;
try {
Class cl = Class.forName(classPath);
//得到这个类的所有成员
Field[] name = cl.getDeclaredFields();
//得到这个类中所有的方法
Method[] method = cl.getDeclaredMethods();
//实例化
t = (T) cl.newInstance();
//调用set设置值
for(int i =0;i
int h =0;
if(((i/2)-1)<=0){
h=0;
}else{
h=i/2-1;
}
String type=name[h].getType().getName();
type = type.substring(type.lastIndexOf(".")+1,type.length());
System.out.println(type+" "+method[i].getName());
if(type.toString().equals("int")){
if(rs.getInt(method[i].getName().toLowerCase().substring(3,method[i].getName().length()))!=0){
method[i].invoke(t,rs.getInt(method[i].getName().toLowerCase().substring(3,method[i].getName().length())));
}else{
method[i].invoke(t,0);
}
}else if(type.toString().toLowerCase().equals("string")){
if(rs.getString(method[i].getName().toLowerCase().substring(3,method[i].getName().length()))!=null){
method[i].invoke(t,rs.getString(method[i].getName().toLowerCase().substring(3,method[i].getName().length())));
}
}else if(type.toString().toLowerCase().equals("date")){
if(rs.getString(method[i].getName().toLowerCase().substring(3,method[i].getName().length()))!=null){
method[i].invoke(t,rs.getDate(method[i].getName().toLowerCase().substring(3,method[i].getName().length())));
}
}else if(type.toString().toLowerCase().equals("double")){
if(rs.getDouble(method[i].getName().toLowerCase().substring(3,method[i].getName().length()))!=0){
method[i].invoke(t,rs.getDouble(method[i].getName().toLowerCase().substring(3,method[i].getName().length())));
}else
{
method[i].invoke(t,0.0);
}
}
}
}
// 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();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return t;
}
/**
* 方法二:将resultSet自动变成对应的类
* 都是set
* @param 类的完整路径
* @param resultSet结果集
* @return 对应的类
*/
public T setT(String classpath,ResultSet rs){
T t = null;
try {
Class classType = Class.forName(classpath);
//得到这个类的所有成员
Field[] name = classType.getDeclaredFields();
//得到这个类中所有的方法
Method[] method = classType.getDeclaredMethods();
//实例化
t = (T) classType.newInstance();
// 获得对象的所有属性
Field fields[] = classType.getDeclaredFields();
for (int i = 0; i < fields.length; i++) {
Field field = fields[i];
String fieldName = field.getName();
String firstLetter = fieldName.substring(0, 1).toUpperCase();
// // 获得和属性对应的getXXX()方法的名字
// String getMethodName = "get" + firstLetter + fieldName.substring(1);
// 获得和属性对应的setXXX()方法的名字