1

我的数据库上有一个表,我称之为发件人表,当我想打印字段的内容时,它给了我这个错误

java-sql-sqlexception [microsoft][odbc-driver-manager] invalid cursor state

无论如何这是我的代码

        str = "SELECT * FROM Sender WHERE sender_PID= " + sender_PID + "";            
        try {
            rs = stmt.executeQuery (str);  
            System.out.println (rs.getInt ("sender_PID"));    

        } catch (Exception ex) {ex.printStackTrace ( );}

我真的很抱歉我的英语不好

database ( )
{
    try {

        Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver");
        conn = DriverManager.getConnection ("jdbc:odbc:DB");
        stmt = conn.createStatement (ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);

    } catch (Exception ex) {ex.printStackTrace();}
}

String checkSender (int sender_PID)
    {
        str = "SELECT sender_PID FROM Sender WHERE sender_PID= " + sender_PID + "";   

        try {           
            rs = stmt.executeQuery (str);                      
            while(rs.next())  
            {
                System.out.println (rs.getInt ("sender_PID"));
            }  

        } catch (Exception ex) {ex.printStackTrace ( );}     


        return "1";
    }
4

2 回答 2

3

您需要先将光标移动到第一条记录rs.next()

注意:您应该PreparedStatement用于此目的

于 2013-04-24T22:24:12.557 回答
0

您的 rs 可能是一个迭代器(一个数据集),因此您可能需要测试以下代码:

while(rs.next())  {
    System.out.println (rs.getInt ("sender_PID"));    
}
于 2013-04-24T22:27:18.097 回答