码迷,mamicode.com
首页 > 其他好文 > 详细

利用反射技术将查询结果封装为对象

时间:2018-02-11 19:50:13      阅读:131      评论:0      收藏:0      [点我收藏+]

标签:view   ring   instant   pos   attribute   this   添加   trace   stack   

public class ORMTest extends HttpServlet {
private static final long serialVersionUID = 1L;

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//创建sql语句
String sql = "select username as Username,password as Password from user where id<?";
//创建个数组,用于给sql语句中的sql赋值
Object obj[] = new Object[]{1020};
try {
//调用getObject方法得到一个Object对象的集合
List<Object> list = getObject(sql, obj, User.class);
//将list存在request域中
request.setAttribute("list", list);
//转发到index.jsp页面
request.getRequestDispatcher("/index.jsp").forward(request, response);
} catch (InstantiationException | IllegalAccessException
| IllegalArgumentException | InvocationTargetException e) { e.printStackTrace();
}

}
static List<Object> getObject(String sql,Object obj[],Class clazz) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{
Connection conn=null;
PreparedStatement ps = null;
ResultSet rs = null;
JDBCUtils utils = JDBCUtils.getInstance();
List<Object> list = new ArrayList<>();
try {
conn = utils.getConnection();
ps = conn.prepareStatement(sql);
//获取参数的元数据
ParameterMetaData pmd = ps.getParameterMetaData();
//获取?个数
int count = pmd.getParameterCount();
//将数组中的元素对应的赋值给sql中的?
for(int i=1;i<=count;i++){
ps.setObject(i, obj[i-1]);
}
//执行sql
rs=ps.executeQuery();
//得到传进来的Bean的所有方法
Method ms[] = clazz.getMethods();
//遍历结果集
while(rs.next()){
//得到结果集元数据
ResultSetMetaData rsmd = rs.getMetaData();
//得到一个结果集的列数
int columnCount = rsmd.getColumnCount();
//得到bean的实例对象
Object object = clazz.newInstance();
//遍历结果集的列
for(int i=1;i<=columnCount;i++){
String columnLabel =rsmd.getColumnLabel(i);//得到列的别名
//得到别名后 可以得到这个列的bean的set方法
String methodName = "set"+columnLabel;
//遍历bean的所有方法
for (Method method : ms) {
//如果有和这个列的set方法相同的方法 if(method.getName().equals(methodName)){
//就将数据库查出来的这个列的值付给这个bean
method.invoke(object, rs.getObject(columnLabel));
}
}
}
//将这个bean添加到集合中 list.add(object); } } catch (SQLException e) {
e.printStackTrace();
}finally{
utils.free(conn, ps, rs);
}
return list;
}

}

//bean
public class User {
public User() {

}

public int id;
public String username;
public String password;
public String sex;
public String age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}

@Override
public String toString() {
return "id="+this.id+"username="+this.username+"password="+this.password
+"age="+this.age+"sex:"+this.sex;
}

}

利用反射技术将查询结果封装为对象

标签:view   ring   instant   pos   attribute   this   添加   trace   stack   

原文地址:https://www.cnblogs.com/jpfss/p/8442821.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!