JDBC obtains the primary key (JDBC, primary key) generated by the database)

Source: Internet
Author: User
Document directory
  • Code for obtaining the primary key generatedkey. Java

JDBC obtains strong primary keys generated by the database
Http://blog.csdn.net/microtong

 

In actual development, the primary keys of tables in the database are often generated by the database. When the insert statement inserts data, fields other than the primary key are inserted. In many cases, after the insert statement is submitted to the database engine for execution, the program needs to obtain the generated primary key to query the inserted records based on the primary key. JDBC provides the second parameter when calling the executeupdate () method of the statement object.Statement. return_generated_keys
To indicate that you want the database engine to return the generated primary key. The generated primary key is returned in the form of a result set.Getgeneratedkeys ()
Method to obtain a result set. Traverse the result set to obtain the primary key generated by the database.
Take the mysql5.0 database as an example to create the member table "member". The field ID is the automatic incremental integer generated by the data. Create a member table using the following SQL statement:
/* Create the mysql5.0 table member */
Create Table member (
Id int not nullAuto_increment
,/* Primary Key automatically generated by the database */
Name varchar (100),/* member name */
Email varchar (255),/* Email */
Primary Key (ID)
) Type = InnoDB default Character Set GBK;

Code for obtaining the primary key generatedkey. Java

Package CN. oakcms;
Import java. SQL .*;
Public class generatedkey {
Public static void main (string [] ARGs ){
Connection conn = NULL; // connection object
Statement stmt = NULL; // statement object
Resultset rs = NULL; // result set
Try {
// Load the MySQL driver
Class. forname ("com. MySQL. JDBC. Driver ");
// Connection string
String url = "JDBC: mysql: // 127.0.0.1: 3306/mydb? Useunicode = true & characterencoding = GBK ";
// Establish a database connection
Conn = drivermanager. getconnection (URL, "root ","");
// Create a statement object
Stmt = conn. createstatement ();
// Insert statement
String SQL ="Insert into member (name, email) values ('zhang san', 'zhangsan @ gmail.com ')
";
// Execute the insert statement to return the primary key generated by the database.
Int COUNT = stmt.exe cuteupdate (SQL, statement. return_generated_keys );

System. Out. println ("successfully inserted" + Count + "records! ");
// The generated primary key is returned in the form of a result set
Rs = stmt. getgeneratedkeys ();

// Traverses the result set and outputs the primary key. In fact, the result set has only one record.
While (Rs. Next ()){
Long id = Rs. getlong (1 );
System. Out. println ("the generated primary key is:" + id );
}
// Close the result set, statement, and connection.
Rs. Close (); stmt. Close (); Conn. Close ();
} Catch (exception e ){
E. printstacktrace ();
} Finally {
If (RS! = NULL) Try {Rs. Close ();} catch (exception ignore ){}
If (stmt! = NULL) Try {stmt. Close ();} catch (exception ignore ){}
If (Conn! = NULL) Try {conn. Close ();} catch (exception ignore ){}
}
}
}
The running result of the program is as follows. A new record is inserted for each running to generate a new ID.
1 record inserted successfully!
The generated primary key is: 6


Oakcms Content Management System
Http://www.oakcms.cn

 

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.