Use Apache DBUtils.jar to ease closing jdbc resources in application

Apache DBUtils

Apache DBUtils 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.

problem that we face with normal jdbc code

We might be using following type of code, when ever we work with JDBC.

let’s assume that if we don’t close resultset ,prepared statement, connection what happens? we are saying connection leakage, we are so many times telling that connection leakage, is that so important? yes it is. you won’t find this as a big issue when you are developing applications, working with applications at home. But, actually if you go live in the production environment , things can be quite different. Connection Leakage is a serious issue for any software as it is a costlier resource.

As I stated in the advantages section, writing perfect Jdbc code isn’t difficult, but it is tedious task. DBUtils.jar provides set of methods to close the resources perfectly. In the DbUtils class all the methods are being declared as static to access these methods directly using class with no need of creating object. The methods available in the class are.

Methods in the DbUtils class


You will be using these methods to close the database related resources like ResultSet , PreparedStatement, Statement and Connection objects in the finally block. when you use these methods the following type of logic is not necessary.

you just have to write this kind of  logic to handle jdbc connection leakage.

Understanding the internals of DbUtils class

* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements.  See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the “License”); you may not use this file except in compliance with
* the License.  You may obtain a copy of the License at
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an “AS IS” BASIS,
* See the License for the specific language governing permissions and
* limitations under the License.

Where to get and How to Use DbUtils.jar ?

  • In order to use these methods you should add DbUtils.jar to your project class path.
  • You can get this jar from here

Leave a Comment