• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Re-using the Java Resultset object

 
Ranch Hand
Posts: 644
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello

is there a way I can re-use the java resultset object?

I am trying to get the data from DB, create a object Map out of it and at the same time need to generate a .csv file. If I pass the same resultset to the method that creates the .csv file I get error. re-executing the same query twice will cost performance issue.

any pointers?

I am using preparedstatement.

Thanks
 
Bartender
Posts: 1845
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are the performance issues that big? Is it a huge query? Or are you doing premature optimization?

If your database driver supports it, the resultSet.beforeFirst() method might be handy.

Can your CSV be produced from the Map you have in memory rather than the ResultSet? i.e. if it already has all the info you need, why do you even need to read it twice?

Or you could refactor your code so that instead of each method taking a result set and scrolling through it, they should take a result set row.
That way you only need to iterate through the result set once, calling both methods from that.
That is slightly more complicated though. I would prefer to keep the code simple even if there is a slight performance penalty.
 
Ranch Hand
Posts: 624
9
BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am never comfortable of passing the ResultSet here and there.
I always iterate through ResultSet once, store the result in a List or whatever Collection is suited for the result.
Then I pass that Collection object to every method if needed.

I do not know how experienced people think about it, but I feel comfortable in releasing the java.sql objects as soon as possible.
 
Saloon Keeper
Posts: 14502
325
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree with Tapas. Extract the data you need from the result set, and then do processing. I'm not a fan of these clunky SQL things.
 
Tapas Chand
Ranch Hand
Posts: 624
9
BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually I saw a lot of open cursor issues in the application when I started my career as a programmer.
May be that has injected a fear of the Sql obejcts in me.
So I am desperate to close them as soon as possible.

But currently the application which I am working on is built in Spring MVC-Hibernate.
So getHibernateTemplate() came to my rescue (though it can not be used in all scenarios) and I am free from Sql-object-fobia.
 
Creativity is allowing yourself to make mistakes; art is knowing which ones to keep. Keep this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic