The PreparedStatement in JDBC is used to obtain the automatically generated primary key value of the database,
PreparedStatement Interface
public interface PreparedStatement
Extends Statement
Indicates the objects of pre-compiled SQL statements.
SQL statements are pre-compiled and stored inPreparedStatement
Object. This object can then be used to efficiently execute the statement multiple times. (Jdk api 1.6)
This interface can pre-compile SQL statements to prevent SQL Injection problems. When you execute the same SQL statement multiple times, because of pre-compilation, you do not need to re-compile the statement during execution, therefore, it is more efficient than Statement.
(Statement is compiled and executed every time an SQL Statement is executed)
------------------------------------------------------------------------------
// Insert statement. The second parameter returns an int type primary key value PreparedStatement pstmt = conn. prepareStatement ("insert into tb_user values (null ,?,?) ", Statement. RETURN_GENERATED_KEYS );
// After the statement is executed, a result set ResultSet with the primary key value is returned.
ResultSet rs = ps. getGeneratedKeys ();
There are similar methods in Statement, which can be used in execute (String SQL, int autoGeneratedKeys) and other methods.
For example:
Import java. SQL. connection; import java. SQL. preparedStatement; import java. SQL. resultSet; import java. SQL. statement;/*** obtain the automatically generated primary key value of the database * @ author dingshuangen **/public class getKey {public static void main (String [] args) {Connection conn = DbUtil. getConnection (); PreparedStatement ps = null; PreparedStatement ps_h = null; ResultSet rs = null; try {// insert statement, the second parameter returns an int type primary key value ps = conn. prepareStatement ("insert I Nto tb_user values (null ,?,?) ", Statement. RETURN_GENERATED_KEYS); ps. setString (1, ""); ps. setString (2, "gourd"); // execute the sentence ps.exe cute (); // obtain the returned primary key value. The returned type is ResultSet result set rs = ps. getGeneratedKeys (); rs. next (); // obtain the primary key value int key = rs. getInt (1); System. out. println ("++ primary key ++" + key + "++" ++ "); ps_h = conn. prepareStatement ("insert into tb_hober values (null ,?,?) "); // Insert multiple data records cyclically. String [] holobby = {" dinner "," sleeping "," doubebean "}; for (String h: holobby) {ps_h.setInt (1, key); ps_h.setString (2, hsf-+ps_h.exe cute ();} System. out. println ("executed successfully");} catch (Exception e) {e. printStackTrace ();} finally {// disable all resources DbUtil. close (rs); DbUtil. close (ps); DbUtil. close (ps_h); DbUtil. close (conn );}}}
The execution result is as follows:
++ Primary key ++ 7 ++ successfully executed
Corresponding database table data:
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.