How to add a new column to an existing table using JDBC API?


You can add a new column to a table using the ALTER TABLE command.

Syntax

ALTER TABLE table_name ADD column_name datatype;

Assume we have a table named Sales in the database with 5 columns namely ProductName, CustomerName, DispatchDate, DeliveryTime, Price and, Location as shown below:

+-------------+--------------+--------------+--------------+-------+----------------+
| ProductName | CustomerName | DispatchDate | DeliveryTime | Price | Location       |
+-------------+--------------+--------------+--------------+-------+----------------+
| Key-Board   | Raja         | 2019-09-01   | 08:51:36     | 7000  | Hyderabad      |
| Earphones   | Roja         | 2019-05-01   | 05:54:28     | 2000  | Vishakhapatnam |
| Mouse       | Puja         | 2019-03-01   | 04:26:38     | 3000  | Vijayawada     |
| Mobile      | Vanaja       | 2019-03-01   | 04:26:35     | 9000  | Vijayawada     |
| Headset     | Jalaja       | 2019-04-06   | 05:19:16     | 6000  | Vijayawada     |
+-------------+--------------+--------------+--------------+-------+----------------+

Following JDBC program establishes connection with MySQL database, and adds a new column named ID to the Sales table and populates it.

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
public class AddingColumn {
   public static void main(String args[]) throws SQLException {
      //Registering the Driver
      DriverManager.registerDriver(new com.mysql.jdbc.Driver());
      //Getting the connection
      String mysqlUrl = "jdbc:mysql://localhost/mydatabase";
      Connection con = DriverManager.getConnection(mysqlUrl, "root", "password");
      System.out.println("Connection established......");
      //Creating the Statement
      Statement stmt = con.createStatement();
      //Query to alter the table
      String query = "ALTER TABLE Sales ADD ID INT NOT NULL";
      //Executing the query
      stmt.executeUpdate(query);
      System.out.println("Column added......");
      //inserting values in to the added column
      PreparedStatement pstmt = con.prepareStatement("UPDATE sales SET id = ? where ProductName = ");
      pstmt.setInt(1, 1);
      pstmt.setString(2,"Key-Board");
      pstmt.executeUpdate();

      pstmt.setInt(1, 2);
      pstmt.setString(2,"Earphones");
      pstmt.executeUpdate();

      pstmt.setInt(1, 3);
      pstmt.setString(2,"Mouse");
      pstmt.executeUpdate();

      pstmt.setInt(1, 4);
      pstmt.setString(2,"Mobile");
      pstmt.executeUpdate();

      pstmt.setInt(1, 5);
      pstmt.setString(2,"Headset");
      pstmt.executeUpdate();

      System.out.println("Values inserted......");
   }
}

Output

Connection established......
Column added......
Values inserted in the added column

Since we have added one column, if you retrieve the contents of the Sales table using the SELECT command you can observe 7 columns, with additional column named id as:

mysql> select * from Sales;
+-------------+--------------+--------------+--------------+-------+----------------+----+
| ProductName | CustomerName | DispatchDate | DeliveryTime | Price | Location       | ID |
+-------------+--------------+--------------+--------------+-------+----------------+----+
| Key-Board   | Raja         | 2019-09-01   | 08:51:36     | 7000  | Hyderabad      | 1  |
| Earphones   | Roja         | 2019-05-01   | 05:54:28     | 2000  | Vishakhapatnam | 2  |
| Mouse       | Puja         | 2019-03-01   | 04:26:38     | 3000  | Vijayawada     | 3  |
| Mobile      | Vanaja       | 2019-03-01   | 04:26:35     | 9000  | Chennai        | 4  |
| Headset     | Jalaja       | 2019-03-01   | 05:19:16     | 6000  | Delhi          | 5  |
+-------------+--------------+--------------+--------------+-------+----------------+----+
5 rows in set (0.00 sec)

Updated on: 30-Jul-2019

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements