Share on Google+Share on Google+

Display the column name using DBCP

This tutorial demonstrate how to display column name of the table using DBCP.


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

class GetColumnName {
  public static void main(String[] args) {
    BasicDataSource bds = new BasicDataSource();
    try {
      Connection con = bds.getConnection();
      try {
        Statement st = con.createStatement();
        ResultSet rs = st.executeQuery("SELECT * FROM student");
        ResultSetMetaData md = rs.getMetaData();
        int col = md.getColumnCount();
        System.out.println("Number of Column : " + col);
        System.out.println("Columns Name: ");
        for (int i = 1; i <= col; i++) {
          String col_name = md.getColumnName(i);
      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 jar files 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




Posted on: April 3, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial 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.