Question

I am running these lines of code in a Java program

        FileInputStream in = new FileInputStream(file);
        InputStreamReader reader = new InputStreamReader(in);
        int fileLength = (int)file.length();

        pstmt1.setCharacterStream(6, reader, fileLength);

But I got the following Exception in some file(s) but not in all file(s)

Exception: Encountered an IOException reading InputStream, parameter #6. Remaining data has been padded with 0x0. See attached Throwable for details. ERRORCODE=-4225, SQLSTATE=null

Where as I am using DB2 Database and the structure of my table is-

CREATE TABLE SOURCE (
                LIB VARCHAR(10) NOT NULL,
                FILE VARCHAR(10) NOT NULL,
                MBR VARCHAR(10) NOT NULL,
                ATTR VARCHAR(10) NOT NULL,
                SEQ DOUBLE NOT NULL,
                DTA CLOB(5M),
                DAT INTEGER,
                RECN INTEGER,
                UNIQUE (LIB, FILE, MBR, ATTR, SEQ) 
            );

Please help me to recover this Exception.

Thanks in advance!

Was it helpful?

Solution

I suspect the problem is that you're setting the length based on the length of the file in bytes, whereas the parameter to setCharacterStream is meant to be a length in characters.

You're also using the platform default encoding, which is rarely a good idea.

Do you know what encoding the data is in? If it's a fixed-width encoding, you can work out the number of characters from the number of bytes. Otherwise, you'd be better off calling the overload which doesn't specify the length:

pstmt1.setCharacterStream(6, reader);

(But do change your code to specify the encoding when you create the InputStreamReader.)

OTHER TIPS

Adding to Jon's answer mentioned above. Try and use Reader objects here.

Reader reader = (Reader) new BufferedReader(new FileReader(file));
int fileLength = (int)file.length();
pstmt1.setCharacterStream(6, reader, fileLength);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top