JAVA + Mysql PreparedStatement.setString() ArrayStoreException

北慕城南 提交于 2019-12-08 03:20:51

问题


I've been getting this ArrayStoreException in my code for loading records from one mysql table to another. I tried truncating the destination table and running my code again and it seems that it encounters the said exception randomly.

I was going to implement a (Spring) JdbcTemplate based DAO when i've encountered an ArrayStoreException during unit testing. I tried to recreate it with ordinary JDBC code and still encounter the error.

For my DDL: All the columns are of type varchar, except for brthdate and birthdate which are of type Date. DEFAULT CHARSET=utf8

My code snippet:

 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();

Stack trace:

......
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)
......

I'm sorry for the long post. I clearly could not explain well enough what happened here. It's the first time i've encountered such a flaw.

Thanks in advance.

来源:https://stackoverflow.com/questions/9663935/java-mysql-preparedstatement-setstring-arraystoreexception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!