- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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)
- Related Articles
- How to create a table in Oracle using JDBC?
- How to create a table in JavaDB using JDBC?
- How to create a table in a database using JDBC API?
- How to create a table with decimal values using JDBC?
- How to create a table with auto-increment column in MySQL using JDBC?
- Example to create a table with all datatypes in MySQL using JDBC?
- Create a table in MySQL that matches another table?
- How to drop a table from JavaDB using JDBC?
- How to create a MySQL table based on JDBC Result Set?
- How to insert Binary data into a table using JDBC?
- How to retrieve binary data from a table using JDBC?
- How to drop a table from a database using JDBC API?
- How to insert a DATALINK object into a table using JDBC?
- How to retrieve a DATALINK object from a table using JDBC?
- How to store decimal values in a table using PreparedStatement in JDBC?

Advertisements