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 |

|
Current Comments
1 comments so far (post your own) View All Comments Latest 10 Comments:hi,
Iam Manikandan
I want how to logout the page one link to another link. i given the link to my design pages logout only didn't came please mail me.
HTTP Status 500 -
--------------------------------------------------------------------------------
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
javax.servlet.ServletException: Cannot retrieve definition for form bean loginaction
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:825)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:758)
org.apache.jsp.design.login_jsp._jspService(login_jsp.java:79)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:324)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
root cause
javax.servlet.jsp.JspException: Cannot retrieve definition for form bean loginaction
org.apache.struts.taglib.html.FormTag.lookup(FormTag.java:831)
org.apache.struts.taglib.html.FormTag.doStartTag(FormTag.java:506)
org.apache.jsp.design.login_jsp._jspx_meth_html_form_0(login_jsp.java:191)
org.apache.jsp.design.login_jsp._jspx_meth_html_html_0(login_jsp.java:164)
org.apache.jsp.design.login_jsp._jspService(login_jsp.java:69)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:324)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
note The full stack trace of the root cause is available in the Apache Tomcat/5.0.28 logs.
--------------------------------------------------------------------------------
Apache Tomcat/5.0.28
Posted by Manikandan on Thursday, 02.28.08 @ 17:49pm | #50490