2015-12-16 34 views
0

爲什麼我會得到我的查詢SQLException的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這種方法:調用getInt

ResultSet a = stmt.executeQuery(); 

if (a.next()) { 
    return a.getInt(1); 
} else { 
    throw new IllegalArgumentException("Nothing found for 1..."); 
} 
+0

異常的全堆棧跟蹤? –

回答

0

呼叫next部分)最初的光標位於第一行之前。所以你需要撥打next。添加像

if (a.next()) { 
    return a.getInt(1); 
} 
return -1; 
+0

我試過了,它給我這個: org.apache.jasper.JasperException:java.lang.ClassCastException:com.sun.enterprise.naming.impl.JavaURLContext不能轉換爲javax.sql.DataSource – pokatore

+0

我懷疑是發生在代碼的不同部分。用詳細信息更新您的問題... – Reimeus

0

ResultSet的Javadoc說,(前

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