Home > Database > Mysql Tutorial > body text

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

WBOY
Release: 2016-06-07 17:02:43
Original
827 people have browsed it

一个用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

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!