如果将记录插入到包含自动递增列的表中,请使用Statement或PreparedStatement对象。
您可以使用getGeneratedKeys()方法检索由该对象生成的特定列的值。
让我们使用CREATE语句在MySQL数据库中创建一个名称为sales 的表,其中一列自动递增,如下所示-
CREATE TABLE Sales( ID INT PRIMARY KEY AUTO_INCREMENT, ProductName VARCHAR (20), CustomerName VARCHAR (20), DispatchDate date, DeliveryTime time, Price INT, Location VARCHAR(20) );
以下JDBC程序使用PreparedStatement将3条记录插入Sales表(上面创建的)中,检索并显示由其生成的自动递增的值。
import java.sql.Connection; import java.sql.Date; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.sql.Time; public class RetrievingData_AutoIncrement_Pstmt { public static void main(String args[]) throws SQLException { //注册驱动程序 DriverManager.registerDriver(new com.mysql.jdbc.Driver()); //获得连接 String mysqlUrl = "jdbc:mysql://localhost/sample_database"; Connection con = DriverManager.getConnection(mysqlUrl, "root", "password"); System.out.println("Connection established......"); //查询以将值插入销售表 String insertQuery = "INSERT INTO Sales (ProductName, CustomerName, DispatchDate, DeliveryTime, Price, Location) VALUES (?, ?, ?, ?, ?, ?)"; //创建一个PreparedStatement对象 PreparedStatement pstmt = con.prepareStatement(insertQuery,Statement.RETURN_GENERATED_KEYS); pstmt.setString(1, "Key-Board"); pstmt.setString(2, "Raja"); pstmt.setDate(3, new Date(1567315800000L)); pstmt.setTime(4, new Time(1567315800000L)); pstmt.setInt(5, 7000); pstmt.setString(6, "Hyderabad"); pstmt.addBatch(); pstmt.setString(1, "Earphones"); pstmt.setString(2, "Roja"); pstmt.setDate(3, new Date(1556688600000L)); pstmt.setTime(4, new Time(1556688600000L)); pstmt.setInt(5, 2000); pstmt.setString(6, "Vishakhapatnam"); pstmt.addBatch(); pstmt.setString(1, "Mouse"); pstmt.setString(2, "Puja"); pstmt.setDate(3, new Date(1551418199000L)); pstmt.setTime(4, new Time(1551418199000L)); pstmt.setInt(5, 3000); pstmt.setString(6, "Vijayawada"); pstmt.addBatch(); //执行批处理 pstmt.executeBatch(); //由当前PreparedStatement对象生成的自动递增的值 ResultSet res = pstmt.getGeneratedKeys(); System.out.println("Auto-incremented values of the column ID generated by the current PreparedStatement object: "); while (res.next()) { System.out.println(res.getString(1)); } } }
输出结果
Connection established...... Records inserted...... Auto-incremented values of the column ID generated by the current PreparedStatement object: 1 2 3
以下JDBC程序使用Statement将3条记录插入Sales表(上面创建的)中,检索并显示由它生成的自动递增的值。
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class RetrievingData_AutoIncrement { public static void main(String args[]) throws SQLException { //注册驱动程序 DriverManager.registerDriver(new com.mysql.jdbc.Driver()); //获得连接 String mysqlUrl = "jdbc:mysql://localhost/sample_database"; Connection con = DriverManager.getConnection(mysqlUrl, "root", "password"); System.out.println("Connection established......"); //创建Statement对象 Statement stmt = con.createStatement(); //查询插入多行 String insertQuery = "insert into sales (ProductName, CustomerName, DispatchDate, DeliveryTime, Price, Location) values" + "('Key-Board', 'Raja', DATE('2019-09-01'), TIME('11:00:00'), 7000, 'India'), " + "('Earphones', 'Roja', DATE('2019-05-01'), TIME('11:00:00'), 2000, 'Vishakhapatnam'), " + "('Mouse', 'Puja', DATE('2019-03-01'), TIME('10:59:59'), 3000, 'Vijayawada')"; //执行INSERT语句 stmt.executeUpdate(insertQuery, Statement.RETURN_GENERATED_KEYS); System.out.println("Records inserted......"); //检索自动生成(自动递增)的键 ResultSet rs = stmt.getGeneratedKeys(); System.out.println("Values of auto-generated keys: "); while(rs.next()) { System.out.println(rs.getInt(1)); } } }
输出结果
Connection established...... Records inserted...... Values of generated keys: 1 2 3