我正在开发一个功能,我应该从 oracle 数据库中获取数据并将其插入到使用 Java 编程的 MSSQL Server 2008 数据库中。(我知道还有其他选择,如 oracle 金门,链接服务器。但这就是我被问到的去做)。我能够从 oracle 服务器获取数据并将其插入 sql 服务器。但问题在于冗余。即,每当我触发或运行程序时,就会插入重复的记录,因为我应该每 6、12、24 小时执行一次。我不希望它发生。那么我该如何避免它,因为源数据库具有不同的连接而目标数据库是不同的连接。
以下是代码。我希望您能帮我解决避免插入重复数据的问题。
/*TO Retrieve data from oracle database and insert it into sql server*/
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Properties;
public class states {
public static void main(String[] args) {
String statecode, statename;
try {
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver")
.newInstance();
// Connecting to Oracle
Connection oracleconn = DriverManager.getConnection(
"jdbc:oracle:thin:@ipaddress:1521:orcl",
"uname", "pwd");
// Connecting to SQL SERVER
Connection sqlconn = DriverManager
.getConnection("jdbc:sqlserver://localhost:1433;databaseName=dbname;user=sa;password=pwd;");
System.out.println("connected");
// create Statement for sql and oracle
/*
* A Statement is an interface that represents a SQL statement. You
* execute Statement objects, and they generate ResultSet objects,
* which is a table of data representing a database result set. You
* need a Connection object to create a Statement object.
*/
Statement oraclestatement = oracleconn.createStatement();
Statement sqlstatement = sqlconn.createStatement();
/*
* The ResultSet interface provides methods for retrieving and
* manipulating the results of executed queries, and ResultSet
* objects can have different functionality and characteristics.
*/
ResultSet oracle_rs = oraclestatement
.executeQuery("select substr(TRIIDTX,1,2),TRINAMETX from T_TRISTATE WHERE TRIIDTX IS NOT NULL AND TRINAMETX IS NOT NULL AND TRINAMETX not LIKE '%''%' ESCAPE '/'");
System.out.println("TRICODETX TRINAMETX \n");
// String dummytable="tbldummystate";
while (oracle_rs.next()) {
System.out.println(" " + oracle_rs.getString(1) + " "
+ oracle_rs.getString(2) + " ");
// converting the string value into integer value
statecode = oracle_rs.getString(1);
statename = oracle_rs.getString(2);
sqlstatement
.executeUpdate("insert into tblStates(StateCode,StateName) values('"
+ statecode + "','" + statename + "')");
// sqlstatement.execute ();
}// end of while loop
} catch (Exception e) {
e.printStackTrace();
}
}// end of
}