Home Tutorial Java Jdbc Jdbcbatch Alter Database Table Using JDBC Batch Process

 
 

Alter Database Table Using JDBC Batch Process
Posted on: October 14, 2010 at 12:00 AM
In this example, we are discuss about alter database table using JDBC Batch process.

Alter Database Table Using JDBC Batch Process:

In this example, we are discuss about alter database table using JDBC Batch process.

First of all, we will create a java class BatchAlterTable.java.

 In this class we will create database connection as:

Connection connection = null;

Class.forName("com.mysql.jdbc.Driver");

connection = DriverManager.getConnection

("jdbc:mysql://localhost:3306/databasename","username","passsword");

Now we will used sequence of the step for Batch processing for alter database table.

1. Create Statement object using createStatement() method on the created Connection object as:

Statement stmt = connection.createStatement();

2. set auto commit is false using setAutoCommit() as:

connection.setAutoCommit(false);

3. Create SQL alter table statement for modify the database user table

 and add these statement in batch using addBatch() method as:

String query = "ALTER TABLE user ADD address varchar(50)";

stmt.addBatch(query); 

4. Execute the SQL statements using executeBatch() method on created statement object as:

stmt.executeBatch();

5. Last commit the connection using commit() method as:

connection.commit();

The code of the BatchAlterTable.java class is:

import java.sql.*;
public class BatchAlterTable {
  
  public static void main(String[] args) {
    try{
        Connection connection = null;
          Class.forName("com.mysql.jdbc.Driver");    
          connection = DriverManager.getConnection(
           
"jdbc:mysql://localhost:3306/roseindia_jdbc_tutorial","root","root");
          try{            
              // Create statement object  
              Statement stmt = connection.createStatement();            
               
              // Set auto-commit to false
              connection.setAutoCommit(false);
              
              // Alter table query
              String query1 = "ALTER TABLE user ADD address varchar(50)";            
           
              stmt.addBatch(query1);
              
              stmt.executeBatch();
              
              System.out.println("Alter table by Batch processing" );

             // connection committed
              connection.commit()
          }
          catch (SQLException s){
            System.out.println("SQL Exception " + s);
          }
        }
        catch (Exception e){
          e.printStackTrace();
        }
  }

}

Program output:

The database table output before alter is:

The eclipse console output is:

The database table output after alter is:

Download source code

Related Tags for Alter Database Table Using JDBC Batch Process:


Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.