我正在研究一種功能,我應該從oracle數據庫中獲取數據並將其插入MSSQL Server 2008數據庫使用Java編程(我知道有其他選擇像oracle金門,鏈接server.but這就是我要求做的)。我能夠從oracle服務器獲取數據並將其插入到sql server中。但問題在於裁員。即無論何時觸發或運行程序,都會插入重複記錄,因爲我應該每隔6,12,24小時執行一次。我不希望它發生。所以我如何避免它作爲源數據庫有不同的連接和目標數據庫是不同的連接。從Java程序中插入MS SQL Server 2008時避免重複的記錄
以下是密碼。我會喜歡你的幫助,避免重複插入數據。
/*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
}
,如果你不希望消除使用Java你可以宣佈在Access數據庫主鍵索引,趕上在代碼中的異常重複。 – haki
我想通過java代碼來做到這一點,問題是我不能改變目標表的客戶端數據庫中的任何東西。它沒有任何限制。 – Mangesh