How to get batch commands to work - Mailing list pgsql-jdbc

From Björn Bergenheim
Subject How to get batch commands to work
Date
Msg-id 00d101c63b30$aa7a2b40$635a59d5@plyschmanxp
Whole thread Raw
Responses Re: How to get batch commands to work  (Kris Jurka <books@ejurka.com>)
List pgsql-jdbc
Hello all, I have tried to check out the archive since this seems like a question I shouldnt be the first to ask, but I have come up with nothing. I'm doing a massive insert batch, and thought that you could add a few insert to a batch, and then execute the batch, and then continue adding more inserts, and then run the batch, and so on. However, when I try to do this, I get an exception when I try to populate my prepared statement after I have run the executeBatch() for the first time. The eception I get is "Parameter index out of range":
 
Caused by: Parameter index out of range.
 at org.postgresql.jdbc1.AbstractJdbc1Statement.bind(AbstractJdbc1Statement.java:1743)
 at org.postgresql.jdbc1.AbstractJdbc1Statement.setInt(AbstractJdbc1Statement.java:782)
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
 at java.lang.reflect.Method.invoke(Method.java:585)
 at org.postgresql.ds.common.PooledConnectionImpl$StatementHandler.invoke(PooledConnectionImpl.java:467)
 at $Proxy2.setInt(Unknown Source)
 at com.frontvision.nemo2.utilities.hostip.Importer.insertCountries(Importer.java:287)
 at com.frontvision.nemo2.utilities.hostip.Importer.read(Importer.java:216)
 ... 15 more
 
I have a pstmt which I use, and in a loop adds stuff to it like this:
    pstmt.setInt(1,countryId);
   pstmt.setString(2, countryName);
   pstmt.setString(3, shortContryName);
   pstmt.addBatch();
 
then after say 200 of these batches, I do a
if(counter%200 = 0)
{
    pstmt.executeBatch();
}
and then continue to add to the batch. Have I got this completly wrong, ei is this not the correct way to do it, or is there something fishy with my db or jdcb drivers? I'm using win8.0 version and jdbc 8.0-314,jdbc3.jar
 
/Bjorn

pgsql-jdbc by date:

Previous
From: Ali Vatani
Date:
Subject: postgres PreparedStatement commented ? problem PSQLException: No value specified for parameter ...
Next
From: Markus Schaber
Date:
Subject: Re: Retrieve Query