2013-10-11 183 views
2

我正在編寫一個程序,它將數據從一個模式中提取出來,重新構造數據以適合新模式,然後將數據插入到具有新模式的新數據庫中。問題是,在我的測試代碼中,最後一條記錄沒有被插入到新數據庫中。插入跳過記錄集中的最後一條記錄

我附上了一個大大簡化的下面的代碼版本,但仍然重現了這個問題。任何人都可以告訴我如何解決以下問題,以便將記錄集中的所有記錄插入到目標數據庫中?目前,下面的不正確地打印出最後一個記錄的System.out.println,但還沒有那最後記錄不存在目標表算賬:

static void migrateDataTest(){ 
    try { 
     Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 
     Connection sourceConn = DriverManager.getConnection("jdbc:odbc:source_data_test"); 
     Statement st = sourceConn.createStatement(); 
     Connection destinationConn = DriverManager.getConnection("jdbc:odbc:receive_data_test"); 
     int ClientNumber; String ClientsLastName; String ClientsFirstName; 
     ResultSet rest = st.executeQuery("SELECT ClientNumber, ClientsLastName, ClientsFirstName FROM sourceTable"); 
     PreparedStatement ps5 = null; 
     while(rest.next()){ 
      ClientNumber = rest.getInt(1); 
      ClientsLastName = rest.getString(2); 
      ClientsFirstName = rest.getString(3); 
      System.out.println(ClientNumber+", "+ClientsLastName+", "+ClientsFirstName); 
      ps5 = destinationConn.prepareStatement(
       "INSERT INTO destinationTable (" 
       + "ClientNumber, FirstName, LastName) VALUES (?, ?, ?)" 
      ); 
      ps5.setInt(1, ClientNumber); 
      ps5.setString(2, ClientsFirstName); 
      ps5.setString(3, ClientsLastName); 
      ps5.executeUpdate(); 
      destinationConn.commit(); 
     } 
     ps5.close(); 
    } 
catch (ClassNotFoundException cnfe){cnfe.printStackTrace();} 
catch (SQLException e) {e.printStackTrace();} 
} 

編輯:

按LOKESH的要求,我將整個代碼塊放在下面創建這個錯誤。我再次運行它以確認它在system.out.println中打印記錄30,但是目標表不包含記錄號30.使用system.out.println打印出跳過的記錄會導致我相信下面的代碼包含錯誤:

static void migrateDataTest(){ 
    try { 
     Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 
     Connection sourceConn = DriverManager.getConnection("jdbc:odbc:source_test"); 
     Statement st = sourceConn.createStatement(); 
     Connection destinationConn = DriverManager.getConnection("jdbc:odbc:receive_data_test"); 
     int ClientNumber; 
     String ClientsLastName; 
     String ClientsFirstName; 
     String ClientsMiddleInitial; 
     Date DOB; 
     int GenderNumber; 
     int RaceNumber; 
     ResultSet rest = st.executeQuery("SELECT ClientNumber, ClientsLastName, ClientsFirstName, ClientsMiddleInitial, DOB, GenderNumber, RaceNumber FROM sourceTable"); 
     PreparedStatement ps5 = null; 
     while(rest.next()){ 
      ClientNumber = rest.getInt(1); 
      ClientsLastName = rest.getString(2); 
      ClientsFirstName = rest.getString(3); 
      ClientsMiddleInitial = rest.getString(4); 
      DOB = rest.getDate(5); 
      GenderNumber = rest.getInt(6); 
      RaceNumber = rest.getInt(7); 
      System.out.println(ClientNumber+", "+ClientsLastName+", "+ClientsFirstName+", "+ClientsMiddleInitial+", "+DOB+", "+GenderNumber+", "+RaceNumber); 
      ps5 = destinationConn.prepareStatement(
       "INSERT INTO destinationTable (" 
       + "ClientNumber, FirstName, MiddleInitial, LastName, DOB, GenderNumber, RaceNumber) " 
       +"VALUES (?, ?, ?, ?, ?, ?, ?)" 
      ); 
      ps5.setInt(1, ClientNumber); 
      ps5.setString(2, ClientsFirstName); 
      ps5.setString(3, ClientsMiddleInitial); 
      ps5.setString(4, ClientsLastName); 
      ps5.setDate(5, DOB); 
      ps5.setInt(6, GenderNumber); 
      ps5.setInt(7, RaceNumber); 
      ps5.executeUpdate(); 
      destinationConn.commit(); 
     } 
     ps5.close(); 
    } 
    catch (ClassNotFoundException cnfe){cnfe.printStackTrace();} 
    catch (SQLException e) {e.printStackTrace();} 
} 
+0

代碼是OK我想,是System.out.println打印什麼東西?任何exceptiosn? –

+0

@ user2310289 System.out.println打印每個記錄,包括最後一條記錄,這些記錄似乎永遠不會在目標表中結束。 – CodeMed

+0

1,2,4可以嗎?你只插入3件東西..'ps5.setString(4,..)' – nachokk

回答

0

的解決方案,奇怪的是,是創造,那就是未能插入記錄其最終值一前一後執行額外準備好的聲明。一旦我添加了一個額外的準備好的聲明後,第一個開始始終插入其所有值。

看起來像java代碼中的一些細微差別,可能在我上面發佈的代碼示例中缺少代碼示例。