Java中实现Oracle分页查询

/**
分页查询所有用户信息,page为要显示的第几页,row为每页有几行
*/
public UserVO findAllUser(int page,int row) {
		
		ResultSet rs = null;
		PreparedStatement prep = null;
		UserVO user = null;
		try {
/**
下面是实现分页的SQL语句
*/
			String sql = "select * from (select a.*,rownum rn from (select * from users where status=1) a) where  "+row+"*"+page+" >= rn and ("+page+"-1)*"+row+" < rn";
/**
Md5Utils.md5Jdk(user.getPasswd())为MD5加密算法,对密码进行加密处理,自定义然后封装在工具类中
*/
			prep = conn.prepareStatement(sql);
			rs = prep.executeQuery();
			while (rs.next()) {
				user = new UserVO();
				user.setUsername(rs.getString("username"));
				user.setPasswd(rs.getString("passwd"));
				user.setPower(rs.getInt("power"));
				user.setName(rs.getString("name"));
				user.setSex(rs.getString("sex"));
				user.setBirthday(rs.getDate("birthday"));
				user.setEmail(rs.getString("email"));
				user.setStatus(rs.getInt("status"));
				System.out.println(user.getUsername()+" "
				+Md5Utils.md5Jdk(user.getPasswd())+" " 
				+user.getPower()+" "
				+user.getName()+" "
				+user.getSex()+" "
				+user.getBirthday()+" "
				+user.getEmail()+" "
				+user.getStatus()
						);
			}

		} catch (Exception e) {
			throw new ServiceException("查询所有用户信息的时候出错了,,。", e);

		} finally {
/**
DbUtils.closeStatement(rs, null, prep)为封装在数据库中的关闭声明对象的方法

*/
			DbUtils.closeStatement(rs, null, prep);

		}

		return user;
	}

 

你可能感兴趣的:(oracle,Java)