oracle - Avoid Duplicate records while inserting in MS SQL Server 2008 from a java program -


i working on functionality supposed fetch data oracle database , insert mssql server 2008 database using java programming.(i know there other alternatives oracle golden gate,linked server.but asked do). able fetch data oracle server , insert sql server. issue redunduncy. i.e duplicate records inserted whenever trigger or run program, supposed every 6,12,24 hours. don't want happen. how can avoid source database has different connection , target database different connection.

following code . iwould in working around avoid duplicate data inserted.

/*to retrieve data oracle database , insert 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 oracle             connection oracleconn = drivermanager.getconnection(                     "jdbc:oracle:thin:@ipaddress:1521:orcl",                     "uname", "pwd");             // connecting sql server             connection sqlconn = drivermanager                     .getconnection("jdbc:sqlserver://localhost:1433;databasename=dbname;user=sa;password=pwd;");             system.out.println("connected");                // create statement sql , oracle              /*              * statement interface represents sql statement.              * execute statement objects, , generate resultset objects,              * table of data representing database result set.              * need connection object create statement object.              */             statement oraclestatement = oracleconn.createstatement();              statement sqlstatement = sqlconn.createstatement();              /*              * resultset interface provides methods retrieving ,              * manipulating results of executed queries, , resultset              * objects can have different functionality , characteristics.              */             resultset oracle_rs = oraclestatement                     .executequery("select substr(triidtx,1,2),trinametx  t_tristate  triidtx not null , trinametx not null , trinametx  not '%''%' 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 string value integer value                  statecode = oracle_rs.getstring(1);                 statename = oracle_rs.getstring(2);                  sqlstatement                         .executeupdate("insert tblstates(statecode,statename) values('"                                 + statecode + "','" + statename + "')");                  // sqlstatement.execute ();               }// end of while loop          } catch (exception e) {             e.printstacktrace();         }      }// end of } 

maybe using merge it?

something like:

.executeupdate(" merge tblstates target using (values ('"+ statecode + "','" + statename + "'))        source (statecode, statename) on target.statecode = source.statecode when not matched target     insert (statecode, statename) values ('"+ statecode + "','" + statename + "'); )"; 

edit: if want update statename statecode exists in target table if it's changed in source table can add following statement before when not matchedclause:

when matched update set statename = ('" + statename + "')"


Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

c# - must be a non-abstract type with a public parameterless constructor in redis -

ajax - PHP/JSON Login script (Twitter style) not setting sessions -