Home » Java programming language

Transaction Management in JDBC

In this article, we are going to study about Transaction Management in JDBC.
Submitted by Jyoti Singh, on February 21, 2018

First, let’s understand what a transaction is. Transaction is a group of tasks or operations. It can also be seen as a single unit of work.

For example, a person booked an aircraft ticket of Rs.1500 then this transaction will be like this.

Persons Account (P)

  1. open account (P)
  2. old balance=P.balance
  3. new balance=P.balance-1500
  4. P.balance=new balance
  5. close account(P)

This amount will be added to the aircraft account.

Transaction management can be understood much better by using ACID properties. ACID stands for Atomicity, Consistency, Isolation and Durability. Let’s understand these terms first.

1) Atomicity: As Transaction is a group of operations, this property ensures that either all of the operations of transaction are executed or none. A transaction must never be partially committed.

2) Consistency: Consistency refers to state of database that is the data in the database must remains same after or before the transaction ,there should not be any adverse effect on database of any transaction. Database must remains in the same state as it was before the transaction execution.

3) Isolation: As in a database many transactions are executed simultaneously, this property ensures that each transaction will be executed as if it’s the only transaction in the system. No transaction should affect any other transaction, the must be independent.

4) Durability: Durability property let the database holds updated data even if system fails, crashes or power off etc. If a transaction is committed successfully and it does and changes to the data but somehow system fails and it could not be written on to the disk, database will still hold the updated data and will update it once the system restarts.

This was all about ACID properties of Transaction. Now let’s take an example to see the Transaction Management in JDBC.

First, you have to create a table in your database named “employee”. Add following fields in it.

Java transaction management database

This is your table where your employee data will be stored.

This is your eclipse class for Transaction Management in JDBC.

package logicProgramming;
import; // to take the inut from user
import java.sql.Connection;     //to establish the connection
import java.sql.DriverManager; 
import java.sql.Statement;

public class ExampleTransaction {
	public static void main(String[] args) {
		try {
			Connection cn=DriverManager.getConnection("jdbc:mysql://localhost:3306/Employee","root", "123");
			cn.setAutoCommit(false);   //// HIghLighted method with arguement true Transaction method disabled.....
			Statement smt=cn.createStatement();
			DataInputStream kb=new DataInputStream(;
			System.out.println("Enter Employee Id");
			int id=Integer.parseInt(kb.readLine());
			System.out.println("Enter Employee Name");
			String name=(kb.readLine());
			System.out.println("Enter Employee Position ");
			String pos=(kb.readLine());
			System.out.println("Enter Salary");
			String salary =(kb.readLine());
			String query="insert into employee values("+id+",'"+name+"','"+pos+"','"+salary+"')";
			System.out.println("Do You Want to Commit Transaction \n1.Yes\n2.No");
			String ans=kb.readLine();
				cn.commit(); // Commiting the transaction
				System.out.println("Transaction SuccessFully Commited...");
				System.out.println("Transaction Rollbacked......");
		catch(Exception e)

Output of this code will look like this:

Java - transaction management example output



Comments and Discussions



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© some rights reserved.