我有以下代码,并且观察到如下SQL异常:
java.sql.SQLException:未调用ResultSet.next
代码:
String query = "select account_number,flag,flag2,flag3,flag4,flag5,flag6,flag7,amount from transaction_p2 where account_number=?";
PreparedStatement ps = connection.prepareStatement(query);
ps.setString(1,"2195281819521610731");
boolean rs = ps.execute();
if(rs)
{
ResultSet set=ps.getResultSet();
System.out.pringln(set+"\t resultset");
System.out.println(set.getInt("amount"));
}
else
{
System.out.println("FALSE");
}
输出:
Connecting database...
Database connected!
oracle.jdbc.driver.ForwardOnlyResultSet@55dbe543 resultset
java.sql.SQLException: ResultSet.next was not called
at oracle.jdbc.driver.GeneratedScrollableResultSet.getInt(GeneratedScrollableResultSet.java:565)
at oracle.jdbc.driver.GeneratedResultSet.getInt(GeneratedResultSet.java:1350)
at com.online.paypal.getSampleConnection.SampleTestConnection.main(SampleTestConnection.java:48)
我只是获得要在system.out.println(set);
中打印的结果集值,但是当我尝试获取该值时,它将引发SQL异常。我不确定如何从结果集中获取值。
请让我知道如何从结果集中获取值以及如何解决此问题。
如果我使用while(set.next())
,则在输出控制台中看不到任何内容。然后返回false。