How to create a table in JDBC using another table?


You can create a table same as an existing table using the following syntax:

CREATE TABLE new_table as SELECT * from old_table;

Assume we have a table named dispatches with 5 records as shown below:

+-------------+--------------+--------------+--------------+-------+----------------+
| ProductName | CustomerName | DispatchDate | DeliveryTime | Price | Location       |
+-------------+--------------+--------------+--------------+-------+----------------+
| Key-Board   | Raja         | 2019-09-01   | 05:30:00     | 7000  | Hyderabad      |
| Earphones   | Roja         | 2019-05-01   | 05:30:00     | 2000  | Vishakhapatnam |
| Mouse       | Puja         | 2019-03-01   | 05:29:59     | 3000  | Vijayawada     |
| Mobile      | Vanaja       | 2019-03-01   | 04:40:52     | 9000  | Chennai        |
| Headset     | Jalaja       | 2019-04-06   | 18:38:59     | 6000  | Goa            |
+-------------+--------------+--------------+--------------+-------+----------------+

Following JDBC program establishes connection with a database and create a new table using the definition of an existing table.

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class CreateTable {
   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 the Statement object
      Statement stmt = con.createStatement();
      //Query to create a table
      String query = "CREATE TABLE Sales as SELECT * from dispatches";
      //Executing the query
      stmt.execute(query);
      System.out.println("Table created......");
   }
}

Output

Connection established......
Table created......

If you verify the contents of the Sales table you can observe that is created same as dispatches table.

mysql> select * from Sales;
+-------------+--------------+--------------+--------------+-------+----------------+
| ProductName | CustomerName | DispatchDate | DeliveryTime | Price | Location       |
+-------------+--------------+--------------+--------------+-------+----------------+
| Key-Board   | Raja         | 2019-09-01   | 05:30:00     | 7000  | Hyderabad      |
| Earphones   | Roja         | 2019-05-01   | 05:30:00     | 2000  | Vishakhapatnam |
| Mouse       | Puja         | 2019-03-01   | 05:29:59     | 3000  | Vijayawada     |
| Mobile      | Vanaja       | 2019-03-01   | 04:40:52     | 9000  | Chennai        |
| Headset     | Jalaja       | 2019-04-06   | 18:38:59     | 6000  | Goa            |
+-------------+--------------+--------------+--------------+-------+----------------+
5 rows in set (0.00 sec)

Updated on: 30-Jul-2019

297 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements