How to reorder the columns of a table in JDBC?


You can reorder the columns of a table in MySQL database using the ALTER TABLE command.

Syntax

ALTER TABLE table_name MODIFY column_name datatype AFTER another_column

Assume we have a table named dispatches_data in the database with 7 columns namely ProductName, CustomerName, DispatchDate, DeliveryTime, Price, Location and, ID with description as −

//Retrieving the Time object
Time timeObj = rs.getTime("DeliveryTime");
//Converting the Time object to String format
String time = timeObj.toString();

Example

Let us create a table with name dispatches in MySQL database using CREATE statement as follows −

CREATE TABLE dispatches(
   ProductName VARCHAR(255),
   CustomerName VARCHAR(255),
   Price INT,
   Location VARCHAR(255),
   DispatchTimeStamp timestamp);

Now, we will insert 6 records in dispatches_data table using INSERT statements −

insert into dispatches values('Key-Board', 'Raja', 7000, 'Hyderabad', TIMESTAMP('2019-05-04 15:02:45'));
insert into dispatches values('Earphones', 'Roja', 2000, 'Vishakhapatnam', TIMESTAMP('2019-06-26 14:13:12'));
insert into dispatches values('Mouse', 'Puja', 3000, 'Vijayawada', TIMESTAMP('2019-12-07 07:50:37'));
insert into dispatches values('Mobile', 'Vanaja', 9000, 'Chennai', TIMESTAMP('2018-03-21 16:00:45'));
insert into dispatches values('Headset', 'Jalaja', 6000, 'Goa', TIMESTAMP('2018-12-30 10:49:27'));
insert into dispatches values('Watch', 'Rajan', 4000, 'Chennai', TIMESTAMP('2019-04-21 14:17:02'));

Following JDBC program establishes connection with the database and re-orders the columns of the dispatches_data table.

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class ReorderingColumnsOfTable {
   public static void main(String args[])throws Exception {
      //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 a Statement object
      Statement stmt = con.createStatement();
      //Query to re-order the table
      String query = "ALTER TABLE dispatches_data MODIFY DispatchTimeStamp timestamp AFTER CustomerName";
      //Executing the query
      stmt.execute(query);
      //Retrieving the contents of the dispatches_data table
      stmt = con.createStatement();
      ResultSet rs = stmt.executeQuery("select * from dispatches_data");
      while(rs.next()) {
         System.out.print("Name: "+rs.getString("ProductName")+", ");
         System.out.print("Customer Name: "+rs.getString("CustomerName")+", ");
         System.out.print("Dispatch time stamp: "+rs.getTimestamp("DispatchTimeStamp")+", ");
         System.out.print("Price: "+rs.getInt("Price")+", ");
         System.out.print("Location: "+rs.getString("Location"));
         System.out.println();
      }
   }
}

Here, in this program we are moving the DispatchTimeStamp column from 5th position to 3rd position using the alter command.

Output

Connection established......
Name: Key-Board, Customer Name: Raja, Dispatch time stamp: 2019-05-04 15:02:45.0, Price: 7000, Location: Hyderabad
Name: Earphones, Customer Name: Roja, Dispatch time stamp: 2019-06-26 14:13:12.0, Price: 2000, Location: Vishakhapatnam
Name: Mouse, Customer Name: Puja, Dispatch time stamp: 2019-12-07 07:50:37.0, Price: 3000, Location: Vijayawada
Name: Mobile, Customer Name: Vanaja, Dispatch time stamp: 2018-03-21 16:00:45.0, Price: 9000, Location: Chennai
Name: Headset, Customer Name: Jalaja, Dispatch time stamp: 2018-12-30 10:49:27.0, Price: 6000, Location: Goa
Name: Watch, Customer Name: Rajan, Dispatch time stamp: 2019-04-21 14:17:02.0, Price: 4000, Location: Chennai
if you verify the contents of the table you can observe that the order of the columns has been changed.


mysql> select * from dispatches_data;
+-------------+--------------+---------------------+-------+----------------+
| ProductName | CustomerName | DispatchTimeStamp | Price | Location |
+-------------+--------------+---------------------+-------+----------------+
| Key-Board | Raja | 2019-05-04 15:02:45 | 7000 | Hyderabad |
| Earphones | Roja | 2019-06-26 14:13:12 | 2000 | Vishakhapatnam |
| Mouse | Puja | 2019-12-07 07:50:37 | 3000 | Vijayawada |
| Mobile | Vanaja | 2018-03-21 16:00:45 | 9000 | Chennai |
| Headset | Jalaja | 2018-12-30 10:49:27 | 6000 | Goa |
| Watch | Rajan | 2019-04-21 14:17:02 | 4000 | Chennai |
+-------------+--------------+---------------------+-------+----------------+
6 rows in set (0.00 sec)

Updated on: 30-Jul-2019

156 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements