我的查詢意味着只返回一行(因爲它是通過唯一ID選擇記錄)。如果結果集是空的,我將拋出一個異常,如果它包含多於一行,則拋出異常。恕我直言,在這種情況下,while(rs.next())的結果集看起來並不漂亮。我可以立即獲取結果集中的行數嗎?如何獲取ResultSet包含的行數?
0
A
回答
3
我不認爲有一種方法可以在單個方法調用中執行此操作。
您可以使用ResultSet的last()
方法,然後調用ResultSet的getRow()
方法,該方法將根據大小給出ResultSet大小並根據需要拋出異常。
請記得有最終條款關閉所有連接/ DS :)
rs.last();
if(rs.getRow() > 1) {
throw (...);
}
3
rs.first();
if(!rs.isLast())
throw new Exception(...);
看來你並不需要知道的行數返回,只是如果有超過1
+0
這就是我實際使用的情況。 – Ivan
0
你試過: java.sql.ResultSet.isBeforeFirst()
來自javadoc: 檢索遊標是否位於此ResultSet對象的第一行之前。
Note:Support for the isBeforeFirst method is optional for ResultSets with a result set type of TYPE_FORWARD_ONLY
Returns:
true if the cursor is before the first row; false if the cursor is at any other position or the result set contains no ro
相關問題
- 1. 如何獲取JDBC ResultSet中的列數?
- 2. ResultSet不包含新插入的行
- 3. 獲取Java中ResultSet返回的行數
- 4. 如何從executeBatch獲取ResultSet?
- 5. 如何獲取使用HSQLDB時由Resultset返回的行數?
- 6. 從jdbc ResultSet獲取數據行
- 7. 如何獲取包含中文字符的行數?
- 8. 獲取包含函數
- 9. 如何獲取表中包含其他行的所有行
- 10. 如何獲取UDP數據包中包含的信息?
- 11. 的ResultSet中的servlet包含空值
- 12. 獲取ResultSet列名
- 13. 如何一般從ResultSet獲取值?
- 14. 如何從ResultSet對象獲取COUNT?
- 15. 如何從cassandra-jdbc ResultSet獲取列名?
- 16. 獲取包含字符串的行號
- 17. 獲取包含東西的整行
- 18. 如何獲取使用ResultSet執行的查詢?
- 19. 包含key1的,不包含密鑰key2獲取行
- 20. 獲取包含數據的最小/最大行號的行號?
- 21. 獲取ResultSet列的數據類型
- 22. 如何獲取NSString中的數字(包含數字和字符)?
- 23. Java SQL存儲過程ResultSet無法從僅包含1行的結果中獲取數據
- 24. 如何獲得java中的總行數excel包含行null?
- 25. 如何獲取包含圖像
- 26. 如何獲取wp包含目錄?
- 27. 如何獲取包含列表
- 28. 如何獲取包含一個datepicker
- 29. 如何在dropdownlist中獲取包含值?
- 30. 確定ResultSet中的列是否包含所有行中的值
重複。請參閱:[如何獲取java.sql.ResultSet的大小?](http://stackoverflow.com/q/192078) – user272735