Experience con. Commit () in Oracle ()

Source: Internet
Author: User

Create a class
Package cn.com. CNPC. tz. tetd. workflow;
Import java. SQL .*;
Import cn.com. CNPC. tz. tetd. Common. notes;
Import java. SQL. connection;
Public class test
{
Public test ()
{
Try {
Jbinit ();
} Catch (exception ex ){
Ex. printstacktrace ();
}

}
Public static void main (string ARGs []) {

Connection con = NULL;
Try {
Class. forname (notes. driver_name); // load the driver
Con = drivermanager. getconnection (notes. db_url,
"Tetdmis", "tetdmis"); // obtain the database connection object
Con. setautocommit (false );
String STR = "insert into tetdmis. imagetable values (1, 2, null )";
Statement stmt = con. createstatement ();
Int d = stmt.exe cuteupdate (STR );
System. Out. println ("inserted" + D + ");
Resultset rs = stmt.exe cutequery ("select * From imagetable ");
While (Rs. Next ())
{
System. Out. println ("===================== ");
}
Con. Commit ();

String str1 = "insert into tetdmis. imagetable values (3,4, null )";
Stmt.exe cuteupdate (str1 );
String dd = NULL;
If (DD. Equals (""))
{
System. Out. println ("11111111111111111111111111 ");
}
Con. Commit ();

} Catch (exception e ){
E. printstacktrace ();
Try
{
Con. rollback ();
} Catch (exception ex)
{
Ex. printstacktrace ();
}
}
}

Private void jbinit () throws exception {
}
}

------------------------------------------------------------------------
Printed in the background:
Java. Lang. nullpointerexception

At cn.com. CNPC. tz. tetd. workflow. Test. Main (test. Java: 38)

Insert 1 entry

================================

Analysis: Exceptions occur in
If (DD. Equals (""))
{
System. Out. println ("11111111111111111111111111 ");
}
Once an exception occurs, the con. Commit () below it will not be executed, and then the outermost catch will con. rollback ()
In this way, the second insert is not inserted.
Bytes -----------------------------------------------------------------------------------------------------
How to Understand catch and exception
Method ()
{

Try {
Try {J1 ();} catch (exception E1) {Throw E1; // throwing to the outermost surface}
Try {jj2 ();} catch (exception E2) {Throw E2; // throwing to the outermost surface}
Try {J3 ();} catch (exception E3) {Throw E3; // throwing to the outermost surface}
} Catch (exception E)
{E. Print (); thow E; // will be handed to exception}

}

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.