如何使用JDBC检索PreparedStatement生成的自动增加的值?
在创建表时,在某些情况下,我们需要自动生成/增加列的值(例如ID)。各种数据库以不同的方式支持此功能。
在MySQL数据库中,您可以使用以下语法声明列自动递增。
CREATE TABLE table_name( ID INT PRIMARY KEY AUTO_INCREMENT, column_name1 data_type1, column_name2 data_type2, column_name3 data_type3, column_name4 data_type4, ............ ........... );
在表中插入记录时,无需在自动增加的列下插入值。这些将自动生成。
例如,在一个表中,如果有一列具有名称ID和数据类型INT,该列将自动递增,并且该表中已经有6条记录。当您使用INSERT语句插入下一条记录时,新记录的ID值为7,下一条记录的ID值为8。
(您可以为这些自动递增的列指定初始值和间隔)。
检索自动增加的值
如果将记录插入到包含自动递增列的表中,请使用PreparedStatement对象。
您可以使用getGeneratedKeys()方法检索由当前PreparedStatement对象生成的特定列的值。
示例
让我们使用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) );
现在,使用PreparedStatement 对象将记录插入到此表中,并检索由它生成的自动递增的值-
使用DriverManager类的registerDriver()方法或forName()名为Class的类的方法注册所需数据库的Driver类。
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
通过将数据库的URL,数据库中用户的用户名和密码(字符串格式)作为参数传递给DriverManager类的getConnection()方法,创建一个Connection对象。
Connection mysqlCon = DriverManager.getConnection(mysqlUrl, "root", "password");
使用连接接口的方法创建PreparedStatement对象prepareStatement()。
将此方法传递给INSERT语句,将绑定变量以字符串格式作为一个参数,将Statement.RETURN_GENERATED_KEYS作为另一个参数作为-
//查询以将值插入销售表 String insertQuery = "INSERT INTO Sales (ProductName, CustomerName, DispatchDate, DeliveryTime, Price, Location) VALUES (?, ?, ?, ?, ?, ?)"; //创建一个PreparedStatement对象 PreparedStatement pstmt = con.prepareStatement(insertQuery, Statement.RETURN_GENERATED_KEYS);
使用setXXX()方法将每个记录的值设置为绑定变量,然后将其添加到批处理中。
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(); ........... ...........
将所有记录的值添加到批处理后,使用executeBatch()方法执行批处理。
pstmt.executeBatch();
最后,使用getGeneratedKeys()方法获取此PreparedStatement对象生成的自动增量键。
ResultSet rs = pstmt.getGeneratedKeys();
while (rs.next()) {
System.out.println(rs.getString(1));
}以下JDBC程序使用PreparedStatement将5条记录插入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.setString(1, "Mobile");
pstmt.setString(2, "Vanaja");
pstmt.setDate(3, new Date(1551415252000L));
pstmt.setTime(4, new Time(1551415252000L));
pstmt.setInt(5, 9000);
pstmt.setString(6, "Chennai");
pstmt.addBatch();
pstmt.setString(1, "Headset");
pstmt.setString(2, "Jalaja");
pstmt.setDate(3, new Date(1554529139000L));
pstmt.setTime(4, new Time(1554529139000L));
pstmt.setInt(5, 6000);
pstmt.setString(6, "Goa");
pstmt.addBatch();
System.out.println("Records inserted......");
//执行批处理
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 4 5