0

私はPostgres sqlデータベースで作業しています.SQLクエリを実行する必要がありdatabase-A、そのクエリから取得した結果が何であれ、結果をそのまま挿入する必要database-Bがあり、これは毎週金曜日に行う必要があります.

そこでScheduledExecutorService、毎週金曜日に上記のジョブを実行する特定のメソッドを呼び出す which を使用することにしました。

以下は、(getFromDatabase)毎週金曜日に実行される私のメソッドです-

以下のメソッドでは、単純な選択クエリを実行し、結果をメソッドdatabase-Aに保存していますTestResponse

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 .... ";
}

そして以下は私のgetFromDatabaseA 方法です -

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;
    }

以下は、willのすべての値がリンクされたリストに移動し、 willのすべての値がリンクされたリストに移動する私のTestResponseクラスです。col1col1col2col2

public class TestResponse {

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

    // getters and setters
}

現在、適切なSQLクエリを作成できるように、メソッドを反復TestResponseする方法がわかりません。insertIntoDatabaseそして、このSQLクエリをそのまま使用して挿入できます。

4

2 に答える 2

1

列の詳細のリストをオブジェクトに挿入しましたが、これは適切な動作ではありません。

次のように変更します

    public class TestResponse {

  private String col1;
  private String col2;

 // getters and setters
  }

のように使う

     List<TestResponse> responses = null;

使用する

    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 .... ";

}

変更する

    getFromDatabaseA 

このような

     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;
于 2014-03-03T05:40:21.013 に答える
-2
SELECT * FROM old_table INTO new_table

またはあなたが使用することができます

 insert into table2 values(select * FROM table1);
于 2014-03-03T04:54:02.747 に答える