java上一条代码 java第一条代码( 四 )


ResultSet rs = pstmt.executeQuery();
if(rs.next()){
u = new User();
u.setId(rs.getInt("id"));
u.setUsername(rs.getString("username"));
u.setPassword(rs.getString("password"));
u.setValid(rs.getString("valid"));
}
DBUtil.close(rs);
DBUtil.close(pstmt);
DBUtil.close(conn);
return u;
}
public Listlt;Usergt; list() throws Exception {
Listlt;Usergt; list = new ArrayListlt;Usergt;();
String sql = "select id,username,password,valid from s_user order by id";
Connection conn = DBUtil.getConnection();
Statement stmt = conn.createStatement();
System.out.println("打印sql:"+sql);
ResultSet rs = stmt.executeQuery(sql);
while(rs.next()){
int id = rs.getInt("id");
String username = rs.getString("username");
String password = rs.getString("password");
String valid = rs.getString("valid");
User u = new User();
u.setId(id);
u.setUsername(username);
u.setPassword(password);
u.setValid(valid);
list.add(u);
}
DBUtil.close(rs);
DBUtil.close(stmt);
DBUtil.close(conn);
return list;
}
public Listlt;Usergt; list(Pagination p) throws Exception {
Listlt;Usergt; list = new ArrayListlt;Usergt;();
String sql = "select * from (select ini.*,rownum rn from " +
"(select id,username,password,valid from s_user order by id) ini " +
"where rownum lt;= ?) where rn gt;= ? ";
int begin = p.getPageSize()*(p.getPage()-1)+1;//10*(3-1)+1=211-10 11-20 21-30
int end = p.getPageSize()*p.getPage();//10*3=30
Connection conn = DBUtil.getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, end);
pstmt.setInt(2, begin);
System.out.println("打印sql:"+sql+"\t参数:["+end+","+begin+"]");
ResultSet rs = pstmt.executeQuery();
while(rs.next()){
int id = rs.getInt("id");
String username = rs.getString("username");
String password = rs.getString("password");
String valid = rs.getString("valid");
User u = new User();
u.setId(id);
u.setUsername(username);
u.setPassword(password);
u.setValid(valid);
list.add(u);
}
DBUtil.close(rs);
DBUtil.close(pstmt);
DBUtil.close(conn);
return list;
}
public void update(User u) throws Exception {
String sql = "update s_user set username=?,password=?,valid=? where id=?";
Connection conn = DBUtil.getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, u.getUsername());
pstmt.setString(2, u.getPassword());
pstmt.setString(3, u.getValid());
pstmt.setInt(4, u.getId());
System.out.println("打印sql:"+sql+"\t参数:["+u.getUsername()+","+u.getPassword()+","+u.getValid()+","+u.getId()+"]");
pstmt.executeUpdate();
DBUtil.close(pstmt);
DBUtil.close(conn);
}
/**
* 设置最大页
* @param p
* @throws SQLException
*/
public void getMaxPage(Pagination p) throws SQLException{
String sql = "select count(*) from s_user";
Connection conn = DBUtil.getConnection();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
if(rs.next()){
int s = rs.getInt(1);//一共有多少条数据
int n = p.getPageSize();//每页大小
int t = (s+n-1)/n;
p.setTotalPage(t);//共有多少页
}
DBUtil.close(rs);
DBUtil.close(stmt);
DBUtil.close(conn);
}
/**
* 测试UserDaoImpl
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
UserDaoImpl impl = new UserDaoImpl();
int size = impl.list().size();
System.out.println("size======"+size);//size======0
}
}
总体来说 , 这是一个翻页查询问题 。

推荐阅读