Copy data from one table to another in a database : Table « Database « Java Tutorial






import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

public class Main {
  public static void main(String[] args) throws Exception {
    String url = "jdbc:mysql://localhost:3306/";
    String dbName = "jdbc4";
    String driver = "com.mysql.jdbc.Driver";
    String userName = "root";
    String password = "root";

    Class.forName(driver).newInstance();

    Connection conn = DriverManager.getConnection(url + dbName, userName, password);
    Statement st = conn.createStatement();

    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();
    }
  }
}








20.26.Table
20.26.1.Create a table in database
20.26.2.Delete record from table
20.26.3.Description of Database Table
20.26.4.Adding a New Column Name in Database Table
20.26.5.Change Column Name of a Table
20.26.6.Make Unique Column in Database Table
20.26.7.Remove Unique Column in Database Table
20.26.8.Arrange a Column of Database Table
20.26.9.Sum of Column in a Database Table
20.26.10.Delete a Column from a Database Table
20.26.11.Copy data from one table to another in a database
20.26.12.Drop table from database
20.26.13.Copy One Database Table to Another