Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8903393
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T01:51:32+00:00 2026-06-15T01:51:32+00:00

i am new to java, i am trying to insert a data from old_db

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T01:51:34+00:00Added an answer on June 15, 2026 at 1:51 am

    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();
            }
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i am trying to insert data from excel sheet into database in java. for
I'm still somewhat new to Java and trying to insert data into a database.
I am new to Java and trying to do a simple program to help
Hey I'm new to java servlets and I am trying to write one that
I am new to JAVA, I am trying to convert an input from a
I get an syntax error while trying to INSERT some values to Mysql from
I'm trying to get data from website which is encoded in UTF-8 and insert
when trying to insert data into my sqlite db I receive a logcat error
Im trying to append some JSON data from the last.fm API, I have been
i m new to android development. i am trying to insert data into remote

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.