生成一段java代码,用于同步oracle数据到mysql,mysql表中有字段保存oracle表的主键,凭借主键增删改数据
时间: 2023-04-06 07:00:24 浏览: 121
以下是一段Java代码,用于同步Oracle数据到MySQL,MySQL表中有字段保存Oracle表的主键,凭借主键增删改数据:
```java
import java.sql.*;
public class OracleToMysqlSync {
public static void main(String[] args) {
Connection oracleConn = null;
Connection mysqlConn = null;
PreparedStatement oracleStmt = null;
PreparedStatement mysqlInsertStmt = null;
PreparedStatement mysqlUpdateStmt = null;
PreparedStatement mysqlDeleteStmt = null;
ResultSet oracleRs = null;
try {
// 连接Oracle数据库
oracleConn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "username", "password");
// 连接MySQL数据库
mysqlConn = DriverManager.getConnection("jdbc:mysql://localhost:3306/dbname", "username", "password");
// 查询Oracle表中的数据
oracleStmt = oracleConn.prepareStatement("SELECT * FROM oracle_table");
oracleRs = oracleStmt.executeQuery();
// 遍历查询结果
while (oracleRs.next()) {
// 获取主键值
int id = oracleRs.getInt("id");
// 查询MySQL表中是否存在该主键值的记录
PreparedStatement mysqlQueryStmt = mysqlConn.prepareStatement("SELECT * FROM mysql_table WHERE id = ?");
mysqlQueryStmt.setInt(1, id);
ResultSet mysqlRs = mysqlQueryStmt.executeQuery();
// 如果MySQL表中不存在该主键值的记录,则插入一条新记录
if (!mysqlRs.next()) {
if (mysqlInsertStmt == null) {
mysqlInsertStmt = mysqlConn.prepareStatement("INSERT INTO mysql_table (id, column1, column2) VALUES (?, ?, ?)");
}
mysqlInsertStmt.setInt(1, id);
mysqlInsertStmt.setString(2, oracleRs.getString("column1"));
mysqlInsertStmt.setString(3, oracleRs.getString("column2"));
mysqlInsertStmt.executeUpdate();
}
// 如果MySQL表中存在该主键值的记录,则更新该记录
else {
if (mysqlUpdateStmt == null) {
mysqlUpdateStmt = mysqlConn.prepareStatement("UPDATE mysql_table SET column1 = ?, column2 = ? WHERE id = ?");
}
mysqlUpdateStmt.setString(1, oracleRs.getString("column1"));
mysqlUpdateStmt.setString(2, oracleRs.getString("column2"));
mysqlUpdateStmt.setInt(3, id);
mysqlUpdateStmt.executeUpdate();
}
}
// 删除MySQL表中不存在于Oracle表中的记录
if (mysqlDeleteStmt == null) {
mysqlDeleteStmt = mysqlConn.prepareStatement("DELETE FROM mysql_table WHERE id = ?");
}
PreparedStatement mysqlQueryAllStmt = mysqlConn.prepareStatement("SELECT id FROM mysql_table");
ResultSet mysqlAllRs = mysqlQueryAllStmt.executeQuery();
while (mysqlAllRs.next()) {
int id = mysqlAllRs.getInt("id");
PreparedStatement oracleQueryStmt = oracleConn.prepareStatement("SELECT * FROM oracle_table WHERE id = ?");
oracleQueryStmt.setInt(1, id);
ResultSet oracleRs2 = oracleQueryStmt.executeQuery();
if (!oracleRs2.next()) {
mysqlDeleteStmt.setInt(1, id);
mysqlDeleteStmt.executeUpdate();
}
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (oracleRs != null) {
oracleRs.close();
}
if (oracleStmt != null) {
oracleStmt.close();
}
if (mysqlInsertStmt != null) {
mysqlInsertStmt.close();
}
if (mysqlUpdateStmt != null) {
mysqlUpdateStmt.close();
}
if (mysqlDeleteStmt != null) {
mysqlDeleteStmt.close();
}
if (oracleConn != null) {
oracleConn.close();
}
if (mysqlConn != null) {
mysqlConn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
```
阅读全文