JDBC transaction management example

By | July 19, 2018

In previous post we learnt about Transaction basic concept

In this post we will learn how to manage transaction in JDBC.

In JDBC API we have Connection interface which provides:

  • setAutoCommit(boolean) method to  disable auto-commit mode by passing the false value. In JDBC by default commit is true so if you execute any statement it’s commits immediately
  • commit() method to commit the transaction if all statements are executed successfully.
  • rollback() method to cancel  the transaction if any one of statements fails.

Project structure in eclipse:

DB Script to create citi_bank table and Inserting two account holders (database schema jdbcdb)
DB.sql

DBUtil.java class which is responsible to connect with MySQL database.

Model class Account.java

Service interface and It’s implementation class:

DAO(Data access Object) interface and it’s implementation class:

Exception classes:AccountNotFoundException.java  and InsufficientBalanceException.java

Client program 

That’s all for this topic JDBC transaction management example

You May Also Like:

Transaction basic concept
Insert and Select Rows with JdbcRowSet Objects
Update and Delete Rows with JdbcRowSet Objects
Jdbc RowSetListener example

If you have any feedback or suggestion please feel free to drop in below comment box.

Leave a Reply

Your email address will not be published. Required fields are marked *