Java SQL:Statement.hasResultSet()? [英] Java SQL: Statement.hasResultSet()?

查看:131
本文介绍了Java SQL:Statement.hasResultSet()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序在一个平台上使用MySQL而在另一个平台上使用SQLite,因此存在差异,例如在使用查询时如 DELETE FROM USERS

My app uses MySQL on one platform and SQLite on another, as such there are differences, such as that when using query like DELETE FROM USERS:


  • MySQL 上, PreparedStatement.getResultSet()将返回 null

  • SQLite 上, PreparedStatement.getResultSet()将抛出 java.sql.SQLException:没有可用的ResultSet

  • On MySQL, PreparedStatement.getResultSet() will return null.
  • On SQLite, PreparedStatement.getResultSet() will throw java.sql.SQLException: no ResultSet available.

这可能是也可能不是SQLite实现中的错误(我认为它应该返回 null ),但我必须以某种方式处理它。

This may or may not be a bug in SQLite implementation (I think it is supposed to return null), but I have to deal with this somehow.

我可以使用 try {...} catch(SQLException e){...} ,如果异常消息是没有ResultSet可用,只需手动返回null。这不是一种正确的方法。

I could use a try { ... } catch (SQLException e) { ... } and if the exception message is "no ResultSet available", simply return null manually. This doesn't feel like a right way to do it though.

我可以提出一个if,检查正在使用的JDBC驱动程序并做出相应的反应,但同样,这并不是解决这个问题的好方法。

I could put up an if that makes a check on what JDBC driver is being used and react accordingly, but again, that doesn't feel like a good solution to the problem.

我真正想要的是像这样的方法.hasResultSet( )返回布尔 获取SQL命令的方法( SELECT,UPDATE ,INSERT 等)来自已执行的语句。 我在SQL API中找不到两者。

What I would really like is either a method like .hasResultSet() that returns a boolean OR a way to get the SQL command (SELECT, UPDATE, INSERT etc) from a statement that has been executed. I can find neither of the two in SQL API though.

推荐答案

执行返回未知数量结果的查询时,需要使用 execute() 。此方法返回 boolean ,指示结果类型:

When executing a query that returns an unknown amount of results, then you need to use execute(). This method returns a boolean indicating the type of result:


  • true :结果是 ResultSet

  • false :结果是更新计数

  • true: result is a ResultSet
  • false : result is an update count

如果结果是 true ,然后使用 getResultSet() 检索 ResultSet ,否则 getUpdateCount() to检索更新计数。如果更新计数为 -1 ,则表示没有更多结果。请注意,当前结果为 ResultSet 时,更新计数也将为 -1 。如果没有更多结果或者结果是更新计数,那么 getResultSet()应该返回null也是很好的,所以SQL Lite的行为会抛出一个异常似乎是错误的。

If the result is true, then you use getResultSet() to retrieve the ResultSet, otherwise getUpdateCount() to retrieve the update count. If the update count is -1 it means there are no more results. Note that the update count will also be -1 when the current result is a ResultSet. It is also good to know that getResultSet() should return null if there are no more results or if the result is an update count, so the behavior of SQL Lite to throw an exception seems to be wrong.

现在,如果要检索更多结果,请调用 getMoreResults() (或其兄弟接受 int 参数)。此方法的 boolean 返回值与 execute()具有相同的含义,因此 false 并不意味着没有更多的结果!

Now if you want to retrieve more results, you call getMoreResults() (or its brother accepting an int parameter). The boolean return value of this method has the same meaning as that of execute(), so false does not mean there are no more results!

如果 getMoreResults(),只有没有结果返回false getUpdateCount()返回 -1 (同样记录在Javadoc中)

There are only no more results if the getMoreResults() returns false and getUpdateCount() returns -1 (as also documented in the Javadoc)

基本上这意味着如果你想正确处理所有结果,你需要做一些事情:

Essentially this means that if you want to correctly process all results you need to do something like:

PreparedStatement pstmt = connection.prepareStatement(...);
// ...
boolean result = pstmt.execute();
while(true)
    if (result) {
        ResultSet rs = pstmt.getResultSet();
        // Do something with resultset ...
    } else {
        int updateCount = pstmt.getUpdateCount();
        if (updateCount == -1) {
            // no more results
            break;
        }
        // Do something with update count ...
    }
    result = pstmt.getMoreResults();
}

这篇关于Java SQL:Statement.hasResultSet()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆