Apache DBUtils Tutorial

The Apache Commons DbUtils library is a small set of classes designed to make working with JDBC easier. JDBC resource cleanup code is mundane, error prone work so these classes abstract out all of the cleanup tasks from your code leaving you with what you really wanted to do with JDBC in the first place: query and update data.

Some of the advantages of using DbUtils are:

No possibility for resource leaks. Correct JDBC coding isn't difficult but it is time-consuming and tedious. This often leads to connection leaks that may be difficult to track down.

Cleaner, clearer persistence code. The amount of code needed to persist data in a database is drastically reduced. The remaining code clearly expresses your intention without being cluttered with resource cleanup.

Automatically populate JavaBean properties from ResultSets. You don't need to manually copy column values into bean instances by calling setter methods. Each row of the ResultSet can be represented by one fully populated bean instance.

We can also use Connection Pool(Datasource) with DBUtils, it is good practice to use a connection pool in your applications in order to increase the performance and scalability

DBUtils simplifies the CRUD (Create,Retrieve,Update,Delete) operations, with any database.

DbUtils is designed to be:

Small - you should be able to understand the whole package in a short amount of time.

Transparent - DbUtils doesn't do any magic behind the scenes. You give it a query, it executes it and cleans up for you.

Fast - You don't need to create a million temporary objects to work with DbUtils.

Referenece - > http://commons.apache.org/proper/commons-dbutils/

Required Libraries

You need to download

  1. Apache Commons DbUtils

Following jar must be in classpath

  1. commons-dbutils-1.5.jar
  2. mysql-connector-java-5.1.28-bin.jar

Create Table Structure

CREATE TABLE `employee` (

insert into `employee` (`EMPLOYEEID`, `EMPLOYEENAME`, `EMPLOYEE_ADDRESS`) values('1','Rockey','1');
insert into `employee` (`EMPLOYEEID`, `EMPLOYEENAME`, `EMPLOYEE_ADDRESS`) values('2','Jose','2');

DBUtils BeanHandler Example

Query Runner Executes SQL queries with pluggable strategies for handling ResultSets. This class is thread safe (see below example)

package com.test.dbutils;

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

import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.BeanHandler;

public class DBUtilsWithConnection {

public static void main(String[] args) throws SQLException {

final String url = "jdbc:mysql://localhost:3306/test";
final String driver = "com.mysql.jdbc.Driver";
final String usr = "root";
final String pwd = "root";

        QueryRunner run =
new QueryRunner();

        Connection conn = DriverManager.getConnection
(url, usr, pwd);
// -----------------------------------------------------------------------------------
ResultSetHandler<Employee> resultHandler = new BeanHandler<Employee>(Employee.class);
try {
Employee emp = run.query(conn, "SELECT * FROM employee WHERE employeename=?",
} finally {

1 2 3 4 Next

3 Responses to "Apache DBUtils Tutorial"
  1. Jim E 2014-03-01 09:57:19.0
  1. admin 2014-03-02 09:57:19.0
  1. Chon Nguyen 2018-01-02 23:43:03.0

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