1

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!

3
  • @AnkurShanbhag Probably PreparedStatement. Commented Feb 5, 2014 at 7:49
  • Yes its a PreparedStatement Commented Feb 5, 2014 at 7:52
  • See attached Throwable for details - can we have a look at that? Commented Feb 5, 2014 at 7:53

2 Answers 2

6

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

Sign up to request clarification or add additional context in comments.

6 Comments

your suggested method is not available in Java 5. Are you telling for JDK 6 or higher
@Kishore: Yes, it was introduced in Java 6. If you're still using an ancient version of Java, I suggest you attempt to upgrade - it'll make your life simpler in various ways.
@JonSkeet Sir, could you please tell me the proper solution of above problem. Actually I am restricted to JDK 1.5. So is there any solid solution for that in JDK1.5. Thanks!
@Siddharth: Well you could read from the reader into memory first - either throwing away the data and just counting characters, or storing it in a StringWriter that you can then convert to a String later. Either way, that would let you count the right number of characters.
@Siddharth: "Still not working" doesn't give nearly enough information, and currently you're losing line endings. I didn't suggest reading a line at a time - I suggest reading a block at a time (using read(char[])) and write the number of characters you read into the StringWriter. Then at the end, you can convert the whole thing into a string - and probably just call setString.
|
3

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

2 Comments

How is that going to help? You're creating a BufferedReader around a FileReader rather than an InputStreamReader around a FileInputStream. That means the OP can't even specify an encoding, which they should... and doesn't solve the problem of specifying the length in bytes instead of in characters.
@raVan If I implemented your advice then the exception is changed Now the excption is End of stream prematurely reached while reading InputStream, parameter #6. Remaining data has been padded with 0x0. ERRORCODE=-4225, SQLSTATE=null

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.