@Test public void test3(){ Connection conn = null; PreparedStatement preparedStatement = null; ResultSet resultSet = null; String sql = "insert into student(name,password) values(?,?)"; try { conn = Methods.getConnection(); //使用重载的prepareStatement(sql,flag)来生成prepareStatement对象 preparedStatement = conn.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS); preparedStatement.setString(1,"yang5"); preparedStatement.setString(2,"1235"); preparedStatement.executeUpdate(); //通过getGenerateKeys获取包含了新生成主键的ResultSet对象 //在ResultSet中只有一列GENERATED_KEY,用于存放新生成的主键值。 resultSet = preparedStatement.getGeneratedKeys(); if(resultSet.next()){ System.out.println(resultSet.getObject(1)); } ResultSetMetaData rsmd = resultSet.getMetaData(); for (int i = 0; i < rsmd.getColumnCount(); i++) { System.out.println(rsmd.getCatalogName(i+1)); } } catch (Exception e) { e.printStackTrace(); } finally { Methods.release(preparedStatement,conn,resultSet); }