Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

try..catch block

 
Yuan Ye
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a question I have for a long time. For a db access method, normally we have the structure of:


Is there a way to clean up th code for the last catch block which is for the db clean up exception? Or do I have to write such block in each db call?
[ April 21, 2006: Message edited by: Yuan Ye ]
 
stu derby
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the "db cleanup" is the same, put all that finally stuff in a static class.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34410
346
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yuan,
A common approach is to create a DBUtils class that has the common code in a static method. Then you can pass the connection/statement/resultset into that method. So one call is repeated instead of a bunch of code.
 
Mattias Arthursson
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another common solution to avoid writing the same dull jdbc code over and over is to use Spring JDBC, a part of the Spring framework. It neatly encapsulates all of the plumbing (connection management, exception handling, looping through result sets, resource cleanup etc.) and lets you focus on the important stuff.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic