简体   繁体   中英

JDBC program to select data from one table and insert into another table in different db

i am new to java, i am trying to insert a data from old_db to new_db. i have tried a program to do that, its shows a error like "syntax error" cant find what the error.

the program has to getconnection to 2 postgres db and select data in a table and insert them into another database table. both table have same fields and datatype.

import java.sql.* ;   
public class con2 
{ 
public static void main( String[] args ) 
{ 
try 
{ 
   Connection con = DriverManager.getConnection( "jdbc:postgresql://localhost:5432/old_db","postgres","password");
try
{
   Connection con1 = DriverManager.getConnection( "jdbc:postgresql://localhost:5432/new_db","postgres","password");

 Statement st = con.createStatement();
 Statement st1 = con1.createStatement();   
ResultSet rs = st.executeQuery("SELECT * FROM users"); 



  int val = st1.executeUpdate("insert into users("+"'rs()'"+")");

        rs.close();
                        st.close(); 
                    st1.close(); 



}
catch(SQLException e) 
  { 
         System.out.println( "could not get JDBC connection for new_db: " + e ); 
  } 
}
catch(SQLException e) 
  { 
         System.out.println( "could not get JDBC connection for  old_db: " + e ); 
  } 
} 
} 

i am using jdbc4

I have written a program like shown below, it's working.

Thanks for the suggestions.

import java.sql.*;
import java.io.*;
import java.util.*;

public class test1 {

    public static void main(String[] argv) throws Exception {
        try {
            Connection con = DriverManager.getConnection( "jdbc:postgresql://localhost:5432/old","user","pass");
            Connection con1 = DriverManager.getConnection( "jdbc:postgresql://localhost:5432/new","user","pass");

            String sql = "INSERT INTO users("+ "name,"+ "active,"+ "login,"+ "password)"+ "VALUES(?,?,?,?)";

            Statement statement = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);

            PreparedStatement pstmt = con1.prepareStatement(sql);

            ResultSet rs = statement.executeQuery("SELECT * FROM users");
            while (rs.next()) {
                String nm = rs.getString(2);
                Boolean ac = rs.getBoolean(3);
                String log = rs.getString(4);
                String pass = rs.getString(5);

                pstmt.setString(1, nm);
                pstmt.setBoolean(2, ac);
                pstmt.setString(3, log);
                pstmt.setString(4, pass);

                pstmt.executeUpdate();
            }
        } catch (SQLException e) {
            System.out.println("could not get JDBC connection: " +e);
        } finally {
            con.close();
            con1.close();
        }
    }

}

This is quite wrong:

  int val = st1.executeUpdate("insert into users("+"'rs()'"+")");

You have to iterate over the ResultSet and INSERT each row.

I'd recommend a PreparedStatement to bind variables and a batch INSERT .

There are lots of other things I'd recommend that you change, but start with that and make the code work.

Two try/catch blocks nested this way is a bad idea. One will do.

Close your resources in a finally block.

This is only an example, but why do you have to SELECT and bring the data to the middle tier at all?

And why reinsert the data that's already there?

The question, as posed, makes no sense whatsoever . Voting to close.

另一种方法是在PostgreSQL中使用DBLinks

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM