SQLException:游标状态无效

时间:2015-12-16 22:07:58

标签: java sql java-ee glassfish derby

为什么我的查询会收到SQLException

PreparedStatement stmt = con.prepareStatement("SELECT note FROM NOTES4EME WHERE id=?");
stmt.setString(1,"1");
ResultSet a = stmt.executeQuery();
return a.getInt(1);

我错过了什么?

我有一个方法:

public int afficheNote(int id) throws ClassNotFoundException, SQLException {
        Class.forName("org.apache.derby.jdbc.ClientDriver");

        String url = "jdbc:derby://localhost:1527/TLdb;create=true;user=root;password=root";

        Connection con = DriverManager.getConnection(url);

        String query = "SELECT NOTES FROM NOTES4EME WHERE ID = '" + id + "'";

        //PreparedStatement stmt = con.prepareStatement(query);


        PreparedStatement stmt = con.prepareStatement("SELECT note FROM NOTES4EME WHERE id=?");
        stmt.setString(1,"1");
        ResultSet a = stmt.executeQuery();
        if (a.next()) {
        return a.getInt(1);
        }
        return -1;

    }

我在.JSP中调用此方法:

<% 
  int a = mybean.afficheNote(1);
%>

2 个答案:

答案 0 :(得分:0)

在调用typedef typename decltype(f(std::declval<T>()))::value_type R;

之前调用next
getInt

答案 1 :(得分:0)

ResultSet Javadoc说(部分)最初光标位于第一行之前。所以你需要调用next。添加类似

的内容
if (a.next()) {
    return a.getInt(1);
}
return -1;