Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JDBC/ResultSet best practices

 
Jim Babcock
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm looking for resultSet info, as well. I've seen it inferred that I shouldn't leave things in resultSets, that there is another data structure that is better suited and releases the connection on pages that don't need to keep it open. Unfortunately, these folks never seem to actually say what to use.

I'd like to be able to insert a row into my resultSet w/o updating the DB, so I don't think resultSet is the data structure I want. Once I do the insert, I'd like to use two while loops to step through this and another resultSet, colelating them. Again, it doesn't seem like I have the right data structure. In the future, I may need to display data a page at a time and worry about throughput matters.

All of the tutorials I find are all variations on:

 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35279
384
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jim,
The best practice is to put the data in an ArrayList (or other Java 2 Collections data structure.) Within that ArrayList, you add objects of your own creation that represent a row in the database.

I wish all these tutorials wouldn't show database access from a JSP. As you alluded to, it is bad practice to do that. Instead, store your ArrayList as an attribute in the request and get that in the JSP.
 
Jim Babcock
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd been using a bean and couldn't find a good discussion on patterns of use for that either. I suspect that I should have had a been for every table, but wasn't sure. In the end, because I couldn't figure out how to set up the most basic of where causes without just passing the whole thing as a string and defeating what I thought purpose of beans was, I just ended up just passing the whole sql string... which didn't seem to be much value add.

I guess I'm frustrated because it seems that all the books or web info I find is geared for simplest case or deeply theoretical and I'm stuck with just enough knowledge to do it wrong and need a concise guide to writing robust, scalable patterns.

Jimbus
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35279
384
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jim,
Now I see what you are looking for. I actually recommend looking for a tutorial on MVC (model view controller) rather than JDBC to start. I expect there are a bunch out there. That will show you how to separate out your logic (database) from your JSP.

I suspect that I should have had a been for every table, but wasn't sure.

That's one approach. When you look on the web on this, search for DAO (data access object.) This will show examples of a bean that knows how to read/write/modify a certain table/object.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic