10bet网址
连接器和api手册
下载本手册

3.6.4检索AUTO_INCREMENT通过JDBC的列值

getGeneratedKeys ()如果需要检索,首选的方法是什么AUTO_INCREMENT键和通过JDBC;下面的第一个例子说明了这一点。第二个示例展示了如何使用标准检索相同的值选择LAST_INSERT_ID ()查询最后一个示例展示了可更新的结果集如何检索AUTO_INCREMENT值时,使用insertRow ()方法。

3.11 Connector/J:正在检索AUTO_INCREMENT列值使用Statement.getGeneratedKeys ()

语句stmt = null;ResultSet rs = null;try{// //创建一个Statement实例,我们可以使用// '正常'结果集假设你有一个连接'conn'到MySQL数据库已经//可用stmt = conn. createstatement ();// //为本例// stmt的表发出DDL查询。executeUpdate("DROP TABLE IF EXISTS autoIncTutorial");支撑。executeUpdate("CREATE TABLE autoIncTutorial (" + "priKey INT NOT NULL AUTO_INCREMENT, " + "dataField VARCHAR(64), PRIMARY KEY (priKey) ");// //插入一行,将在'priKey'字段// stmt中生成一个AUTO INCREMENT //键。executeUpdate("INSERT INTO autoIncTutorial (dataffield) ")" + "values('我能得到自动递增字段吗?')",Statement.RETURN_GENERATED_KEYS);// //示例:使用Statement.getGeneratedKeys() //检索一个自动递增的值// value // int autoIncKeyFromApi = -1;rs = stmt.getGeneratedKeys(); if (rs.next()) { autoIncKeyFromApi = rs.getInt(1); } else { // throw an exception from here } System.out.println("Key returned from getGeneratedKeys():" + autoIncKeyFromApi); } finally { if (rs != null) { try { rs.close(); } catch (SQLException ex) { // ignore } } if (stmt != null) { try { stmt.close(); } catch (SQLException ex) { // ignore } } }

3.12 Connector/J:正在检索AUTO_INCREMENT列值使用选择LAST_INSERT_ID ()

语句stmt = null;ResultSet rs = null;try{// //创建一个Statement实例,用于// 'normal'结果集。stmt = conn.createStatement();// //为本例// stmt的表发出DDL查询。executeUpdate("DROP TABLE IF EXISTS autoIncTutorial");支撑。executeUpdate("CREATE TABLE autoIncTutorial (" + "priKey INT NOT NULL AUTO_INCREMENT, " + "dataField VARCHAR(64), PRIMARY KEY (priKey) ");// //插入一行,将在'priKey'字段// stmt中生成一个AUTO INCREMENT //键。executeUpdate("INSERT INTO autoIncTutorial (dataffield) ")" + "values('我能得到自动递增字段吗?')");// //使用MySQL LAST_INSERT_ID() //函数来做与getGeneratedKeys() // int autoIncKeyFromFunc = -1相同的事情; rs = stmt.executeQuery("SELECT LAST_INSERT_ID()"); if (rs.next()) { autoIncKeyFromFunc = rs.getInt(1); } else { // throw an exception from here } System.out.println("Key returned from " + "'SELECT LAST_INSERT_ID()': " + autoIncKeyFromFunc); } finally { if (rs != null) { try { rs.close(); } catch (SQLException ex) { // ignore } } if (stmt != null) { try { stmt.close(); } catch (SQLException ex) { // ignore } } }

3.13 Connector/J:正在检索AUTO_INCREMENT中的列值可更新的结果集

语句stmt = null;ResultSet rs = null;try{// //创建一个Statement实例,我们可以使用// '正常'的结果集,以及'可更新'的//一个,假设你有一个连接'conn'到//一个MySQL数据库已经可用// stmt = conn. createstatement (java.sql.ResultSet. com)TYPE_FORWARD_ONLY java.sql.ResultSet.CONCUR_UPDATABLE);// //为本例// stmt的表发出DDL查询。executeUpdate("DROP TABLE IF EXISTS autoIncTutorial");支撑。executeUpdate("CREATE TABLE autoIncTutorial (" + "priKey INT NOT NULL AUTO_INCREMENT, " + "dataField VARCHAR(64), PRIMARY KEY (priKey) ");// //从可更新的结果集// rs = stmt检索AUTO INCREMENT键//的示例。executeQuery("SELECT priKey, dataffield " + "FROM autoIncTutorial");rs.moveToInsertRow ();rs.updateString("dataField", "AUTO INCREMENT here?");rs.insertRow (); // // the driver adds rows at the end // rs.last(); // // We should now be on the row we just inserted // int autoIncKeyFromRS = rs.getInt("priKey"); System.out.println("Key returned for inserted row: " + autoIncKeyFromRS); } finally { if (rs != null) { try { rs.close(); } catch (SQLException ex) { // ignore } } if (stmt != null) { try { stmt.close(); } catch (SQLException ex) { // ignore } } }

运行前面的示例代码应该产生以下输出:

getGeneratedKeys()返回的键:1 SELECT LAST_INSERT_ID()返回的键:1插入的行返回的键:1

有时,使用选择LAST_INSERT_ID ()查询,因为该函数的值的作用域是连接。因此,如果在同一连接上发生其他查询,则该值将被覆盖。另一方面,getGeneratedKeys ()方法的作用域声明实例,因此即使在同一连接上发生其他查询,也可以使用它声明实例。