java用实体类接收多条数据库数据怎么写
比如这样select*fromtable,注意是多条数据,比如用户有姓名年龄等,然后输出多条用户信息...
比如这样select * from table,注意是多条数据,比如用户有姓名年龄等,然后输出多条用户信息
展开
1个回答
展开全部
多条数据可以返回一个list啊,我给你看一个例子:
public List SearchAll(){
List l = new ArrayList();
EmployeeBean ea = null;
ResultSet rs = null;
PreparedStatement ps = null;
Connection con = null;
sql = "select * from Employee";
try {
con = BaseDAO.GetConnection();
ps = con.prepareStatement(sql);
rs = ps.executeQuery();
while (rs.next()) {
ea = new EmployeeBean();
ea.setId(rs.getInt(1));
ea.setDerpartId(rs.getInt(2));
ea.setName(rs.getString(3));
ea.setPassword(rs.getString(4));
l.add(ea);
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally{
BaseDAO.close(rs, ps, con);
}
return l;
}
public List SearchAll(){
List l = new ArrayList();
EmployeeBean ea = null;
ResultSet rs = null;
PreparedStatement ps = null;
Connection con = null;
sql = "select * from Employee";
try {
con = BaseDAO.GetConnection();
ps = con.prepareStatement(sql);
rs = ps.executeQuery();
while (rs.next()) {
ea = new EmployeeBean();
ea.setId(rs.getInt(1));
ea.setDerpartId(rs.getInt(2));
ea.setName(rs.getString(3));
ea.setPassword(rs.getString(4));
l.add(ea);
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally{
BaseDAO.close(rs, ps, con);
}
return l;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询