"Invalid data conversion" 在 DB2 中使用准备好的语句和批处理

"Invalid data conversion" in DB2 with prepared statements and batch

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

        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 条记录,我会收到以下错误:

Invalid data conversion: Requested conversion would result in a loss of precision of 32768. ERRORCODE=-4461, SQLSTATE=42815

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

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