AppletTalk.com Forum Index AppletTalk.com
Java discussions newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Batching PreparedStatements with Spring??

 
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java and Databases
View previous topic :: View next topic  
Author Message
Jaba
Guest





PostPosted: Mon Feb 13, 2006 9:12 pm    Post subject: Batching PreparedStatements with Spring?? Reply with quote



I'm trying to get familiar with Spring and its many capabilities. I'm
currently trying to batch prepared statements and I'm having difficulty
doing it.

Can someone give me some guidance on how to do this? Everything I've
done to this point has been using a simple SQL statement. I've even
batched SQL statements and run that with success.

I've been trying to use "batchUpdate(String
sql,BatchPreparedStatementSetter pss)" but I can not figure out the
whole BatchPreparedStatementSetter aspect of the method.

Any help would be appreciated.

Thanks
Back to top
Jan Thomä
Guest





PostPosted: Fri Mar 10, 2006 3:12 pm    Post subject: Re: Batching PreparedStatements with Spring?? Reply with quote



Jaba wrote:
Quote:
[..]
I've been trying to use "batchUpdate(String
sql,BatchPreparedStatementSetter pss)" but I can not figure out the
whole BatchPreparedStatementSetter aspect of the method.
[..]


The BatchPreparedStatementSetter is an interface that you have to
implement within your project. It holds information about the content
which is to be updated... A simple example:

YOu got a table

MyTable
myVal1 : int
myVal2 : int


now you want to batch insert 5000 entries into that. You got that data
in an array;

int[5000][2] myData;

// ... Fill that array here

now you wanna put that into the db

db.batchUpdate("insert into myTable( myVal1, myVal2 ) values ( ?, ? )",
new MyStatementSetter( myData ) );


now the statement setter class

class MyStatementSetter implements BatchPreparedStatementSetter {

private int[][] data;
public MyStatementSetter( int[][] data ) {
this.data = data;
}

public int getBatchSize() {
return data.length;
}

// this is called for each row
public void setValues( PreparedStatement ps, int i ) {
ps.setInt( 0, data[i][0] ); // set first value
ps.setInt( 1, data[i][1] ); // set second value
}
}

This should yield 5000 new rows in your table. As you can see the
statement setter is called for each row to update the values in the
prepared statement. After that, the statement is fed to the jdbc driver.

Hope this helps.

Greetings,
Jan
Back to top
Display posts from previous:   
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java and Databases All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.