public class UserTableDao {
/**
* 查询 表中的数据 返回列表集合
*/
public List<UserTable> queryUser(){
//定义返回集合
List<UserTable> list = new ArrayList<UserTable>();
//获得数据库链接
DBUtil db = new DBUtil();
Connection conn = db.conn();
//定义预编译接口对象
PreparedStatement ps =null;
//返回结果集对象
ResultSet rs = null;
//执行 数据库的操作
String sql = "select * from userTable";
try {
ps = conn.prepareStatement(sql);
//预编译 给sql语句 赋值
//ps.setString(parameterIndex, x);
//调用执行 方法
rs = ps.executeQuery();
//遍历结果集
while(rs.next()){
UserTable usEnt = new UserTable(
rs.getInt("userId"),
rs.getString("userName"),
rs.getString("userSex"),
rs.getString("userAddress"));
list.add(usEnt);
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
db.closeDB(rs, ps, conn);
}
return list;
}
//取出数据
public static void main(String[] args) {
UserTableDao udao = new UserTableDao();
List<UserTable> list = udao.queryUser();
for (UserTable uent : list) {
System.out.println(uent.toString());
}
}
}