Home Tutorial Java Dbcp Retrieving data from table using DBCP


Share on Google+Share on Google+
Retrieving data from table using DBCP
Posted on: April 3, 2010 at 12:00 AM
This tutorial demonstrate how to retrieve records from table using DBCP.


import java.sql.*;
import org.apache.commons.dbcp.BasicDataSource;

public class GetAllRowsDBCP {
  public static void main(String[] args) {
    System.out.println("Getting All Rows from a table!\n");
    BasicDataSource bds = new BasicDataSource();
    try {
      Connection con = bds.getConnection();
      try {
        Statement st = con.createStatement();
        ResultSet res = st.executeQuery("SELECT * FROM  users");
        System.out.println("ID: " "\t\t" "NAME: ");
        while (res.next()) {
          int i = res.getInt("id");
          String s = res.getString("name");
          System.out.println(i + "\t\t" + s);
      catch (SQLException s) {
        System.out.println("SQL code does not execute.");
    catch (Exception e) {

Directory structure of files

In the lib folder place all required file ie. commons-collections.jar, commons-dbcp.jar, commons-pool.jar, j2ee.jar and mysql-connector-java-5.1.7-bin.jar

In the pathset.bat write following statement

set classpath=.;lib/commons-collections.jar;lib/commons-dbcp.jar;lib/commons-pool.jar;lib/j2ee.jar;lib/mysql-connector-java-5.1.7-bin.jar



Related Tags for Retrieving data from table using DBCP :

Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: April 3, 2010

Recommend the tutorial

Advertisements Advertisements