【问题标题】:mysql, java - Get last inserted auto increment id from one table and insert it again on another tablemysql, java - 从一个表中获取最后插入的自动增量 id 并将其再次插入到另一个表中
【发布时间】:2014-03-14 05:32:07
【问题描述】:

Get last inserted auto increment id in mysql 重复的问题

我正在创建一个在M_GROUPS 上插入的组。

M_GROUPS表:

GROUP_ID INT AUTO_INCREMENT, 
GROUP_CREATOR_ID INT

来自会话。

我需要将GROUP_IDGROUP_CREATOR_ID 插入到
M_GROUP_MEMBERS 表中

GROUP_ID INT,
MEMBER_ID INT.

我的问题是我不能从M_GROUPS 获取自动增量值GROUP_ID

public void groupCreation(String groupname, int grouptype, int creator_id) {

    DatabaseService oDatabaseService = new DatabaseService();
    Connection connection = oDatabaseService.connect();
    try {
        Statement stmt = null;
        stmt = connection.createStatement();
        String sql;

        sql = "INSERT INTO M_GROUPS( GROUP_NAME, GROUP_CREATOR_ID,
                                     GROUP_TYPE, CREATION_TIME)"
            + " VALUES ('"
            + groupname
            + "','"
            + creator_id
            + "','"
            + grouptype + "',NOW())";

        //stmt.executeUpdate(sql);
        stmt.executeUpdate(sql);
    } catch (SQLException se) {
        se.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if (connection != null)
                connection.close();
        } catch (SQLException se) {
            se.printStackTrace();
        }
    }
}

【问题讨论】:

    标签: java mysql


    【解决方案1】:

    使用Statement 对象中的getGeneratedKeys() 方法来识别新的自动生成值。迭代返回的ResultSet对象,按照批处理语句的顺序获取新生成的键值。

    变化:

    stmt.executeUpdate(sql);
    

    收件人:

    int rowsAffected = 
      stmt.executeUpdate( sql, Statement.RETURN_GENERATED_KEYS );  
    ResultSet rs = stmt.getGeneratedKeys();  
    
    //******************************************************  
    // in case of batch insert, you can check how many are inserted
    rs.last();  
    int rows = rs.getRow();  
    System.out.println( "Generated keys count: " + rows );  
    //******************************************************/  
    
    int currentRow = 1;  
    rs.beforeFirst();  
    while( rs.next() ) {  
        System.out.println( /**/( currentRow++ ) + " = " + /**/rs.getInt( 1 ) );  
    } // while rs  
    

    一旦您在变量中拥有这个自动生成的键值,您就可以将它与其他 SQL 语句一起使用,以存储在其他表中。

    注意:如果您使用的 JDBC 驱动程序不支持此方法,则调用 getGeneratedKeys() 可能会抛出 java.sql.SQLFeatureNotSupportedException

    【讨论】:

    • 不工作意味着什么? rs.getInt( 1 ) 为您提供刚刚从您的插入语句生成的自动递增主键值的值。
    • 它给出了 java.sql.SQLException:列数与 com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1078) 处 com.mysql.jdbc 的第 1 行的值计数不匹配.MysqlIO.checkErrorPacket(MysqlIO.java:4190) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4122)
    • 它必须在您的任何其他声明中,例如insert。检查是否为要插入的列传递了相同数量的值。也给我看一下声明。
    • @Ravinder 先生,我这样做可以请您查看以下链接stackoverflow.com/questions/22423194/…
    猜你喜欢
    • 2016-04-17
    • 1970-01-01
    • 1970-01-01
    • 2021-08-25
    • 2017-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-30
    相关资源
    最近更新 更多