"数据转换无效"在DB2中准备好语句和批处理

时间:2016-04-03 01:54:29

标签: database jdbc db2 prepared-statement batch-processing

我使用JDBC创建临时表,向其中添加记录(使用预准备语句和批处理),然后将所有内容传输到另一个表:

        String createTemporaryTable = "declare global temporary table temp_table (RECORD smallint,RANDOM_INTEGER integer,RANDOM_FLOAT float,RANDOM_STRING varchar(600)) ON COMMIT PRESERVE ROWS in TEMP";

        statement.execute(createTemporaryTable);

        String sql = "INSERT INTO session.temp_table (RECORD,RANDOM_INTEGER,RANDOM_FLOAT,RANDOM_STRING) VALUES (?,?,?,?)";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);

        float f = 0.7401298f;
        Integer integer = 123456789;
        String string = "This is a string that will be inserted into the table over and over again.";

        // add however many random records you want to the temporary table
        int numberOfRecordsToInsert = 35000;

        for (int i = 0; i < numberOfRecordsToInsert; i++) {

            preparedStatement.setInt(1, i);
            preparedStatement.setInt(2, integer);
            preparedStatement.setFloat(3, (float) f);
            preparedStatement.setString(4, string);
            preparedStatement.addBatch();

        }

        preparedStatement.executeBatch();

        // transfer everything from the temporary table just created to the main table
        String transferFromTempTableToMain = "insert into main_table select * from session.temp_table";
        statement.execute(transferFromTempTableToMain);

在此示例中,此工作最多可达30000条记录。但是,如果我要插入说35000条记录,我会收到以下错误:

  

无效的数据转换:请求的转换会导致丢失   精度为32768. ERRORCODE = -4461,SQLSTATE = 42815

1 个答案:

答案 0 :(得分:1)

问题是字段RECORDsmallintsmallint是带符号的16位整数,范围为-32768到32767。

因此不允许插入一个32768的int值,因为它不适合。您需要将记录声明为INTEGER