pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
How to get batch commands to work
2+ messages / 2 participants
[nested] [flat]

* How to get batch commands to work
@ 2006-02-26 23:59  Björn Bergenheim <bjornJmagic@kenetworks.se>
  0 siblings, 1 reply; 2+ messages in thread

From: Björn Bergenheim @ 2006-02-26 23:59 UTC (permalink / raw)
  To: 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

^ permalink  raw  reply  [nested|flat] 2+ messages in thread

* Re: How to get batch commands to work
@ 2006-02-27 22:14  Kris Jurka <books@ejurka.com>
  parent: Björn Bergenheim <bjornJmagic@kenetworks.se>
  0 siblings, 0 replies; 2+ messages in thread

From: Kris Jurka @ 2006-02-27 22:14 UTC (permalink / raw)
  To: Björn Bergenheim <bjornJmagic@kenetworks.se>; +Cc: pgsql-jdbc



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

Attachments:

BatchTest.java (text/plain, 770B) download import java.sql.*; public class BatchTest { public static void main(String args[]) throws Exception { Class.forName("org.postgresql.Driver"); Connection conn = DriverManager.getConnection("jdbc:postgresql://localhost:5432/jurka","jurka",""); conn.setAutoCommit(false); Statement stmt = conn.createStatement(); stmt.execute("CREATE TEMP TABLE t (a int primary key)"); PreparedStatement ps = conn.prepareStatement("INSERT INTO t VALUES (?)"); ps.setInt(1,1); ps.addBatch(); ps.setInt(1,2); ps.addBatch(); ps.executeBatch(); ps.setInt(1,3); ps.addBatch(); ps.executeBatch(); ps.close(); ResultSet rs = stmt.executeQuery("SELECT a FROM t"); while (rs.next()) { System.out.println(rs.getInt(1)); } } }
^ permalink raw reply [nested|flat] 2+ messages in thread


end of thread, other threads:[~2006-02-27 22:14 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2006-02-26 23:59 How to get batch commands to work Björn Bergenheim <bjornJmagic@kenetworks.se>
2006-02-27 22:14 ` Kris Jurka <books@ejurka.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox