IllegalArgumentException in batch execution - Mailing list pgsql-jdbc
From | Altaf Malik |
---|---|
Subject | IllegalArgumentException in batch execution |
Date | |
Msg-id | 384952.35231.qm@web110413.mail.gq1.yahoo.com Whole thread Raw |
Responses |
Re: IllegalArgumentException in batch execution
|
List | pgsql-jdbc |
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
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
pgsql-jdbc by date: