Home Tutorial Java Jdbc Transaction Transaction in JDBC

 
 

Share on Google+Share on Google+
Transaction in JDBC
Posted on: March 15, 2010 at 12:00 AM
Advertisement
It is a feature of JDBC Transactions to Preserve Data Integrity. Savepoint set the initialization position and the rollback unsaved all the change made starting from the savepoint where it is set.

Description:

It is a feature of JDBC Transactions to Preserve Data Integrity. Savepoint set the initialization position and the rollback unsaved all the change made starting from the savepoint where it is set.

Here in this code you will see that Savepoint savep1 = con.setSavepoint("SAVEPOINT1"); sets its position.

Insertion operation is done but no permanently changes because it rollback the insertion operation.

 

Code:

package package1;

import java.sql.*;

public class prog1{

public static void main(String[] args) {

System.out.println("Demonstrating the savepoint and rollback in java!");

Connection con = null;

String url = "jdbc:mysql://localhost:3306/";

String db = "mydb";

String driver = "com.mysql.jdbc.Driver";

try{

Class.forName(driver);

con = DriverManager.getConnection(url+db,"root","root");

try{

con.setAutoCommit(false);

Statement st = con.createStatement();

Savepoint savep1 = con.setSavepoint("SAVEPOINT1");

st.executeUpdate("INSERT into employee VALUES("+1+","+"'raman'"+")");

System.out.println("1 row affected");

con.rollback(savep1);

System.out.println("Insertion done but rollback so no row affected");

con.commit();

}

catch (SQLException s){

System.out.println("SQL statement is not executed!");

}

}

catch (Exception e){

e.getMessage();

}

}

}

Output

Demonstrating the savepoint and rollback in java!

1 row affected

Insertion done but rollback so no row affected

Advertisement

Related Tags for Transaction in JDBC:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: March 15, 2010

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com