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}
}