Question

I am working with Postgres sql database.. I need to run a SQL query against database-A and whatever results I am getting from that query, I need to insert the results into database-B as it is and this has to be done every weekly on Friday.

So I decided to use ScheduledExecutorService which will call a particular method every week on Friday which does the above job.

Below is my method (getFromDatabase) which will run every Friday -

In the below method, I am executing a simple select query against database-A and I am storing the result in TestResponse method

protected static void getFromDatabase() {
    TestResponse response = null;
    TestDaoImpl dao = new TestDaoImpl();
    String sql1 = "select col1, col2 from application limit 5";
    try {
        // get the data from database-A table
        response = dao.getFromDatabaseA(sql1);
        System.out.println(response);

        // now iterate this response and then insert into database-B table
        insertIntoDatabase(response);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

private static void insertIntoDatabase(TestResponse resp) {
    // how to use resp so that I can generate below correct insert SQL query?
    // which I can use to execute it?

    String query = "INSERT into table-database-B .... ";
}

And below is my getFromDatabaseA method -

private List<String> col1List = new LinkedList<String>();
private List<String> col2List = new LinkedList<String>();

 public TestResponse getFromDatabaseA(String query) throws Exception {
    TestResponse response = new TopMalwareAppsResponse();
    try {
        conn = TestConnection.getInstance().getCpds().getConnection();
        statement = conn.createStatement();
        rs = statement.executeQuery(query);
        // Extract data from result set
        while (rs.next()) {
        // Retrieve by column name
        String col1 = rs.getString("col1");
        col1List.add(col1);

        String col2 = rs.getString("col2");
        col2List.add(col2);
       }

        response.setCol1(col1List);
        response.setCol2(col2List);

    } catch (Exception e) {
        e.printStackTrace();
    }
    return response;
    }

Below is my TestResponse class in which all the values of col1 will go to col1 linked list and all the values of col2 will go to col2 linked list.

public class TestResponse {

    private List<String> col1;
    private List<String> col2;

    // getters and setters
}

Now I am not sure how to iterate TestResponse in insertIntoDatabase method in such a way so that I am able to make proper SQL query. And then I can use thi SQL query as it is to insert.

Was it helpful?

Solution

You inserted list of column details in to an object it is not a good behaviour,

change it as

    public class TestResponse {

  private String col1;
  private String col2;

 // getters and setters
  }

use like

     List<TestResponse> responses = null;

Use

    private static void insertIntoDatabase(List<TestResponse> resps) {
// how to use resp so that I can generate below correct insert SQL query?
// which I can use to execute it?
**// Iterate the above list and update second table**
String query = "INSERT into table-database-B .... ";

}

alter

    getFromDatabaseA 

like this

     List<TestResponse> testResponses = new ArrayList<TestResponse>();

     // Iterate for values or execute query for multiple time to get all details from table 1
   {  
     TestResponse testResponse = new TestResponse();
     testResponse.setsetCol1(/*  ADD COL1 DATA */);
     testResponse.setsetCol2(/*  ADD COL2 DATA */);

     testResponses.add(testResponse);
     }
     return testResponses;

OTHER TIPS

SELECT * FROM old_table INTO new_table

or you can use

 insert into table2 values(select * FROM table1);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top