java - 使用 getGenerateKeys() Java

标签 java jdbc

任何人都可以看到我是如何错过这个结果集的吗?我从 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"}); )  {
       
        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)

最佳答案

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

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

关于java - 使用 getGenerateKeys() Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13674823/

相关文章:

java - 如何在c#中复制java加密

Java - 读取、操作和编写 WAV 文件

java - 创建并有机会从字符串池中创建 String 对象有什么意义

java - MySQL "No operations allowed after statement closed"

java - Java : How to Differ Inserted/Updated/NoChange states 上的 MySQL "INSERT ... ON DUPLICATE KEY UPDATE"

java - com.mysql.jdbc.exceptions.jdbc4。第一个 java 项目 - 感觉很愚蠢

java - JDBC SQL客户端工具推荐

java - 我应该如何包装来自未知来源的对象

java - 是否可以使用 Project Reactor 等待事件而不阻塞线程?

java - JDBC SQL SELECT 查询总和方法行为不正确