新建一个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); //装载驱动程序
con = DriverManager.getConnection(Notes.DB_URL,
"tetdmis", "tetdmis"); //获得数据库连接对象
con.setAutoCommit(false);
String str = "insert into tetdmis.imagetable values(1,2,null)";
Statement stmt = con.createStatement();
int d = stmt.executeUpdate(str);
System.out.println("插入了" + d + "条");
ResultSet rs = stmt.executeQuery("select * from imagetable");
while(rs.next())
{
System.out.println("====================");
}
con.commit();
String str1 = "insert into tetdmis.imagetable values(3,4,null)";
stmt.executeUpdate(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 {
}
}
------------------------------------------------------------------------
后台打印出的:
java.lang.NullPointerException
at cn.com.cnpc.tz.tetd.workflow.test.main(test.java:38)
插入了1条
====================
分析:异常出现在
if (dd.equals(""))
{
System.out.println("11111111111111111111111111");
}
一旦这里出现异常,它下面的con.commit()就不会执行,然后,最外面的catch就会con.rollback()
这样第二次插入的东西就没有插入进去
-----------------------------------------------------------------------------------------------------
怎样理解catch与exception
method()
{
try{
try{JJ1();}catch(Exception e1){throw e1;//抛向最外面}
try{JJ2();}catch(Exception e2){throw e2;//抛向最外面}
try{JJ3();}catch(Exception e3){throw e3;//抛向最外面}
}catch(Exception e)
{ e.print(); thow e; //将会交给Exception }
}