aspose file tools*
The moose likes JDBC and the fly likes Multiple insert into Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Multiple insert into "MS Access" database" Watch "Multiple insert into "MS Access" database" New topic
Author

Multiple insert into "MS Access" database

Sameer Ahmed
Greenhorn

Joined: Apr 15, 2011
Posts: 11
Hi,

I am trying to insert multiple values to the MSAccess database , i tried doing



but only the last row is stored in the table. Can somebody tell me where the problem is?
Any other alternative is also welcomed.

thanks
sameer
Wendy Gibbons
Bartender

Joined: Oct 21, 2008
Posts: 1107

As far as I can see from the snippet of code you supplied you are only creating 1 row of data, one row with three values in it.
Sameer Ahmed
Greenhorn

Joined: Apr 15, 2011
Posts: 11
My bad, forgot to put the for loop, code edited.
Wei Dai
Ranch Hand

Joined: Jun 22, 2005
Posts: 86
for(int i=0;i<100;i++){
query="insert into ABC values (?, ?, ?)";
PreparedStatement pst = conn.prepareStatement(query);
pst.setString(1,"a");

It should be
query="insert into ABC values (?, ?, ?)";
PreparedStatement pst = conn.prepareStatement(query);
for(int i=0;i<100;i++){
pst.setString(1,"a");
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3611
    
  60

Wei is right. If that was done in a way that actually compiled, you'd be creating a new statement in every iteration of the loop, every of which would contain one row in the batch, and only execute the batch of the last created instance.

Also, you should specify columns into which you're inserting the values, see Always use column lists.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Multiple insert into "MS Access" database