Fast and Flexible DB Retrieval API from Apache DB Util

Sample Program : 

Dependency Jar Library Files :

1] commons-dbcp-1.4.jar

2] commons-dbutils-1.7.jar

3] commons-lang3-3.7.jar

4] commons-pool2-2.4.3.jar

5] mysql-connector-java-6.0.6.jar

=============================================

public class ErrorBean {
private int id;
private String severity;
private String errorcode;

public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getSeverity() {
return severity;
}
public void setSeverity(String severity) {
this.severity = severity;
}
public String getErrorcode() {
return errorcode;
}
public void setErrorcode(String errorcode) {
this.errorcode = errorcode;
}
}

===========================================

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

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;
import org.apache.commons.dbutils.handlers.BeanListHandler;

public class AsbmQueryGenerator {

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

final String url = “jdbc:mysql://localhost:3306/imig_error_engine”;
final String driver = “com.mysql.jdbc.Driver”;
final String usr = “root”;
final String pwd = “root”;
Connection connection =null;
try{

AsbmQueryGenerator asbm = new AsbmQueryGenerator();
connection = asbm.AsbmDatabaseConnector(url,driver,usr,pwd);
System.out.println(“Connection Success :=> “+connection);

QueryRunner run = new QueryRunner();
ResultSetHandler<List<ErrorBean>> resultHandler = new BeanListHandler<ErrorBean>(ErrorBean.class);
List<ErrorBean> errorList = run.query(connection, “SELECT * FROM core_error”,resultHandler);
System.out.println(” ERROR SIZE :”+errorList.size());

for(ErrorBean errBean:errorList){
//System.out.print( ToStringBuilder.reflectionToString((Object)errorList.get(1),ToStringStyle.MULTI_LINE_STYLE));
System.out.print(“ID: ” + errBean.getId());
System.out.print(“, SEVERITY: ” + errBean.getSeverity());
System.out.print(“, ERROR_CODE: ” + errBean.getErrorcode());
}
} finally {
DbUtils.close(connection);
}

}

/*
* This will return the Database Connection
*
*/
public Connection AsbmDatabaseConnector(String urlString,String driverString,String userName,String password)throws SQLException
{
DbUtils.loadDriver(driverString);
Connection connection = DriverManager.getConnection(urlString, userName, password);
return connection;
}

}

About ashokabhat

I am a C,C ,JAVA,Adobe Flex,.NET Programmer Currently working as a Software Developer
This entry was posted in Core Java. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s