jdbc分頁

//s_pageNo起始頁面
//s_pageSize每頁的大小
public List<User> getUsers(int pageNo, int pageSize) {
		connector = new JDBCConnector();
		Connection con = connector.getConnectionByJDBC();
		ResultSet rs = null;
		PreparedStatement ps = null;
		User user = null;
		List<User> users = new ArrayList<User>();
		try {
			String sql = "select * from User";
			ps = con.prepareStatement(sql,
					ResultSet.TYPE_SCROLL_INSENSITIVE,
					ResultSet.CONCUR_READ_ONLY);
			ps.setMaxRows(pageNo*pageSize);
			rs = ps.executeQuery();
			rs.relative(pageNo - 1)*pageSize+1;
			while (!rs.isAfterLast()) {
				user = new User();
				user.setId(rs.getInt("id"));
				user.setUserName(rs.getString("username"));
				user.setPassword(rs.getInt("password"));
				user.setCreatetime(rs.getDate("createtime"));
				users.add(user);
				rs.next();
			}
			ps.close();
			rs.close();
			con.close();
			connector.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return users;
	}
相關文章
相關標籤/搜索