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 ()
ThrowsSqlexception {
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 ()
ThrowsSqlexception {
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;
}
}
}