Obtain the primary key instance automatically generated by the database:

Source: Internet
Author: User

Obtain the primary key instance automatically generated by the database

Public voidInsert (){

Connection con =Null;

Preparedstatement ST =Null;

Resultset rs =Null;

// Obtain the connection

Try{

Con = dbmanager.Getconnection();

String SQL = "insert into users (name, password) values (?,?) ";

St = con. preparestatement (SQL); // pre-compiledSQLStatement

St. setstring (1, "Tom ");

St. setstring (2, "1234 ");

St.exe cuteupdate ();

Rs = ST. getgeneratedkeys (); // The returned result set is a single row, Single Column

If(Rs. Next ()){

System.Out. Println (Rs. getint (1); // output id value

}

}Catch(Sqlexception e ){

//TodoAuto-generated Catch Block

E. printstacktrace ();

}

// Resource release

Finally{

Dbmanager.Release(Con, St, RS );

}

}

Additional: Connection of dbmanager. Java to the database

Public classDbmanager {

Private StaticStringUsername;

Private StaticStringPassword;

Private StaticStringURL;

Private StaticStringDriver;

 

Static{

Try{

Inputstream in = dbmanager.Class. Getclassloader (). getresourceasstream ("config/DBCP. properties ");

Properties prop =NewProperties ();

Prop. Load (in );

Driver= Prop. getproperty ("driverclassname ");

URL= Prop. getproperty ("url ");

Username= Prop. getproperty ("username ");

Password= Prop. getproperty ("password ");

Class.Forname(Driver);

}Catch(Exception e ){

Throw newExceptionininitializererror (E );

}

}

Public staticConnection getconnection ()
Throws
Sqlexception {

ReturnDrivermanager.Getconnection(URL,
Username,Password);

}

Public static voidRelease (connection Conn, statement St, resultset RS ){

If(RS! =Null){

Try{

Rs. Close ();

}Catch(Exception e) {e. printstacktrace ();}

Rs =Null;

}

If(St! =Null){

Try{

St. Close ();

}Catch(Exception e) {e. printstacktrace ();}

St =Null;

}

If(Conn! =Null){

Try{

Conn. Close ();

}Catch(Exception e) {e. printstacktrace ();}

Conn =Null;

}

}

}

 

Obtain the primary key instance automatically generated by the database:

Public voidInsert (){

Connection con =Null;

Preparedstatement ST =Null;

Resultset rs =Null;

// Obtain the connection

Try{

Con = dbmanager.Getconnection();

String SQL = "insert into users (name, password) values (?,?) ";

St = con. preparestatement (SQL); // pre-compiledSQLStatement

St. setstring (1, "Tom ");

St. setstring (2, "1234 ");

St.exe cuteupdate ();

Rs = ST. getgeneratedkeys (); // The returned result set is a single row, Single Column

If(Rs. Next ()){

System.Out. Println (Rs. getint (1); // output id value

}

}Catch(Sqlexception e ){

//TodoAuto-generated Catch Block

E. printstacktrace ();

}

// Resource release

Finally{

Dbmanager.Release(Con, St, RS );

}

}

Additional: Connection of dbmanager. Java to the database

Public classDbmanager {

Private StaticStringUsername;

Private StaticStringPassword;

Private StaticStringURL;

Private StaticStringDriver;

 

Static{

Try{

Inputstream in = dbmanager.Class. Getclassloader (). getresourceasstream ("config/DBCP. properties ");

Properties prop =NewProperties ();

Prop. Load (in );

Driver= Prop. getproperty ("driverclassname ");

URL= Prop. getproperty ("url ");

Username= Prop. getproperty ("username ");

Password= Prop. getproperty ("password ");

Class.Forname(Driver);

}Catch(Exception e ){

Throw newExceptionininitializererror (E );

}

}

Public staticConnection getconnection ()
Throws
Sqlexception {

ReturnDrivermanager.Getconnection(URL,
Username,Password);

}

Public static voidRelease (connection Conn, statement St, resultset RS ){

If(RS! =Null){

Try{

Rs. Close ();

}Catch(Exception e) {e. printstacktrace ();}

Rs =Null;

}

If(St! =Null){

Try{

St. Close ();

}Catch(Exception e) {e. printstacktrace ();}

St =Null;

}

If(Conn! =Null){

Try{

Conn. Close ();

}Catch(Exception e) {e. printstacktrace ();}

Conn =Null;

}

}

}

 

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.