News »Browse Articles »
Using a database transaction with JDBC
0
Using a database transaction with JDBC
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
/**
*
* @author javadb.com
*/
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();
}
}
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
/**
*
* @author javadb.com
*/
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();
}
}
Search News
News Categories
What's the News?
Post a link to something interesting from another site, or submit your own original writing for the Java community to read.
Most Popular News
-
How to stand out from other Java/JEE Professionals?
Published about 14-01-2009 | Rated +3 -
10 reasons IT certification will be important in 2009
Published about 05-01-2009 | Rated +2 -
The 9 hottest skills for `09
Published about 02-01-2009 | Rated +1 -
New Features in Servlets 3.0
Published about 05-01-2009 | Rated +4
Most Recent User Submitted News
- Using a database transaction with JDBC
Submitted by Balamurali | Rated 0 - Reorganizing Long Test Classes
Published about 29-07-2009 | Rated 0 - Tips: A JSF Dynamic Web Project
Published about 12-10-2009 | Rated 0 - EXAMPLE OF POLYMORPHISM IN JAVA
Submitted by Balamurali | Rated 0







