import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.Statement; import cn.itcast.utils.JdbcUtils; public class Demo4 { /** * 获取自动生成的主键 create table test1 ( id int primary key auto_increment, name varchar(20) ); */ public static void main(String[] args) { Connection conn = null; PreparedStatement st = null; ResultSet rs = null; try{ conn = JdbcUtils.getConnection(); String sql = "insert into test1(name) values(?)"; st = conn.prepareStatement(sql); st.setString(1, "aaa"); st.executeUpdate(); rs = st.getGeneratedKeys(); if(rs.next()){ System.out.println(rs.getInt(1)); } }catch (Exception e) { e.printStackTrace(); }finally{ JdbcUtils.release(conn, st, rs); } } }