Share on Google+Share on Google+

JDBC Transaction Example

In this tutorial you will learn how run multiple SQL statements in JDBC using JDBC transaction

JDBC Transaction Example

JDBC Transaction

 JDBC transaction is used to execute a group of SQL statement so that they can execute together successfully. When  you create a connection using JDBC, by default it is in auto-commit mode and  each SQL statement is treated as transaction and it is committed automatically when an SLQ statement is executed. When you want that one or more SQL statement can execute together, then you have to set conn.setAutoCommit(false).

When you set a auto-commit false then SQL statement will be executed only when you call commit() method explicitly. All the previous call will committed together or there is a problem then all statement will roll back.

At first create table named student in MySql database and inset values into it as.

CREATE TABLE student (
Name tinytext NOT NULL,
Course varchar(25) NOT NULL,
Address text

Here is the video tutorial which shows you how to run the code example with instruction.

How to manage transaction in JDBC?

JDBC Transaction Video Tutorial

Then Write the Following java code.


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

public class JDBCTransactionExmple {
public static void main(String[] args) throws SQLException {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
String driverName = "com.mysql.jdbc.Driver";
String connectionUrl = "jdbc:mysql://localhost:3306/";
String databaseName = "student";
String userName = "root";
String password = "root";
try {
	// Loading Driver
	// Creating Connection
	conn = DriverManager.getConnection(connectionUrl + databaseName,
			userName, password);
	// Setting auto commit false
	System.out.println("Databese Connection Done........");
} catch (ClassNotFoundException e) {
	System.out.println("Error In Connection");
try {
	// Creating Statement
	stmt = conn.createStatement();
	// Creating Query String
	String updateQuery1 = "INSERT INTO student VALUES(1,'Vinay','MCA','Motihari')";
	String updateQuery2 = "INSERT INTO student VALUES(2,'Ram','BCA','Patna')";
	String selectQuery = "SELECT * FROM student";
	rs = stmt.executeQuery(selectQuery);
	// Calling commit() method
	while ( {
		System.out.println("Roll No.- " + rs.getInt("RollNo")
				+ ", Name- " + rs.getString("Name") + ", Course- "
				+ rs.getString("Course") + ", Address- "
				+ rs.getString("Address"));
} catch (Exception e) {
} finally {
	// Closing Connection
	System.out.println("Connection Closed..........");

When you run this application it will display message as shown below:

Databese Connection Done........
Roll No.- 1, Name- Vinay, Course- MCA, Address- Motihari
Roll No.- 2, Name- Ram, Course- BCA, Address- Patna
Connection Closed..........

Download this example code

Download the code discussed in the Video Tutorials in Eclipse project format.


Posted on: October 14, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial 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.