File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is it possible to use batch for SQL select statment

 
Sharma Ashutosh
Bartender
Posts: 346
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it possible to use batch for SQL select statment?
I m using prepared statement-which is setting one argument and then immedietely it fires a SQL select query to fetch the data. This way it's not very effient as i keep on getting this argument in bulk and they are grouped in certain manner.
What i want to know whether i can addBatch() and then executeBatch() or some other varient of batch updates in SQL select kind of queries?
for(int i=0; i < someLenth; i++)
{
pstmt.setLong(1,arr[i])
pstmt.addBatch();
}
pstmt.executeBatch();
where pstmt is just a prepared statement.
I need a resultSet object just the way i can get it from pstmt.executeQuery()
 
Shailesh Chandra
Ranch Hand
Posts: 1082
Java Oracle Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I dont think you can do in this way.

Just a suggestion
you make a single query using IN clause and then you will get one resultset and process your data in code.
HashMap/Treemap will help you to implement this logic

Shailesh
 
Sharma Ashutosh
Bartender
Posts: 346
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Shailesh-but IN expects all the values to be resolved at compile time-i just cant iterate thru this array and then use IN.
 
Shailesh Chandra
Ranch Hand
Posts: 1082
Java Oracle Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
However I dont recommend to use Statement over PreparedStatement but for this case you can use Statement to avoid multiple hit to server.

I will look for, that how it can be done with preparedstatement.

Shailesh
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 33703
316
Eclipse IDE Java VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Monu,
You can still use a prepared statement:
select fields from table where id in (?, ?, ?, ?)

If you search this forum for batching, I made some posts on preparing a few different size in clauses. This gives a performance benefit as you reuse the same set of preparaed statements each time.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic