Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to copy table from one database to another?

Tags:

java

resultset

I need to take a table from one database and upload it to a different database. So, I create two separate connection . Here is my code

Connection connection1 = // set up connection to dbms1
Statement statement = connection1.createStatement();
ResultSet result = statement.executeQuery("select * from ............. "); 

Connection connection2 =  // set up connection to dbms2
// Now I want to upload the ResultSet result into the second database
Statement statement2 = connection2.createStatement("insert into table2 " + result);
statement2.executeUpdate();

The above last lines do not work How can i do this ? The bottomline is how to reuse a ready resultset

ResultSet is a ready java object . I hope there is a way add it to batch or something like this and executeUpdate , but not to write the result set to some temporary space (List, csv etc.) and the insert

like image 352
Borat Sagddiev Avatar asked Mar 25 '14 03:03

Borat Sagddiev


People also ask

How do I copy data from one database to another?

Then you can copy the data at your leisure. The quickest way is to use a tool, like RazorSQL or Toad for doing this. To copy the table from one database to another database, first you have to create the exact table structure for the new table as old one, than copy the table entries from one table to another.

How do I transfer a table from one SQL Server to another?

Using Backup and Restore to Copy a SQL Server Table to Another Server. You can copy the data to a temporary table in a new database in server-A, then backup this database and restore it in the destination server, and finally move the data from the restored database into the real destination table.

Is it possible to copy one table to another table?

This works successfully. You can copy one table to other db table even with some additional columns. Stack Overflow requires external JavaScript from another domain, which is blocked or failed to load. Retry using another source.

How to copy data from one database to another in phpMyAdmin?

For very large tables whose SQL codes would be a pain to copy and paste, PHPmyadmin's Import and Export menus can easily be used as well. If you need the entire table structure (not just the basic data layout), use Task>Script Table As>Create To>New Query Window and run in your new database. Then you can copy the data at your leisure.


2 Answers

The simplest way to do this is with a prepared statement for the insert. It lets you create a single statement object that can be used to run the query multiple times with different parameter values.

try (final Statement statement1 = connection1.createStatement();
     final PreparedStatement insertStatement = 
     connection2.prepareStatement("insert into table2 values(?, ?)"))
{
    try (final ResultSet resultSet =
         statement1.executeQuery("select foo, bar from table1"))
    {
        while (resultSet.next())
        {
            // Get the values from the table1 record
            final String foo = resultSet.getString("foo");
            final int bar = resultSet.getInt("bar");

            // Insert a row with these values into table2
            insertStatement.clearParameters();
            insertStatement.setString(1, foo);
            insertStatement.setInt(2, bar);
            insertStatement.executeUpdate();
        }
    }
}

The rows are inserted into table2 as you iterate through the results from table1, so there's no need to store the whole result set.

You can also use the prepared statement's addBatch() and executeBatch() methods to queue up all the inserts and send them to the database all at once, instead of sending a separate message to the database for each individual inserted row. But that forces JDBC to hold all the pending inserts in memory locally, which it seems you're trying to avoid. So the one-row-at-a-time inserts are your best bet in this case.

like image 143
Wyzard Avatar answered Oct 04 '22 11:10

Wyzard


If you don't want to manually list out all the field names for every table in the database, you should be able to do this two-step process instead:

  1. Copy the table schema using the answer in this question.
  2. Use resultSet.getMetaData() to get the list of fields, and use that to drive a modified version of the SELECT/INSERT code in @Wyzard's answer.

I will post code here if I get it working.

like image 45
Alex R Avatar answered Oct 04 '22 10:10

Alex R