使用getGeneratedKeys()Java

时间:2012-12-02 22:52:22

标签: java jdbc

有谁能看到我如何错过这个ResultSet?我从customerID = rs.getInt(1)获得了以下错误:

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
this@that.com
those@these.com
wank@that.com

Result Set: org.apache.derby.impl.jdbc.EmbedResultSet40@61c70928
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)

2 个答案:

答案 0 :(得分:2)

在从ResultSet对象中检索行之前,必须调用ResultSet.next()

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

答案 1 :(得分:0)

您需要先确保行可用。有关如何使用ResultsSet检索数据,请参阅此tutorial

根据文件:

  

您可以通过游标访问ResultSet对象中的数据。请注意,此游标不是数据库游标。此游标是指向ResultSet中一行数据的指针。 最初,光标位于第一行之前。 ResultSet.next方法将光标移动到下一行。如果光标位于最后一行之后,则此方法返回false。此方法使用while循环重复调用ResultSet.next方法以遍历ResultSet中的所有数据

示例:

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