JAVA + Mysql PreparedStatement.setString()ArrayStoreException

时间:2012-03-12 08:43:53

标签: java mysql jdbc prepared-statement

我在我的代码中得到了这个ArrayStoreException,用于将记录从一个mysql表加载到另一个mysql表。我试图截断目标表并再次运行我的代码,似乎它随机遇到了所述异常。

当我在单元测试期间遇到ArrayStoreException时,我打算实现一个基于(Spring)JdbcTemplate的DAO。我试图用普通的JDBC代码重新创建它,但仍然遇到错误。

对于我的DDL: 除了brthdate和birthdate之外,所有列都是varchar类型,类型为Date。 DEFAULT CHARSET = utf8

我的代码段:

 employeesSql = "select id_no, " + 
                "lastname, " + 
                "frstname, " + 
                "mdlename, " +
                "brthdate,sex, " +
                "sss_no, " +
                "tin_no " + 
                "from lms.pms_empf " +
                "where length(trim(id_no)) > 0 " + 
                "and id_no <> '000000' " + 
                " ORDER BY id_no";
        employeeSql = "select count(*) " 
                + "from dim_employees " 
                + "where id_no=?";
        updateSql = "update dim_employees " + 
                "set last_name = ?, " +
                "first_name = ?, " +
                "middle_name = ?, " +
                "birthdate = ?, " +
                "sex = ?, " +
                "sss_no = ?, " +
                "tin_no = ? " +
                "where id_no = ?";
        insertSql = "insert into dim_employees " +
                  "(id_no, last_name, first_name, " +
                  "middle_name,birthdate, sex, sss_no, tin_no) " +
                  "values (?,?,?,?,?,?,?,?)";

        employeesStmt = con.createStatement();
        employeeStmt = con.prepareStatement(employeeSql);
        updateStmt = con.prepareStatement(updateSql);
        insertStmt = con.prepareStatement(insertSql);

        employeesCursor = employeesStmt.executeQuery(employeesSql);

        while (employeesCursor.next()) {

            idNo = new String(employeesCursor.getBytes(1), "UTF-8");
            lastName =  new String(employeesCursor.getBytes(2), "UTF-8");
            firstName = new String(employeesCursor.getBytes(3), "UTF-8");
            middleName = new String(employeesCursor.getBytes(4), "UTF-8");
            birthDate = employeesCursor.getDate(5);
            sex = new String(employeesCursor.getBytes(6), "UTF-8");
            sssNo = new String(employeesCursor.getBytes(7), "UTF-8");
            tinNo = new String(employeesCursor.getBytes(8), "UTF-8");


            employeeStmt.setString(1, idNo);
            employeeCursor = employeeStmt.executeQuery();

            while (employeeCursor.next()) {

                if (employeeCursor.getInt(1) > 0) {
                    //update
                    updateStmt.setString(1, lastName);
                    updateStmt.setString(2, firstName);
                    updateStmt.setString(3, middleName);
                    updateStmt.setDate(4, birthDate);
                    updateStmt.setString(5, sex);
                    updateStmt.setString(6, sssNo);
                    updateStmt.setString(7, tinNo);
                    updateStmt.setString(8, idNo);

                    updateStmt.executeUpdate();
                    updateStmt.executeUpdate();
                }
                else {
                    //insert
                    insertStmt.setString(1, idNo);

                    insertStmt.setString(2,lastName);
                    insertStmt.setString(3, firstName);
                    insertStmt.setString(4, middleName);
                    insertStmt.setDate(5, birthDate);
                    insertStmt.setString(6, sex);
                    insertStmt.setString(7, sssNo);
***//exception points here**        insertStmt.setString(8, tinNo);

                    insertStmt.executeUpdate();
                    insertStmt.clearParameters();
                }

            }

            employeeStmt.clearParameters();

堆栈追踪:

......
Caused by: java.lang.ArrayStoreException
at java.lang.String.getChars(String.java:854)
at com.mysql.jdbc.PreparedStatement.setString(PreparedStatement.java:4520)
......
    at com.vdc.lmsprocs.EmployeeProc.setEmployees(EmployeeProc.java:135)
......

对不起,我很抱歉。我显然无法解释这里发生的事情。这是我第一次遇到这样的缺陷。

提前致谢。

0 个答案:

没有答案