File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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: 33130

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]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
I agree. Here's the link:
subject: Connection attempt
It's not a secret anymore!