0

我正在使用 Java 中的 PreparedStatement 从 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。

4

2 回答 2

0

我认为您忘记在变量 writePreparedStatement之前声明: 而不是: pst

PreparedStatement pst = lCon.prepareStatement(selectRelationQry);

pst = lCon.prepareStatement(selectRelationQry);
于 2014-01-11T22:51:32.503 回答
0

使用 getString() 而不是 next() 来获取行的值。

如果您的结果少于三行,您将耗尽结果集。我不知道在返回 false 一次后是否需要 next() 才能工作。

于 2014-01-11T23:25:33.530 回答