私は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
クラスです。col1
col1
col2
col2
public class TestResponse {
private List<String> col1;
private List<String> col2;
// getters and setters
}
現在、適切なSQLクエリを作成できるように、メソッドを反復TestResponse
する方法がわかりません。insertIntoDatabase
そして、このSQLクエリをそのまま使用して挿入できます。