如何确定获得的Java ResultSet是否为空?
Class.forName("org.sqlite.JDBC"); Connection conn = DriverManager.getConnection("jdbc:sqlite:userdata.db"); Statement stat = conn.createStatement(); ResultSet rs = stat.executeQuery("SELECT * from table WHERE is_query_processed = 0;"); int rowcount = rs.getRow(); System.out.println("Row count = "+rowcount); // output 1 rs.first(); // This statement generates an exception
为什么会这样?
我通常使用的模式如下:
boolean empty = true; while( rs.next() ) { // ResultSet processing here empty = false; } if( empty ) { // Empty result set }
这是一个简单的方法:
public static boolean isResultSetEmpty(ResultSet resultSet) { return !resultSet.first(); }
注意事项
这会将光标移动到开头。 但是如果你只是想测试它是否为空,那么你可能还没有做任何事情。
另外
在进行任何处理之前,请立即使用first()
方法。 ResultSet rs = stat.executeQuery(“SELECT * from table WHERE is_query_processed = 0;”);
if(rs.first()) { // there's stuff to do } else { // rs was empty }
参考
ResultSet(Java Platform SE 6)
你也可以这样做:
rs.last(); int numberOfRows = rs.getRow(); if(numberOfRows) { rs.beforeFirst(); while(rs.next()) { ... } }
while (results.next())
用于遍历结果集。如果结果为空,则结果.next()将返回false。
为什么执行没有进入while循环?
如果ResultSet为空,则rs.next()
方法返回false,并且无论rownumber(不计数) rs.getRow()
返回,都不会输入while循环的主体。 科林斯的例子很有效。
向前和向后移动光标以确定行数不是正常的JDBC实践。 通常的JDBC实践是将ResultSet
映射到值对象的List
,每个值对象表示一个表行实体,然后只使用List
方法来确定是否有任何行。
例如:
List users = userDAO.list(); if (users.isEmpty()) { // It is empty! if (users.size() == 1) { // It has only one row! } else { // It has more than one row! }
list()
方法如下所示:
public List list() throws SQLException { Connection connection = null; Statement statement = null; ResultSet resultSet = null; List users = new ArrayList (); try { connection = database.getConnection(); statement = connection.createStatement(); resultSet = statement.executeQuery(SQL_LIST); while (resultSet.next()) { User user = new User(); user.setId(resultSet.getLong("id")); user.setName(resultSet.getString("name")); // ... users.add(user); } } finally { if (resultSet != null) try { resultSet.close(); } catch (SQLException logOrIgnore) {} if (statement != null) try { statement.close(); } catch (SQLException logOrIgnore) {} if (connection != null) try { connection.close(); } catch (SQLException logOrIgnore) {} } return users; }
另请参阅此答案以了解其他JDBC示例。
CLOSE_CURSORS_AT_COMMIT
public static final int CLOSE_CURSORS_AT_COMMIT
The constant indicating that ResultSet objects should be closed when the method Connection.commit is called.
试试这个:
ResultSet MyResult = null; MyResult = Conexion.createStatement().executeQuery("Your Query Here!!!"); MyResult.last(); int NumResut = MyResult.getRow();MyResult.beforeFirst(); //Follow with your other operations....
这种方式你将能够正常工作。
可能是您可以将结果集对象转换为String对象并检查它是否为空。
`if(resultset.toString().isEmpty()){ // containg null result } else{ //This conains the result you want }`
这样可以在不跳过第一条记录时检查它是否为空
if (rs.first()) { do { // ResultSet is not empty, Iterate over it } while (rs.next()); } else { // ResultSet is empty }
- Java实例变量可访问性
- 如何在JSF中使用带有UIData的java.util.Set。 特别是h:数据表?
- 引起:java.lang.ClassNotFoundException:org.apache.zookeeper.KeeperException
- Play-Framework 2.4:使用Spring-Depedency Injection与Play-Framework而不是Guice
- ASP.NET Master Pages等效于Java
- 直到Runtime.getRuntime()。exec()完成执行后才会显示Swing消息
- 在字符串中生成字符组合并不完全有效,为什么?
- 通过Java Servlet中的jquery ajax发送参数
- 如何在JavaFX 2中打开另一个窗口?