2012-12-02 38 views
0

任何人都可以看到我如何錯過這個ResultSet嗎?我發現了以下錯誤,從customerID = rs.getInt(1)使用getGeneratedKeys()Java

public boolean addCustomer(Customer customer) { 
    String customerSQL = "INSERT INTO Customer (first_name, last_name)" 
      + "VALUES (?, ?)"; 
    String emailSQL = "INSERT INTO Email (customer_ID, email_address, primary_email)" 
      + "VALUES (?,?,?)"; 
    int customerID = 0; 

    try (Connection connection = getConnection(); 
      PreparedStatement customerPs = connection.prepareStatement(
        customerSQL, new String[] {"CUSTOMER_ID"}); 
      //PreparedStatement idPs = connection.prepareStatement(idSQL); 
      //PreparedStatement emailPs = connection.prepareStatement(emailSQL) 
      ) { 

     customerPs.setString(1, customer.getFirstName()); 
     customerPs.setString(2, customer.getLastName()); 
     customerPs.executeUpdate(); 
     ResultSet rs = customerPs.getGeneratedKeys(); 

     System.out.println("Result Set: " + rs.toString()); 
     customerID = rs.getInt(1); 
     System.out.print("Customer ID: " + customerID); 



    } 
    catch(SQLException e) 
    { 
     System.err.println("Error: " + e); 
     e.printStackTrace(System.out); 
     return false; 
    } 

    return false; 
} 

輸出

run: 
Bill Joel 
[email protected] 
[email protected] 
[email protected] 

Result Set: [email protected] 
Error: java.sql.SQLException: Invalid cursor state - no current row. 
java.sql.SQLException: Invalid cursor state - no current row. 
     at org.apache.derby.impl.jdbc.SQLExceptionFactory40.getSQLException(Unknown Source) 
     at org.apache.derby.impl.jdbc.Util.newEmbedSQLException(Unknown Source) 
     at org.apache.derby.impl.jdbc.Util.newEmbedSQLException(Unknown Source) 
     at org.apache.derby.impl.jdbc.Util.generateCsSQLException(Unknown Source) 
     at org.apache.derby.impl.jdbc.EmbedConnection.newSQLException(Unknown Source) 
     at org.apache.derby.impl.jdbc.ConnectionChild.newSQLException(Unknown Source) 
     at org.apache.derby.impl.jdbc.EmbedResultSet.checkOnRow(Unknown Source) 
     at org.apache.derby.impl.jdbc.EmbedResultSet.getColumn(Unknown Source) 
     at org.apache.derby.impl.jdbc.EmbedResultSet.getInt(Unknown Source) 
     at customeremailmanager.CustomerDB.addCustomer(CustomerDB.java:78) 
     at customeremailmanager.CustomerEmailManager.main(CustomerEmailManager.java:24) 
Caused by: java.sql.SQLException: Invalid cursor state - no current row. 
     at org.apache.derby.impl.jdbc.SQLExceptionFactory.getSQLException(Unknown Source) 
     at org.apache.derby.impl.jdbc.SQLExceptionFactory40.wrapArgsForTransportAcrossDRDA(Unknown Source) 
     ... 11 more 
BUILD SUCCESSFUL (total time: 1 second) 
+0

它始終是更好的問題在這裏發表代碼,而不是鏈接到的外部網站。 – kosa

回答

2

你必須從你的ResultSet對象retriving行之前調用ResultSet.next()

ResultSet rs = customerPs.getGeneratedKeys(); 
      while(rs.next()){ 
      System.out.println("Result Set: " + rs.toString()); 
      customerID = rs.getInt(1); 
} 
0

您需要先確保行可用。關於如何使用ResultsSet檢索數據,請參閱此tutorial

按照Doc:

您通過遊標訪問一個結果對象中的數據。請注意,此遊標不是數據庫遊標。該遊標是指向ResultSet中的一行數據的指針。 最初,光標位於第一行之前。方法ResultSet.next將光標移動到下一行。如果光標位於最後一行之後,則此方法返回false。這種方法使用while循環反覆調用ResultSet.next方法通過所有的數據在結果迭代

例子:

while (rs.next()) { 
     customerID = rs.getInt(1); 
    }