Thread: How to get batch commands to work

How to get batch commands to work

From
Björn Bergenheim
Date:
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

Re: How to get batch commands to work

From
Kris Jurka
Date:

On Mon, 27 Feb 2006, Bj�rn Bergenheim wrote:

> 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":
>

This should work fine as the attached test does for me.  Can you show us a
complete example of something that fails?

Kris Jurka
Attachment