我的数据库上有一个表我称之为Sender表,当我想打印字段的内容时,它给了我这个错误
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";
}
答案 0 :(得分:3)
您需要先通过rs.next()
注意:您应该PreparedStatement
用于此目的
答案 1 :(得分:0)
你的rs可能是一个迭代器(数据集),所以你可能需要测试以下代码:
while(rs.next()) {
System.out.println (rs.getInt ("sender_PID"));
}