JDBC 结果集行为异常

发布于 2024-11-30 20:02:36 字数 636 浏览 0 评论 0原文

我正在访问我的数据库并获取一个 ResultSet,它通过调用 getRow() 告诉我当前行 # 是 2,但是当我调用 next() 方法时,它无法循环:

// Doesn't even loop once; fails on initial next() call -- entire loop skipped
try
{
    ResultSet oResults = executeSQL("SELECT * FROM widgets");

    // Returns a value of 2
    int iRowCount = oResults.getRow();

    if(iRowCount == 0)
        return;

    while(oResults.next())
    {
        // Never gets executed
    }

    // This gets executed though, and all the way down...
    // ...
}
catch(Exception e) { handleException(e); }

今天早上我花了近一个小时尝试弄清楚发生了什么,但这是我试图执行的文字代码(这是一个测试/沙箱项目,所以它可能没有逻辑意义)。这曾经发生在任何人身上吗?

I am hitting my database and getting a ResultSet that tells me the current row # is 2 with a call to getRow(), but that fails to loop when I call the next() method:

// Doesn't even loop once; fails on initial next() call -- entire loop skipped
try
{
    ResultSet oResults = executeSQL("SELECT * FROM widgets");

    // Returns a value of 2
    int iRowCount = oResults.getRow();

    if(iRowCount == 0)
        return;

    while(oResults.next())
    {
        // Never gets executed
    }

    // This gets executed though, and all the way down...
    // ...
}
catch(Exception e) { handleException(e); }

I've spent nearly an hour this morning trying to figure out what's happening, but this is the literal code that I am trying to execute (it's a test/sandbox project, so it might not make logical sense). This ever happen to anybody?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦明 2024-12-07 20:02:36

标准 JDBC 库返回一个 ResultSet,但不知道其中有多少行。除非您的 executeSQL 实现返回 CachedRowSet(本质上是预先滚动结果集并将所有行加载到内存中),您永远不会知道行数,直到您到达末尾时 oResults.next()返回假。

因此,您通过检查行数来控制程序流程的尝试将永远不会起作用。

接下来,调用 ResultSet。 getRow() 也可能没用。 javadoc 是这样说的:

检索当前行号。第一行是数字 1,第二行是数字 2,依此类推。
注意:对于结果集类型为 TYPE_FORWARD_ONLY 的 ResultSet,对 getRow 方法的支持是可选的

JDBC 驱动程序的大多数实现仅向前(即,您无法通过 previous() 方法),所以结果getRow() 可能没用。

您的代码应如下所示:

ResultSet oResults = executeSQL("SELECT * FROM widgets");

while(oResults.next())
{
    // Process rows
}

如果您仍然没有进入 while 循环,则意味着您的查询没有返回任何行 - 没有其他解释。如果没有行是“不可能的”,那么我建议您检查连接参数,以确保您连接到正确的数据库(例如,而不是连接到本地主机)。

The standard JDBC libraries return a ResultSet that has no clue as to how many rows are in it. Unless your implementation of executeSQL returns a CachedRowSet (which essentially has pre-scrolled through the result set and loaded all rows into memory), you'll never know the number of rows until you hit the end when oResults.next() returns false.

Thus your attempt at controlling program flow by checking the row count will never work.

Next, your call to ResultSet.getRow() is also probably useless. Here's what the javadoc says:

Retrieves the current row number. The first row is number 1, the second number 2, and so on.
Note: Support for the getRow method is optional for ResultSets with a result set type of TYPE_FORWARD_ONLY

Most implementations from JDBC drivers are forward only (ie you can't scroll backwards via the previous() method), so the result of getRow() is probably not useful.

Here's what your code should look like:

ResultSet oResults = executeSQL("SELECT * FROM widgets");

while(oResults.next())
{
    // Process rows
}

If you are still not getting into the while loop, it means your query is returning no rows - there's no other explanation. If getting no rows is "impossible", then I suggest you check your connection parameters to make sure you're connecting to the correct database (and not to localhost for example).

可可 2024-12-07 20:02:36

getRow() 方法允许您检查光标当前所在的行号,因此对 next() 的调用超出了结果集的范围。设置 iRowCount 后尝试设置 oResults.absolute(0);

The method getRow() lets you check the number of the row where the cursor is currently positioned, so the call to next() is out of range of your results set. Try setting oResults.absolute(0); after you set iRowCount.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文