Thread: IllegalArgumentException in batch execution

IllegalArgumentException in batch execution

From
Altaf Malik
Date:
All,
 I have noticed a strange behavior in jdbc. Consider the following code:

/*
          CREATE TABLE public.testtbl (
            a  integer,
            b  character varying(50));
         */
        Class.forName("org.postgresql.Driver");
        Connection con = DriverManager.getConnection("jdbc:postgresql://localhost:5432/postgres?loglevel=0","postgres","postgres");

        PreparedStatement stmt = con.prepareStatement("insert into testtbl values(?,?)");

        for (int i=0; i< 400;i++) {
            stmt.setInt(1, 110);
            if (i==0)
                stmt.setString(2, "Hello\0 Mr.X");
            else
                stmt.setString(2, "Hello Mr.X");
            stmt.addBatch();
        }

        try{
            stmt.executeBatch();
        } catch(Exception exp){
            stmt.clearBatch();
            exp.printStackTrace();
        }
       
        for (int i=0; i< 400;i++) {
            stmt.setInt(1, 10);
            stmt.setString(2, "Hello Mr.X");
            stmt.addBatch();
        }
        stmt.executeBatch();

When I execute this code, I get Two errors:

(1):
java.lang.IllegalArgumentException: org.postgresql.util.PSQLException: Zero bytes may not occur in string parameters.
        at org.postgresql.core.v3.SimpleParameterList.toString(SimpleParameterList.java:163)
        at org.postgresql.core.v3.SimpleQuery.toString(SimpleQuery.java:46)
        at org.postgresql.jdbc2.AbstractJdbc2Statement$BatchResultHandler.handleError(AbstractJdbc2Statement.java:2586)
        at org.postgresql.core.v3.QueryExecutorImpl$ErrorTrackingResultHandler.handleError(QueryExecutorImpl.java:348)
        at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1796)
        at org.postgresql.core.v3.QueryExecutorImpl.sendQuery(QueryExecutorImpl.java:1055)
        at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:398)
        at org.postgresql.jdbc2.AbstractJdbc2Statement.executeBatch(AbstractJdbc2Statement.java:2727)
        at Main.main(Main.java:28)

This error is fine because I am adding an invalid character. Here is 2nd error:
(2)
java.lang.IndexOutOfBoundsException: Index: 145, Size: 145
        at java.util.ArrayList.RangeCheck(ArrayList.java:546)
        at java.util.ArrayList.get(ArrayList.java:321)
        at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1680)
        at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:407)
        at org.postgresql.jdbc2.AbstractJdbc2Statement.executeBatch(AbstractJdbc2Statement.java:2727)
        at Main.main(Main.java:39)

If I remove the first error, the second error is automatically removed. To me, it seems that this is some after effect of the first error. What is wrong here?

Regards,
Altaf Malik

Re: IllegalArgumentException in batch execution

From
Kris Jurka
Date:

On Tue, 8 Jun 2010, Altaf Malik wrote:

> All,
>  I have noticed a strange behavior in jdbc. Consider the following code:
>
>         PreparedStatement stmt = con.prepareStatement("insert into testtbl
> values(?,?)");
>
>             stmt.setInt(1, 110);
>             stmt.setString(2, "Hello\0 Mr.X");
>             stmt.addBatch();
>             stmt.executeBatch();
>
> When I execute this code, I get Two errors:
>
> (1):
> java.lang.IllegalArgumentException: org.postgresql.util.PSQLException: Zero
> bytes may not occur in string parameters.

This has been fixed in CVS.

Kris Jurka