【发布时间】:2017-05-09 21:13:41
【问题描述】:
我在理解 JDBC java 中的分页时遇到了问题,我希望有人能告诉我一些关于它的事情,我是 java 编程的新手,作为示例不容易理解这些方法的功能setFetchsize() ; getFetchSize();依此类推,我尝试在 Mysql 中创建一个关于用户的表,然后通过使用此语句“SELECT * FROM User”从表中获取所有信息来进行分页,我认为我将获得前 10 行,然后是 10 行,因为它的默认值,我必须从数据库中获取所有信息作为分页,这是我的代码,我尝试将 LIMIT 与它一起使用,但我仍然不明白它是如何工作的。
@Override
public List<User> getAll() throws SQLException {
try (PreparedStatement statement = connection.getConnection()
.prepareStatement("SELECT * FROM User")) {
int fetchSize = statement.getFetchSize();
System.out.println("Statement fetch size : " + fetchSize);
statement.setFetchSize(50);
ResultSet result = statement.executeQuery();
result.setFetchSize(33);
while (result.next()) {
list.add(extractUser(result));
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
System.out.println(list);
return list;
}
private User extractUser(ResultSet result) throws SQLException {
long id = result.getLong(1);
String userName = result.getString(2);
String firstName = result.getString(3);
String lastName = result.getString(4);
long teamId = result.getLong(5);
String userStatus = result.getString(6);
return new User(id, userName, firstName, lastName, teamId, userStatus);
}
【问题讨论】: