Connection con=null; PreparedStatement ps=null; ResultSet rs=null; try { //建立连接 con= JDBCUtils.getConnection(); //创建语句 String sql="insert into t_user values('test','123',null,null)"; ps = con.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS); ps.executeUpdate(); rs= ps.getGeneratedKeys();//获取主键集合 if (rs.next()) { System.out.println(rs.getInt(1));//获取主键 }