insert会跳过记录集中的最后一条记录

时间:2013-10-11 01:26:46

标签: java jdbc

我正在编写一个程序,用于从一个模式中提取数据,重新构造数据以适应新模式,然后使用新模式将数据插入到新数据库中。问题是,在我的测试代码中,最后一条记录没有插入到新数据库中。

我附上了下面代码的大大简化版本,但仍然重现了这个问题。任何人都可以告诉我如何解决以下问题,以便记录集中的所有记录都插入目标数据库?目前,下面的内容确实正确地打印出了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();}
}

1 个答案:

答案 0 :(得分:0)

奇怪的是,解决方案是在未能在其记录集中插入最终值的语句之后创建并执行其他预准备语句。之后我添加了一个额外的预备语句,第一个开始一致地插入其所有值。

看起来像我在上面的原始帖子中发布的代码示例中可能缺少的java代码中的一些细微差别。