重拾JDBC(八)取得数据库自动生成的主键

xiaoxiao2021-02-28  13

取得数据库自动生成的主键

@Test public void testGetKeyValue() { Connection connection = null; PreparedStatement preparedStatement = null; try { connection = JDBCTools.getConnection(); String sql = "INSERT INTO customers(name, email, birth)" + "VALUES(?,?,?)"; // preparedStatement = connection.prepareStatement(sql); //使用重载的 prepareStatement(sql, flag) //来生成 PreparedStatement 对象 preparedStatement = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS); preparedStatement.setString(1, "ABCDE"); preparedStatement.setString(2, "abcde@atguigu.com"); preparedStatement.setDate(3, new Date(new java.util.Date().getTime())); preparedStatement.executeUpdate(); //通过 getGeneratedKeys() 获取包含了新生成的主键的 ResultSet 对象 //在 ResultSet 中只有一列 GENERATED_KEY, 用于存放新生成的主键值. ResultSet rs = preparedStatement.getGeneratedKeys(); if(rs.next()){ System.out.println(rs.getObject(1)); } ResultSetMetaData rsmd = rs.getMetaData(); for(int i = 0; i < rsmd.getColumnCount(); i++){ System.out.println(rsmd.getColumnName(i + 1)); } } catch (Exception e) { e.printStackTrace(); } finally{ JDBCTools.releaseDB(null, preparedStatement, connection); } }
转载请注明原文地址: https://www.6miu.com/read-1999992.html

最新回复(0)