As far as style goes, I would use a PreparedStatement
for performance AND safety…
PreparedStatement ps = connection.prepareStatement(
“insert into some_table(some_pri_key,some_other_value) values (?,’bob’)”
);
for( int i = 0; i < 400; ++i )
{
ps.setInt( 1, i );
ps.executeUpdate();
connection.commit();
}
You should also be using executeUpdate()
rather than executeQuery(), if you’re doing an update. Also, you
should probably consider not committing after each insert and putting a bunch
of inserts into one commit.
-----Original Message-----
From: Jason Kretzer/STAR BASE
Consulting Inc. [mailto:JKretzer@xxxxxxxxxxxxxxx]
Sent: Monday, May 03, 2004 9:42 AM
To: users@xxxxxxxxxx
Subject: [cinjug-users] odd db2
insert behavior
Good morning all,
I
have a class that populates a db2 database. Here is a basic sample of
what I am doing.
for(int
i=0; i<400; i++)
{
stmt.executeQuery("INSERT INTO SOME_TABLE
(SOME_PRI_KEY, SOME_OTHER_VALUE) VALUES ("+i+",'bob')");
connection.commit();
}
First
of all, how bad is the style of this? Secondly, when I insert like this
and then get the contents of the table, I get something similar to this
SOME_PRI_KEY
SOME OTHER VALUE
385
bob
386
bob
387
bob
388
bob
389
bob
390
bob
391
bob
392
bob
393
bob
394
bob
395
bob
396
bob
397
bob
398
bob
399
bob
1
bob
2
bob
3
bob
4
bob
5
bob
Anyone
know why this is happening? Could it just be a timing issue?
Thanks,
-Jason