Get last inserted auto increment id in mysql

前端 未结 6 833
你的背包
你的背包 2020-12-09 10:10

I am trying to get the mysql command like mysql_insert_id(); which retrieve the last inserted row\'s auto_increment id. What can I do to get it in Java?

rs =         


        
6条回答
  •  执念已碎
    2020-12-09 10:54

    Using JDBC, you can use Connection.PreparedStatement(query, int) method.

    PreparedStatement pstmt = conn.prepareStatement(Query, Statement.RETURN_GENERATED_KEYS);  
    pstmt.executeUpdate();  
    ResultSet keys = pstmt.getGeneratedKeys();    
    keys.next();  
    key = keys.getInt(1);
    

提交回复
热议问题