{
getHostDataTo();
}
});
jButtonClone = new JButton4j(Common.icon_clone);
desktopPane.add(jButtonClone);
jButtonClone.setText("Clone Database");
jButtonClone.setBounds(129, 444, 160, 36);
jButtonClone.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
String schemaFrom = "";
String schemaTo = "";
JDBControl ctrl;
// Source & Destination must be different Hosts//
setStatusBarText("Validating selected hosts");
if (hostIDFrom.equals(hostIDTo) == false)
{
// Check we can connect to Source //
setStatusBarText("Connecting to source...");
if (hstFrom.connect(sessionFrom, hostIDFrom))
{
// Check we can connect to Destination //
setStatusBarText("Connecting to destination...");
if (hstTo.connect(sessionTo, hostIDTo))
{
// Check Application Schema Versions are the
// same in Source and Destination //
setStatusBarText("Checking schema versions...");
ctrl = new JDBControl(hostIDFrom, sessionFrom);
schemaFrom = ctrl.getKeyValue("SCHEMA VERSION");
ctrl = new JDBControl(hostIDTo, sessionTo);
schemaTo = ctrl.getKeyValue("SCHEMA VERSION");
if (schemaFrom.equals(schemaTo))
{
// OK //
setStatusBarText("Getting source table names...");
int tableCountFrom = 0;
JDBStructure strucFrom = new JDBStructure(hostIDFrom, sessionFrom);
LinkedList<String> tablesFrom = strucFrom.getTableNames();
tableCountFrom = tablesFrom.size();
progressBar.setMinimum(1);
progressBar.setMaximum(tableCountFrom);
setStatusBarText("Getting destination table names...");
int tableCountTo = 0;
JDBStructure strucTo = new JDBStructure(hostIDTo, sessionTo);
LinkedList<String> tablesTo = strucTo.getTableNames();
tableCountTo = tablesTo.size();
if (tableCountFrom == tableCountTo)
{
String table = "";
for (int tf = 0; tf < tableCountFrom; tf++)
{
table = tablesFrom.get(tf);
progressBar.setValue(tf+1);
progressBar.paintImmediately(progressBar.getVisibleRect());
if (tablesTo.contains(table))
{
// GET FIELDS FOR CURRENT TABLE
setStatusBarText("Getting field names for "+table);
LinkedList<JDBField> fieldNames = strucFrom.getFieldNames(table);
JWait.milliSec(100);
// CREATE INSERT STATEMENT FOR
// TARGET DATABASE //
setStatusBarText("Generating insert SQL for "+table);
String insertTable = "insert into " + table;
String insertFields = "";
String insertPlaceMarkers = "";
String comma = "";
for (int temp = 0; temp < fieldNames.size(); temp++)
{
if (temp == 0)
{
comma = "";
} else
{
comma = ",";
}
insertFields = insertFields + comma + fieldNames.get(temp).getfieldName();
insertPlaceMarkers = insertPlaceMarkers + comma + "?";
}
String insertStatement = insertTable + " (" + insertFields + ") values (" + insertPlaceMarkers + ")";
// SELECT ALL SOURCE RECORDS //
setStatusBarText("Copying table "+table);
Long recordsCopied = (long) 0;
try
{
hstFrom.getConnection(sessionFrom).setAutoCommit(false);
hstTo.getConnection(sessionTo).setAutoCommit(false);
PreparedStatement truncateData = hstTo.getConnection(sessionTo).prepareStatement("truncate table " + table);
PreparedStatement destinationData = hstTo.getConnection(sessionTo).prepareStatement(insertStatement);
PreparedStatement sourceData = hstFrom.getConnection(sessionFrom).prepareStatement("select * from " + table);
sourceData.setFetchDirection(ResultSet.FETCH_FORWARD);
destinationData.setFetchDirection(ResultSet.FETCH_FORWARD);
sourceData.setFetchSize(1);
destinationData.setFetchSize(1);
truncateData.setFetchSize(1);
ResultSet sourceResultset = sourceData.executeQuery();
truncateData.execute();
truncateData.close();
truncateData = null;
while (sourceResultset.next())
{
for (int fldfrom = 0; fldfrom < fieldNames.size(); fldfrom++)
{
JDBField field = fieldNames.get(fldfrom);
if (field.getfieldType().toLowerCase().equals("varchar"))
{
String value;
value = sourceResultset.getString(field.getfieldName());
destinationData.setString(fldfrom + 1, value);
//System.out.println("Table = \"" + table + "\" Field = \"" + field.getfieldName() + "\" Value = \"" + value + "\"");
}
if ((field.getfieldType().toLowerCase().equals("decimal")) | (field.getfieldType().toLowerCase().equals("numeric")) | (field.getfieldType().toLowerCase().equals("float")))
{
BigDecimal value;
value = sourceResultset.getBigDecimal(field.getfieldName());
destinationData.setBigDecimal(fldfrom + 1, value);
//System.out.println("Table = \"" + table + "\" Field = \"" + field.getfieldName() + "\" Value = \"" + JUtility.replaceNullObjectwithBlank(value).toString() + "\"");
}
if (field.getfieldType().toLowerCase().equals("datetime"))
{
Timestamp value;
value = sourceResultset.getTimestamp(field.getfieldName());
destinationData.setTimestamp(fldfrom + 1, value);
//System.out.println("Table = \"" + table + "\" Field = \"" + field.getfieldName() + "\" Value = \""
// + JUtility.replaceNullObjectwithBlank(value).toString() + "\"");
}
if ((field.getfieldType().toLowerCase().equals("int")) | (field.getfieldType().toLowerCase().equals("bigint")))
{
Integer value;
value = sourceResultset.getInt(field.getfieldName());
destinationData.setInt(fldfrom + 1, value);
//System.out.println("Table = \"" + table + "\" Field = \"" + field.getfieldName() + "\" Value = \""
// + JUtility.replaceNullObjectwithBlank(value).toString() + "\"");
}
field=null;
}
try
{
destinationData.execute();
hstTo.getConnection(sessionTo).commit();
destinationData.clearParameters();
recordsCopied++;
} catch (Exception ex)
{
System.out.println(ex.getMessage());
}
}
sourceResultset.close();
sourceResultset = null;
destinationData.close();
destinationData = null;
sourceData.close();
sourceData = null;
setStatusBarText("Copying complete");
} catch (SQLException e)
{
labelCommand.setText("Error reading "+table);
}
} else
{
labelCommand.setText("Table "+table+" does not exist in destination");
}
}
} else
{
labelCommand.setText("Number of tables mismatch "+String.valueOf(tableCountFrom)+"/"+String.valueOf(tableCountTo));
}
} else
{
labelCommand.setText("Schema version mismatch "+String.valueOf(schemaFrom)+"/"+String.valueOf(schemaTo));
}
} else
{
labelCommand.setText("Cannot connect to destination.");
}
hstFrom.disconnect(Common.sessionID);
} else
{
labelCommand.setText("Cannot connect to source.");
}
} else
{
labelCommand.setText("Cannot clone to self.");
}
}
});
jButtonClose = new JButton4j(Common.icon_close);
desktopPane.add(jButtonClose);
jButtonClose.setText("Close");
jButtonClose.setBounds(293, 444, 160, 36);
jButtonClose.addActionListener(new ActionListener()
{