Java 反射封装JDBC查询,条件查询、全部查询封装到一个类结果为对象;

//封装方法

package com.huanyuguoji.Test;



import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;


public class Jdbc {
public static Connection conn = null;
public static PreparedStatement pre = null;
public static ResultSet set = null;


public static void Driver() {
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");
System.out.println("驱动加载");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


}


public static Object select(Class c, HashMap map) {
Object op[]=null;
Jdbc.Driver();
Object p = null;
String classname = c.getSimpleName();
String sql = "select * from " + classname;
int i = 0;
Set sets = null;
if (map != null) {
sets = map.keySet();
for (String string : sets) {
if (i == 0) {
sql += " where " + string + "= ? ";
i++;
} else if (i > 0) {
sql += " and " + string + "=? ";
i++;
}
}
}
try {
pre = conn.prepareStatement(sql);
System.out.println(sql);
if(map!=null) {
i = 1;
for (String string : sets) {


pre.setObject(i++, map.get(string));
}
}
set = pre.executeQuery();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

int l=0;
if (set != null) {
try {
while (set.next()) {

op =new Object[set.getRow()];//set.getRow方法获取长度,定义Object数组长度
System.out.println(set.getString("name"));
p = c.newInstance();//创建当前类反射对象
Field[] fiel = c.getDeclaredFields();//获取属性
for (Field field : fiel) {
String fieldName = field.getName();//获取属性名

String toupcas = "set" + fieldName.substring(0, 1).toUpperCase()
+ fieldName.substring(1);//截取首字母并大写在加上除去首字母加set就是set方法
Method method = c.getMethod(toupcas, field.getType());//获取  (set方法,参数类型)
                      method.invoke(p, set.getObject(fieldName));//往反射对象里的set方法添加数据库字段
                  
}
op[l]=p;
l++;
}



} 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();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}  catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

return op;

}

}

--------------------------------------------------------------------------------------------------------------------------

//主方法

package com.huanyuguoji.Test;



import java.util.HashMap;


public class Test {


public static void main(String[] args) {
// TODO Auto-generated method stub
HashMapmap=new HashMap<>();
map.put("name", "zhangs");

      Object[] in= (Object[]) Jdbc.select(ClassName.class, null);
       for (Object object : in) {
ClassName name=(ClassName) object;
System.out.println(name.getAddress()+"---"+name.getName()+"---"+name.getAge()+"---"+name.getSsex());
}
}


}

------------------------------------------------------------------------------------------------------------------------

//主类

package com.huanyuguoji.Test;



public class ClassName {
  private String name;
  private String age;
  private String ssex;
  private String address;
public ClassName() {

}
public ClassName(String name, String age, String ssex, String address) {

this.name = name;
this.age = age;
this.ssex = ssex;
this.address = address;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getSsex() {
return ssex;
}
public void setSsex(String ssex) {
this.ssex = ssex;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
  
}

你可能感兴趣的:(Java,反射)