Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Copy Table in a MySQL Database 
 

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

 

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

                         

» View all related tutorials
Related Tags: c database query ide table data io column numbers number sum using this id simple tab student university if ie

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

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

Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.