我已实现此代码但不起作用。任何人都可以告诉我错误的原因我无法找到它:(这是代码:
PreparedStatement stmA = connsqlite.prepareStatement("SELECT * FROM dbTable");
PreparedStatement stmB = connphpmyadmin.prepareStatement("INSERT INTO codes VALUES ('"??????????"')");
stmA.executeQuery("DROP TABLE dbTable");
stmB.executeUpdate();
connsqlite.close();
connphpmyadmin.close();
如您所见,我希望同步2个不同的数据库,一个本地服务器和一个服务器。同步完成后,我想从本地数据库中删除表。 我认为答案是在" ??????"我已经提出但我不知道该写些什么。如果有人知道请帮助我理解。谢谢!
答案 0 :(得分:0)
这只是一个如何做到这一点的例子。记住?
标记是要传递的一个参数/值。因此,您需要根据自己的目的修改查询。
我不知道你的表有什么类型的列,所以如果你需要修改它。
//Representation of single item in table
class DbTableItem {
private String value1;
private String value2;
private String value3;
//... Whatever field that required to exchange information between 2 tables
public String getValue1() {
return value1;
}
public void setValue1(String value1) {
this.value1 = value1;
}
public String getValue2() {
return value2;
}
public void setValue2(String value2) {
this.value2 = value2;
}
public String getValue3() {
return value3;
}
public void setValue3(String value3) {
this.value3 = value3;
}
}
Connection connection1 =//your connection to local db;
Connection connection2 =//your connection to another db;
PreparedStatement stmA = connection.prepareStatement("SELECT * FROM dbTable");
//Executing query to retreive data
ResultSet resultSet = stmA.executeQuery();
List<DbTableItem> dbTableList = new ArrayList<>();
//Setting items and adding to list
while (resultSet.next()){
DbTableItem dbTableItem = new DbTableItem();
dbTableItem.setValue1(resultSet.getString("COLUMN_NAME"));
dbTableItem.setValue2(resultSet.getString("COLUMN_NAME"));
dbTableItem.setValue2(resultSet.getString("COLUMN_NAME"));
dbTableList.add(dbTableItem);
}
//Preparing next query for batch process
PreparedStatement stmB = connection2.prepareStatement("INSERT INTO codes VALUES (?,?,?)");
//Adding to batch
for(DbTableItem dbTableItem: dbTableList) {
stmB.setObject(1, dbTableItem.getValue1());
stmB.setObject(2, dbTableItem.getValue2());
stmB.setObject(2, dbTableItem.getValue3());
//And so on
stmB.addBatch();
}
//Executing batch with query
stmB.executeBatch();
//Droping table
//Closing connection1 and connection2