`

MySQL 分页语句

阅读更多
private static final String QUERYPERPAGESQL = "select * from book limit ?,?";
//该sql语句实现每页显示的记录条数

//按页获取记录
	public List<Book> queryPerPage(int page) {//page为当前处于第几页
		List<Book> list = new ArrayList<Book>();
		
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		int startRow = (page-1)*countPerPage;//countPerPage为每页显示的记录条数
		conn = MysqlDBConn.getMysqlDBConn().getconnection();
		try {
			ps = conn.prepareStatement(this.QUERYPERPAGESQL);
			ps.setInt(1, startRow);
			ps.setInt(2, countPerPage);
			rs = ps.executeQuery();
			while(rs.next())
			{
				Book book = new Book();
				book.setAuthor(rs.getString("author"));
				book.setBookname(rs.getString("bookname"));
				book.setDiscount(rs.getString("discount"));
				book.setPrice(rs.getString("price"));
				book.setId(rs.getInt("id"));
				book.setPublisher(rs.getString("publisher"));
				list.add(book);
			}
		} catch (SQLException e) {
			e.printStackTrace();
			throw new MyRunTimeException(e.getMessage(), e);
		}finally{
			colse(conn, ps, rs);
		}
		return list;
	}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics