Home | Ajax | BioInformatics | Dojo | EAI | EJB | Hibernate | J2ME | Java | Java Glossary | Java Servlets | JavaScript | Jboss | JDBC | JDO | Jmeter | JSF | JSP | JUnit | Maven | MySQL | Spring Framework | SQL | Struts | Technology | WAP | Web Services | XML
 
 
Search All Tutorials

 
Programming Tutorials: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML
 
JDBC
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
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.

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

                         

Facing Programming Problem?
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

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

 

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.

Hot Web Programming Job

Java String toLowerCase Example
Java String toCharArray Example
Java String substring Example
Java String indexOf Example
Java String startsWith Example
Java String hashCode Example
Java String matches Example
Java String length Example
Java String lastIndexOf Example
Java String isEmpty Example
Java String equalsIgnoreCase Example
Java String equals Example
Java String endsWith Example
Java String copyValueOf Example
Java String contentEquals Example
  EAI Articles
  Java Certification
Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

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

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

Copyright © 2007. All rights reserved.