The PreparedStatement in JDBC is used to obtain the automatically generated primary key value of the database,

Source: Internet
Author: User

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 inPreparedStatementObject. 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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.