Apache DBUtils Tutorial

DBUtils ArrayListHandler Example

package com.test.dbutils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;
import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.ArrayListHandler;

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();

        DbUtils.loadDriver
(driver);
        Connection conn = DriverManager.getConnection
(url, usr, pwd);
       
// -----------------------------------------------------------------------------------

       
try {
           
List<Object[]> query = run.query(conn, "SELECT * FROM employee", new ArrayListHandler());
           
for (Object[] objects : query) {
               
System.out.println(Arrays.toString(objects));
           
}
        }
finally {
           
DbUtils.close(conn);
       
}

    }
}
Output
[1, Rockey, 1]

[2, Jose, 2]

DBUtils ResultSetHandler Example

ResultSetHandler implementation that converts the first ResultSet row into a JavaBean. This class is thread safe.

package com.test.dbutils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.Arrays;
import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;

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();

        DbUtils.loadDriver
(driver);
        Connection conn = DriverManager.getConnection
(url, usr, pwd);
       
// -----------------------------------------------------------------------------------

       
ResultSetHandler<Object[]> handler = new ResultSetHandler<Object[]>() {
           
public Object[] handle(ResultSet rs) throws SQLException {
               
if (!rs.next()) {
                   
return null;
               
}
               
ResultSetMetaData meta = rs.getMetaData();
               
int cols = meta.getColumnCount();
                Object
[] result = new Object[cols];

               
for (int i = 0; i < cols; i++) {
                   
result[i] = rs.getObject(i + 1);
               
}
               
return result;
           
}
        }
;

       
try {
           
Object[] result = run.query(conn, "SELECT * FROM employee WHERE employeename=?", handler, "Rockey");
            System.out.println
(Arrays.toString(result));

       
} finally { // Use this helper method so we don't have to check for
           
DbUtils.close(conn);
       
}

    }
}
Output
[1, Rockey, 1]




Previous 1 2 3 4 Next








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

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