JDBC 结果集行为异常
我正在访问我的数据库并获取一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
标准 JDBC 库返回一个
ResultSet
,但不知道其中有多少行。除非您的executeSQL
实现返回 CachedRowSet(本质上是预先滚动结果集并将所有行加载到内存中),您永远不会知道行数,直到您到达末尾时 oResults.next()返回假。因此,您通过检查行数来控制程序流程的尝试将永远不会起作用。
接下来,调用 ResultSet。 getRow() 也可能没用。 javadoc 是这样说的:
JDBC 驱动程序的大多数实现仅向前(即,您无法通过 previous() 方法),所以结果
getRow()
可能没用。您的代码应如下所示:
如果您仍然没有进入 while 循环,则意味着您的查询没有返回任何行 - 没有其他解释。如果没有行是“不可能的”,那么我建议您检查连接参数,以确保您连接到正确的数据库(例如,而不是连接到本地主机)。
The standard JDBC libraries return a
ResultSet
that has no clue as to how many rows are in it. Unless your implementation ofexecuteSQL
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:
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:
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).
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.