[英]ResultSet is false in executeQuery Mysql - Java even when row exist
我正在使用Java中的prepareStatement从MySql的表中获取两列。如果我直接在mySql DB上执行查询,则查询有效并返回行。 但是对于此方法调用,ResultSet在每个ResultSet.next()调用上都具有值“ false”。 下面是代码:
private ResultSet fetchFriends(int lActProfId) throws SQLException {
// TODO Auto-generated method stub
String selectRelationQry= "SELECT friend_id, type FROM fs_relationship WHERE profile_id =?";
System.out.println("---- In method FetchAndInsertInWall()->fetchFriends - Qry: "+selectRelationQry);
pst = lCon.prepareStatement(selectRelationQry);
pst.setInt(1, lActProfId);
System.out.println("lActProfId: "+ lActProfId );
ResultSet res3 = pst.executeQuery();
System.out.println("---- temp--- In method FetchAndInsertInWall()->fetchFriends query has no Results");
if(!res3.next()){
System.out.println("---- In method FetchAndInsertInWall()->fetchFriends query has no Results");
} else {System.out.println("Elements in res3: "+res3.next() + res3.next() + res3.next());
}
pst = null;
return res3;
}
我在控制台上得到以下输出:
---- In method FetchAndInsertInWall()->fetchFriends - Qry: SELECT friend_id, type FROM fs_relationship WHERE profile_id =?
lActProfId: 5002
---- temp--- In method FetchAndInsertInWall()->fetchFriends query has no Results
Element in res31: false
----> WallInsertions Failed for Uid: 5002
java.sql.SQLException: Illegal operation on empty result set.
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1078)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:989)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:975)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:920)
at com.mysql.jdbc.ResultSetImpl.checkRowPos(ResultSetImpl.java:855)
at com.mysql.jdbc.ResultSetImpl.getInt(ResultSetImpl.java:2710)
at com.mysql.jdbc.ResultSetImpl.getInt(ResultSetImpl.java:2851)
at com.fs.javadbact.FetchDataAndInsertInWall.fetchAndInsertInWall(FetchDataAndInsertInWall.java:64)
at com.fs.javadbact.ConsumerTest.run(ConsumerTest.java:86)
at java.lang.Thread.run(Unknown Source)
Jan 12, 2014 3:34:57 AM com.fs.javadbact.ConsumerTest run
请帮助我如何解决此问题,并获取具有正确数据的正确ResultSet。
我认为您已经忘记在pst
变量之前声明PreparedStatement
写:
PreparedStatement pst = lCon.prepareStatement(selectRelationQry);
而不是 :
pst = lCon.prepareStatement(selectRelationQry);
使用getString()而不是next()来获取行的值。
如果结果少于三行,则将耗尽结果集。 不知道一次返回false后是否需要next()工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.