首页 >数据库 >mysql教程 >一个用JDBC处理transaction的样例程序

一个用JDBC处理transaction的样例程序

WBOY
WBOY原创
2016-06-07 17:02:43864浏览

一个用JDBC处理transaction的样例程序import java.sql.*;public class TestJDBC { public static void main(String [] args) {

一个用JDBC处理transaction的样例程序

import java.sql.*;


public class TestJDBC {
 public static void main(String [] args) {
  Connection conn = null;
  Statement stmt = null;
  try {
   Class.forName("Oracle.jdbc.driver.OracleDriver");
   conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL","scott","tiger");
   
   conn.setAutoCommit(false);
   stmt = conn.createStatement();
   stmt.addBatch("insert into dept2 values (61,'test','apple')");
   stmt.addBatch("insert into dept2 values (62,'test','apple')");
   stmt.addBatch("insert into dept2 values (63,'test','apple')");
   stmt.executeBatch();
   
   conn.commit();
   
   conn.setAutoCommit(true);
  } catch (ClassNotFoundException e) {
   e.printStackTrace();
  } catch (SQLException e) {
   
   if(conn!=null) {
    try {
     conn.rollback();
     conn.setAutoCommit(true);
    } catch (SQLException e1) {
     e1.printStackTrace();
    }
   }
   e.printStackTrace();
  }
  finally {
   if(stmt!=null) {
    try {
     stmt.close();
    } catch (SQLException e) {
     e.printStackTrace();
    }
   }
   if(conn!=null) {
    try {
     conn.close();
    } catch (SQLException e) {
     e.printStackTrace();
    }
   }
  }
 }
}

linux

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn