在Java中實現數據庫分頁查詢可以通過使用數據庫查詢語句的LIMIT和OFFSET子句來實現。下面是一個示例代碼:
```java
public List
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
List
try {
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydatabase", "username", "password");
String sql = "SELECT * FROM employee LIMIT ? OFFSET ?";
stmt = conn.prepareStatement(sql);
int offset = (pageNumber - 1) * pageSize;
stmt.setInt(1, pageSize);
stmt.setInt(2, offset);
rs = stmt.executeQuery();
while(rs.next()) {
Employee employee = new Employee();
employee.setId(rs.getInt("id"));
employee.setName(rs.getString("name"));
employee.setAge(rs.getInt("age"));
// other columns
employees.add(employee);
}
} catch(SQLException e) {
e.printStackTrace();
} finally {
try {
if(rs != null) rs.close();
if(stmt != null) stmt.close();
if(conn != null) conn.close();
} catch(SQLException e) {
e.printStackTrace();
}
}
return employees;
}
```
在上面的示例中,我們使用了LIMIT和OFFSET子句來限制返回的記錄數和偏移量,實現了數據庫的分頁查詢。在調用getEmployees方法時,指定頁碼和每頁大小即可獲取相應的數據。