0
A
回答
1
這些都是德比的關鍵字,幫助實現分頁時:OFFSET and FETCH
用法示例:
//all these values are supplied by the GUI
int rowsPerPage = 10;
int pageNumber = 3;
String columnToSortBy = "first_name"; //this value should never be directly edited by the user. Otherwise it could be used for sql injection
StringBuilder statement = new StringBuilder();
statement.append("select * from users\n");
statement.append("order by " + columnToSortBy + "\n");
statement.append("offset ? rows fetch first ? rows only");
try (PreparedStatement ps = conn.prepareStatement(statement.toString())) {
ps.setInt(1, pageNumber * rowsPerPage);
ps.setInt(2, rowsPerPage);
ResultSet rs = ps.executeQuery();
printResultSet(rs);
}
一個更復雜的例子:
//all these values are supplied by the GUI
int rowsPerPage = 10;
int pageNumber = 3;
String columnToSortBy = "first_name"; //this value should never be directly edited by the user. Otherwise it could be used for sql injection
String selectClause = "select * from users\n";
String whereClause = "where first_name like ?";
String orderClause = "order by " + columnToSortBy + "\n";
String limitClause = "offset ? rows fetch first ? rows only";
//build the sql statement
String statement = "";
statement += selectClause;
statement += whereClause;
//get the total rows
int totalRows = 0;
try (PreparedStatement ps = conn.prepareStatement(statement)) {
ps.setString(1, "Sam%");
ResultSet rs = ps.executeQuery();
while (rs.next()) {
totalRows++;
}
}
System.out.println(totalRows + " total rows\n");
//change the statement to use pagination
statement += orderClause;
statement += limitClause;
//get the 3rd page
try (PreparedStatement ps = conn.prepareStatement(statement)) {
ps.setString(1, "Sam%");
ps.setInt(2, pageNumber * rowsPerPage);
ps.setInt(3, rowsPerPage);
ResultSet rs = ps.executeQuery();
printResultSet(rs);
}
System.out.println("\nNext page\n");
//get the 4th page
try (PreparedStatement ps = conn.prepareStatement(statement)) {
ps.setString(1, "Sam%");
ps.setInt(2, (pageNumber + 1) * rowsPerPage);
ps.setInt(3, rowsPerPage);
ResultSet rs = ps.executeQuery();
printResultSet(rs);
}
相關問題
- 1. 對Apache Derby使用LDAP
- 2. 使用Apache Derby的INET_ATON?
- 3. Apache Derby錯誤
- 4. apache derby + jpa
- 5. 使用jdbcTemplate進行分頁
- 6. 使用WP_Query進行分頁
- 7. 使用POST進行分頁
- 8. 使用UIScrollView進行分頁
- 9. 使用CreateDerivedCollection進行分頁
- 10. 使用LINQ進行分頁?
- 11. 使用Apache Spark進行實時分析
- 12. 使用Apache Spark進行重新分區
- 13. Apache derby多用戶控件?
- 14. 禁用Apache Derby的緩存
- 15. Apache Derby的分組或總誤差:
- 16. 如何配置Grails以使用Apache Derby?
- 17. 如何在Apache Derby中使用SEQUENCE?
- 18. apache derby - 解釋select
- 19. 使用GNU並行進行分頁
- 20. 的Apache Derby掛斷執行查詢
- 21. 將Apache Derby作爲Windows服務運行
- 22. 使用Apache Derby運行TestDB程序--java.sql.SQLException:url不能爲空
- 23. 使用LDAP進行Apache Drill
- 24. 使用HTML變量進行分頁
- 25. 使用Java API for ElasticSearch進行分頁
- 26. 在jsp中使用ajax進行分頁
- 27. 如何使用simpledb進行分頁?
- 28. 使用Datatable進行自定義分頁?
- 29. 使用PHP和SQL進行分頁
- 30. 使用休眠標準進行分頁
感謝很好寫的例子!另見http://stackoverflow.com/questions/19606571/derby-db-sql-select-rows-starting-from-row-number/19606655#19606655 –