java ResultSet 未正确定位,也许您需要调用 next
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10830145/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ResultSet not positioned properly, perhaps you need to call next
提问by Corsa di Tempo
Hi I have problem with this code as it seems to always return the SQL Exception. It was working for a few goes then stopped and came back with exception. so I'm not sure what's wrong. I have tried to change the vld.close() to different places before so yeah. Thanks
嗨,我对这段代码有疑问,因为它似乎总是返回 SQL 异常。它工作了几次然后停止并异常返回。所以我不确定出了什么问题。我曾尝试将 vld.close() 更改为不同的地方,所以是的。谢谢
PreparedStatement vld = conn.prepareStatement("SELECT * FROM carsharing.available(?,?,?,?)");
vld.setString(1, carName);
vld.setString(2, memberUser);
vld.setTimestamp(3, new java.sql.Timestamp(startTime.getTime()));
vld.setTimestamp(4, new java.sql.Timestamp(endTime.getTime()));
System.out.println("test1");
ResultSet rset = vld.executeQuery();
System.out.println("test2");
rset.next();
int num = rset.getInt("num");
boolean valid = rset.getBoolean("aval");
vld.close();
System.out.println(num);
System.out.println(valid);
回答by hcpl
1) Verify SQL
1)验证SQL
Can you verify first that the SQL is correct? It doesn't look correct to me. What you now try to execute is the following SQL:
您能先验证 SQL 是否正确吗?对我来说看起来不正确。您现在尝试执行的是以下 SQL:
SELECT * FROM carsharing.available(?,?,?,?)
If you want to limit the resultset by the given name, user and timestamps SQL should look like:
如果要按给定名称限制结果集,用户和时间戳 SQL 应如下所示:
SELECT * FROM carsharing WHERE carNameField=? AND memberUserField=? AND tsField1=? AND tsField2=?
Where you would replace the Field names with the correct column names for your schema.
将字段名称替换为架构的正确列名称的位置。
2) Resultset navigation
2) 结果集导航
Then when you have your resultset after execution you can use first() to navigate to the first position and it also returns boolean so you can check if there even are results available. If you want more values fetched you'll need a loop.
然后,当您在执行后获得结果集时,您可以使用 first() 导航到第一个位置,它还返回布尔值,以便您可以检查是否有可用的结果。如果您想要获取更多值,则需要一个循环。
//...
ResultSet rset = vld.executeQuery();
if( rset.first()){
int num = rset.getInt("num");
boolean valid = rset.getBoolean("aval");
}
//...
回答by Prathap
U need to iterate the result set. U just can't as
rset.next();
The code is
你需要迭代结果集。你只是不能,因为
rset.next();
代码是
ResultSet rset = vld.executeQuery();
System.out.println("test2");
while(rset.next()) {
int num = rset.getInt("num");
boolean valid = rset.getBoolean("aval");
System.out.println(num);
System.out.println(valid);
}
vld.close();