用java代码来定时增量同步数据库表的实现代码
时间: 2024-03-11 11:50:47 浏览: 22
以下是一个用Java代码实现定时增量同步数据库表的示例:
```java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
public class IncrementalSync {
private static final String DB_URL = "jdbc:mysql://localhost:3306/mydb";
private static final String DB_USER = "root";
private static final String DB_PASSWORD = "mypassword";
private static final String SOURCE_TABLE = "source_table";
private static final String DESTINATION_TABLE = "destination_table";
private static final String LAST_SYNC_COLUMN = "last_sync";
public static void main(String[] args) {
Timer timer = new Timer();
timer.scheduleAtFixedRate(new IncrementalSyncTask(), 0, 60 * 60 * 1000);
}
private static class IncrementalSyncTask extends TimerTask {
public void run() {
Connection sourceConn = null;
Connection destConn = null;
PreparedStatement selectStmt = null;
PreparedStatement insertStmt = null;
ResultSet rs = null;
try {
sourceConn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
destConn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
// Get the timestamp of the last sync from the destination table
Timestamp lastSync = null;
String selectLastSyncSql = "SELECT " + LAST_SYNC_COLUMN + " FROM " + DESTINATION_TABLE;
PreparedStatement selectLastSyncStmt = destConn.prepareStatement(selectLastSyncSql);
ResultSet lastSyncRs = selectLastSyncStmt.executeQuery();
if (lastSyncRs.next()) {
lastSync = lastSyncRs.getTimestamp(1);
}
lastSyncRs.close();
selectLastSyncStmt.close();
// Select all rows from the source table that have been updated since the last sync
String selectSql = "SELECT * FROM " + SOURCE_TABLE + " WHERE last_updated > ?";
selectStmt = sourceConn.prepareStatement(selectSql);
selectStmt.setTimestamp(1, lastSync);
rs = selectStmt.executeQuery();
// Insert the updated rows into the destination table
String insertSql = "INSERT INTO " + DESTINATION_TABLE + " VALUES (?, ?, ?)";
insertStmt = destConn.prepareStatement(insertSql);
while (rs.next()) {
int id = rs.getInt("id");
String name = rs.getString("name");
Timestamp lastUpdated = rs.getTimestamp("last_updated");
insertStmt.setInt(1, id);
insertStmt.setString(2, name);
insertStmt.setTimestamp(3, lastUpdated);
insertStmt.executeUpdate();
}
// Update the timestamp of the last sync in the destination table
String updateSql = "UPDATE " + DESTINATION_TABLE + " SET " + LAST_SYNC_COLUMN + " = ?";
PreparedStatement updateStmt = destConn.prepareStatement(updateSql);
updateStmt.setTimestamp(1, new Timestamp(System.currentTimeMillis()));
updateStmt.executeUpdate();
updateStmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try { if (rs != null) rs.close(); } catch (SQLException e) { }
try { if (selectStmt != null) selectStmt.close(); } catch (SQLException e) { }
try { if (insertStmt != null) insertStmt.close(); } catch (SQLException e) { }
try { if (sourceConn != null) sourceConn.close(); } catch (SQLException e) { }
try { if (destConn != null) destConn.close(); } catch (SQLException e) { }
}
}
}
}
```
该示例使用了Java的Scheduler API来定期执行一个任务,这个任务会连接源数据库和目标数据库,在目标数据库中记录上一次同步的时间戳,然后查询源数据库中有哪些行是在上一次同步之后更新过的,将这些行插入到目标数据库中,并更新目标数据库中的时间戳。可以根据需要修改源数据库、目标数据库、表名、时间戳列名等参数。