• Post Reply Bookmark Topic Watch Topic
  • New Topic

Re-using the Java Resultset object  RSS feed

 
trupti nigam
Ranch Hand
Posts: 640
1
  • Mark post as helpful
  • send pies
  • 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
 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • 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.
 
Tapas Chand
Ranch Hand
Posts: 614
9
BSD Java
  • Mark post as helpful
  • send pies
  • 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.
 
Stephan van Hulst
Saloon Keeper
Posts: 7969
143
  • Mark post as helpful
  • send pies
  • 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: 614
9
BSD Java
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!