Java JDBC Connection commit/rollback transaction

Description

Java JDBC Connection commit/rollback transaction

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class Main {
   public static void run(Connection conn){
      boolean successFlag = false;
      try {/*from ww w  .  j  a v  a2s  .c  o  m*/
          conn.setAutoCommit(false);
          //query
          //insert
          //insert
          //update

          // Commit Transactions
          if (successFlag == true)
              conn.commit();  
          else
              conn.rollback();

          conn.setAutoCommit(true);

      } catch (java.sql.SQLException ex) {
          System.out.println(ex);
      } finally {
          if (conn != null) {
              try {
                  conn.close();
              } catch (SQLException ex) {
                  ex.printStackTrace();
              }
          }
      }
       
   }
}



PreviousNext

Related