Using a database transaction with JDBC

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;


public class Main {
    
    /**
     * Updates tables using a transaction
     */
    public void updateDatabaseWithTransaction() {
        
        Connection connection = null;
        Statement statement = null;
        
        try {
            Class.forName("[nameOfDriver]");
            
            connection = DriverManager.getConnection("[databaseURL]",
                    "[userid]",
                    "[password]");
            
            //Here we set auto commit to false so no changes will take
            //effect immediately.
            connection.setAutoCommit(false);
            
            statement = connection.createStatement();
            
            //Execute the queries
            statement.executeUpdate("UPDATE Table1 SET Value = 1 WHERE Name = 'foo'");
            statement.executeUpdate("UPDATE Table2 SET Value = 2 WHERE Name = 'bar'");
            
            //No changes has been made in the database yet, so now we will commit
            //the changes.
            connection.commit();
            
            
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        } catch (SQLException ex) {
            ex.printStackTrace();
            
            try {
                //An error occured so we rollback the changes.
                connection.rollback();
            } catch (SQLException ex1) {
                ex1.printStackTrace();
            }
        } finally {
            try {
                if (statement != null)
                    statement.close();
                if (connection != null)
                    connection.close();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }
        
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        
        new Main().updateDatabaseWithTransaction();
        
    }
}

0 comments:

                                                                

Site Meter