Apache Commons DBUtils - Using DataSource


Advertisements

So far, we've using connection object while using QueryRunner. We can also use datasource seemlessly. The following example will demonstrate how to read a record using Read query with the help of QueryRunner and datasource. We'll read a record from Employees Table.

Syntax

QueryRunner queryRunner = new QueryRunner( dataSource );
Employee emp = queryRunner.query("SELECT * FROM employees WHERE first=?", resultHandler, "Sumit");

Where,

  • dataSource − DataSource object configured.

  • resultHandler − ResultSetHandler object to map result set to Employee object.

  • queryRunner − QueryRunner object to read employee object from database.

To understand the above-mentioned concepts related to DBUtils, let us write an example which will run a read query. To write our example, let us create a sample application.

Step Description
1 Update the file MainApp.java created under chapter DBUtils - First Application.
2 Compile and run the application as explained below.

Following is the content of the Employee.java.

public class Employee {
   private int id;
   private int age;
   private String first;
   private String last;
   public int getId() {
      return id;
   }
   public void setId(int id) {
      this.id = id;
   }
   public int getAge() {
      return age;
   }
   public void setAge(int age) {
      this.age = age;
   }
   public String getFirst() {
      return first;
   }
   public void setFirst(String first) {
      this.first = first;
   }
   public String getLast() {
      return last;
   }
   public void setLast(String last) {
      this.last = last;
   }
}

Following is the content of the CustomDatasource.java.

import javax.sql.DataSource;
import org.apache.commons.dbcp2.BasicDataSource;

public class CustomDataSource {
   // JDBC driver name and database URL
   static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
   static final String DB_URL = "jdbc:mysql://localhost:3306/emp";
   
   //  Database credentials
   static final String USER = "root";
   static final String PASS = "admin";
   private static DataSource datasource;
   private static final BasicDataSource basicDataSource;

   static {
      basicDataSource = new BasicDataSource();
      basicDataSource.setDriverClassName(JDBC_DRIVER);
      basicDataSource.setUsername(USER);
      basicDataSource.setPassword(PASS);
      basicDataSource.setUrl(DB_URL);
   }

   public static DataSource getInstance() {
      return basicDataSource;
   }
}

Following is the content of the MainApp.java file.

import java.sql.SQLException;

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

public class MainApp {
   public static void main(String[] args) throws SQLException {    
      
      DbUtils.loadDriver(JDBC_DRIVER);
      QueryRunner run = new QueryRunner(CustomDataSource.getInstance());
      ResultSetHandler<Employee> resultHandler = new BeanHandler<Employee>(Employee.class);

      Employee emp = queryRunner.query("SELECT * FROM employees WHERE id=?",
         resultHandler, 103);
      
      //Display values
      System.out.print("ID: " + emp.getId());
      System.out.print(", Age: " + emp.getAge());
      System.out.print(", First: " + emp.getFirst());
      System.out.println(", Last: " + emp.getLast());
   }
}

Once you are done creating the source files, let us run the application. If everything is fine with your application, it will print the following message.

ID: 103, Age: 33, First: Sumit, Last: Mittal
Advertisements