Here's what worked for me in the end - simple routine to copy multiple table from the T0 schema to the T1, T2 and T3 schemas
public class CopyFromT0toT123 {
public static void main(String[] args) throws Exception {
CopyFromT0toT123 copy = new CopyFromT0toT123();
copy.copyTable("table name 1", Schema.XXXX_T0, Schema.XXXX_T1, Schema.XXXX_T2, Schema.XXXX_T3);
copy.copyTable("table name 2", Schema.XXXX_T0, Schema.XXXX_T1, Schema.XXXX_T2, Schema.XXXX_T3);
copy.copyTable("table name 3", Schema.XXXX_T0, Schema.XXXX_T1, Schema.XXXX_T2, Schema.XXXX_T3);
}
private void copyTable(String tableName, Schema schemaSource, Schema... schemaDestinations) throws Exception {
// Clear out table
clearOutDestinationTable(tableName, schemaDestinations);
// Start copying
String sqlSelect = "SELECT * FROM " + tableName;
Schema schema = Schema.XXXX_T0;
Connection conn = DriverManager.getConnection(schema.getConnectionString(), schema.getUserId(), schema.getPassword());
PreparedStatement prepStmnt = conn.prepareStatement(sqlSelect);
ResultSet resSet = prepStmnt.executeQuery();
copyToDestinationTable(tableName, resSet, schemaSource, schemaDestinations);
resSet.close();
prepStmnt.close();
conn.close();
}
private void copyToDestinationTable(String tableName, ResultSet resSet, Schema schemaSource, Schema... schemaDestinations) throws Exception {
for (Schema schemaDestination : schemaDestinations) {
System.out.println("Copying " + tableName + " to " + schemaDestination.toString());
String sqlInsert = buildInsert(tableName);
Connection connDestination = DriverManager.getConnection(schemaDestination.getConnectionString(), schemaDestination.getUserId(), schemaDestination.getPassword());
PreparedStatement prepStmntDestination = connDestination.prepareStatement(sqlInsert);
while (resSet.next()) {
for (int i = 0; i < resSet.getMetaData().getColumnCount(); i++) {
prepStmntDestination.setObject(i + 1, resSet.getObject(i + 1));
}
prepStmntDestination.executeUpdate();
}
prepStmntDestination.close();
connDestination.close();
}
}
private void clearOutDestinationTable(String tableName, Schema... schemaDestinations) throws SQLException {
for (Schema schemaDestination : schemaDestinations) {
String sqlDelete = "DELETE FROM " + tableName;
Connection connDestination = DriverManager.getConnection(schemaDestination.getConnectionString(), schemaDestination.getUserId(), schemaDestination.getPassword());
PreparedStatement prepStmntDestination = connDestination.prepareStatement(sqlDelete);
prepStmntDestination.executeUpdate();
prepStmntDestination.close();
connDestination.close();
}
}
private String buildInsert(String tableName) throws SQLException {
String sqlSelect = "SELECT * FROM " + tableName;
String result = "";
Schema schema = Schema.XXXX_T0;
Connection conn = DriverManager.getConnection(schema.getConnectionString(), schema.getUserId(), schema.getPassword());
PreparedStatement prepStmnt = conn.prepareStatement(sqlSelect);
ResultSet resSet = prepStmnt.executeQuery();
ResultSetMetaData metaData = resSet.getMetaData();
String[] columnNames = new String[metaData.getColumnCount()];
result = "INSERT INTO " + tableName + "(";
for (int i = 0; i < columnNames.length; i++) {
result = result + metaData.getColumnName(i + 1) + ",";
}
result = result.substring(0, result.length() - 1) + ") VALUES ( ";
for (int i = 0; i < columnNames.length - 1; i++) {
result = result + " ? ,";
}
result = result + " ? )";
resSet.close();
prepStmnt.close();
conn.close();
return result;
}
}
public enum Schema {
XXXX_T0("XXXX_T0", "password here", "T0", "20", true,
"jdbc:oracle:thin:@(DESCRIPTION..."),
XXXX_T1("XXXX_T1", "password here", "T1", "30", true,
"jdbc:oracle:thin:@(DESCRIPTION....."),
XXXX_T2("XXXX_T2", "password here", "T2", "40", true,
"jdbc:oracle:thin:@(DESCRIPTION....."),
XXXX_T3("XXXX_T3", "password here", "T2", "50", true,
"jdbc:oracle:thin:@(DESCRIPTION....."),
private final String connectionString;
private final String userId;
private final String password;
private final String displayId;
private final String order;
private final boolean interAct;
private Schema(String userId, String password, String databaseId, String order, boolean interAct, String connectionString) {
this.connectionString = connectionString;
this.userId = userId;
this.password = password;
this.displayId = databaseId;
this.order = order;
this.interAct = interAct;
}
/**
* Return a specific Schema for a give Schema database id
*/
public static final Schema getSchema(String displayId) {
for (Schema schema : values()) {
if (displayId.equals(schema.displayId)) {
return schema;
}
}
return null;
}
public String getConnectionString() {
return connectionString;
}
public String getUserId() {
return userId;
}
}