Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Is it possible to use batch for SQL select statment

 
Bartender
Posts: 352
  • 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()
 
Ranch Hand
Posts: 1087
Oracle Spring Java
  • 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: 352
  • 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: 1087
Oracle Spring Java
  • 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
 
author & internet detective
Posts: 40198
816
Eclipse IDE VI Editor Java
  • 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'm doing laundry! Look how clean this tiny ad is:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
    Bookmark Topic Watch Topic
  • New Topic