Home Tutorial Java Dbcp Add new column to table using DBCP


Share on Google+Share on Google+
Add new column to table using DBCP
Posted on: April 5, 2010 at 12:00 AM
This tutorial demonstrate how to add new column to the table using DBCP. If the table already have existing column with data inside then the new column will have the null value to all its respective rows.


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

public class AddColumn {
  public static void main(String[] args) {
    BasicDataSource bds = new BasicDataSource();
    try {
      Connection con = bds.getConnection();
      try {
        Statement st = con.createStatement();
        int n = st
        .executeUpdate("ALTER TABLE emp ADD phone integer(15)");
        System.out.println("Column added !!!");
      catch (SQLException s) {
        System.out.println("No field added");
    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



Related Tags for Add new column to 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 5, 2010

Recommend the tutorial

Advertisements Advertisements