简体   繁体   English

JDBC ResultSet表现异常

[英]JDBC ResultSet behaving strangely

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: 我正在访问数据库,并获得一个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'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? 这曾经发生在任何人身上吗?

The standard JDBC libraries return a ResultSet that has no clue as to how many rows are in it. 标准JDBC库返回的ResultSet不知道其中有多少行。 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. 除非您的executeSQL实现返回一个CachedRowSet (它实际上已经预滚动了结果集并将所有行加载到内存中),否则您将永远不会知道行数,直到oResults.next()返回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. 接下来,您对ResultSet.getRow()的调用也可能无用。 Here's what the javadoc says: 这是javadoc所说的:

Retrieves the current row number. 检索当前行号。 The first row is number 1, the second number 2, and so on. 第一行是数字1,第二行是数字2,依此类推。
Note: Support for the getRow method is optional for ResultSets with a result set type of TYPE_FORWARD_ONLY 注意:对于结果集类型为TYPE_FORWARD_ONLY的ResultSet,对getRow方法的支持是可选的

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. JDBC驱动程序的大多数实现都是向前的(即,您不能通过previous()方法向后滚动),因此getRow()的结果可能没有用。

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

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. 使用方法getRow()可以检查光标当前所在的行号,因此对next()的调用不在结果集的范围内。 Try setting oResults.absolute(0); 尝试设置oResults.absolute(0); after you set iRowCount. 设置iRowCount之后。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM