Copy Table in a MySQL Database

In this section, you will learn to copy one table to another in a same MySQL database.


 

Ads

Tutorials   
Change Column Name in MySQL Java Mysql Connection Example JDBC Example with MySQL Jdbc Mysql Connection String Jdbc Mysql Connection Url Getting Column Names from a database table in Java Count Rows from a Database Table JDBC Example with MySQL Write Text File to Table Inserting Image in Database Table Insert Data in Table Using Stored Procedure Copy One Database Table to Another Copy Table in a MySQL Database Set Timestamp by using the Prepared Statement Set Time by using the Prepared Statement Set Date by using the Prepared Statement Prepared Statement Set Big Decimal Set byte, short and long data types by using the Prepared Statement Set Data Types by using Prepared Statement Using the Prepared Statement Twice Deleting Records using the Prepared Statement Count Records using the Prepared Statement Inserting Records using the Prepared Statement Update Records using Prepared Statement Select Records Using Prepared Statement Prepared Statement With Batch Update Statement Batch Update Prepared Statement Set Object Cross Join Tables in a Specific Database Join tables with the NATURAL RIGHT JOIN operation Join tables with the NATURAL LEFT JOIN operation Join tables in the specific database Delete a Column from a Database Table Delete a Specific Row from a Database Table Deleting All Rows from a Database Table Sum of Column in a Database Table Arrange a Column of Database Table Arrange a Column of Database Table Remove Unique Column in Database Table Make Unique Column in Database Table Change Column Name of a Table Adding a New Column Name in Database Table Retrieving All Rows from a Database Table Inserting values in MySQL database table Retrieving Tables from a Database Deleting a Table from Database Description of Database Table Creating a MySQL Database Table to store Java Types Creating a Database Table Creating a Database in MySQL
Ads

Copy Table in a MySQL Database

     

In this section, you will learn to copy one table to another in a same MySQL database.

Description of Code:

This program helps you in copying one table to another in a same MySQL database. To copy any table,  firstly, you need to establish the connection with MySQL database (jdbc4). This database has both the tables (Copyemployee and employee table). That is the one which is to be copied to the other table and the second is the table to be copied. For copying a table, we have applied the ?INSERT INTO Copyemployee SELECT * FROM employee? SQL statement. Whenever a table is copied, it displays ?Numbers of row(s) affected?. If none of the rows is copied then it shows a message ?Don?t add any row!?.

[Note: Both tables have the same field and its data type.]

Syntax for copy tables:

INSERT INTO <new_table_name> SELECT * FROM <old_table_name>

Here is code of program:

import java.sql.*;

public class CopyOneTableToAnother{
  public static void main(String[] args) {
  System.out.println("Copy data from one table 
  to another in a database!"
);
  Connection conn = null;
  String url = "jdbc:mysql://localhost:3306/";
  String dbName = "jdbc4";
  String driver = "com.mysql.jdbc.Driver";
  String userName = "root"
  String password = "root";
  try {
  //Load jdbc driver
  Class.forName(driver).newInstance();
  //Establish the connection
  conn = DriverManager.getConnection(url+dbName,userName,password);
  Statement st = conn.createStatement();
  //Copy table
  int rows = st.executeUpdate("INSERT INTO Copyemployee 
   SELECT * FROM employee"
);
  if (rows == 0){
  System.out.println("Don't add any row!");
  }
  else{
  System.out.println(rows + " row(s)affected.");
  conn.close();
  }
  }
  catch (Exception e) {
  e.printStackTrace();
  }
  }
}

Download this program.

Table Name: employee

empId empName empSal
1 Vinod 50000
2 Sushil 80000

Table Name: Copyemployee

empId empName empSal
8 AmarDeep 10000
7 Noor 50000

Output of this program:

C:\vinod>javac CopyOneTableToAnother.java

C:\vinod>
C:\vinod>java CopyOneTableToAnother
Copy data from one table to another in a database!
2 row(s)affected.

C:\vinod>

After copy table: Copyemployee

empId empName empSal
8 AmarDeep 10000
7 Noor 50000
1 Vinod 50000
2 Sushil 80000

Advertisements

Ads
Share on Google+Share on Google+

Copy Table in a MySQL Database

Posted on: July 6, 2007 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

 

Discuss: Copy Table in a MySQL Database   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0

Ads

 

Ads