aspose file tools
The moose likes JDBC and Relational Databases and the fly likes Connection attempt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "Connection attempt" Watch "Connection attempt" New topic

Connection attempt

Dan Parsons
Ranch Hand

Joined: May 30, 2007
Posts: 70
Is this a good way to insert and update into a database or is there anything I can improve?

Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 31655

Two things jump out at me:
1) The statement resource is never closed. This should be closed in a finally block to release resources. Otherwise, you are hoping the garbage collector will clean it up for you. This is fine for memory. For a valuable database resource, it is problematic.
2) The code is brittle if it isn't called exactly the way you expect. If you are just experimenting, this is fine. If you are going to have to maintain the code, it is difficult. For example, what if the caller doesn't call finally? (resource leak) What if connection is null? (the method just silently does nothing)

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
subject: Connection attempt