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 Creating a database connection class 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 "Creating a database connection class" Watch "Creating a database connection class" New topic

Creating a database connection class

Tariq Ahsan
Ranch Hand

Joined: Nov 03, 2003
Posts: 116
I have swing gui where in the logon screen the user enters user id and password and selects a database instance. After the LOGON button is clicked it calls a method called 'accessDB' where Connection object is used to connect to the database and do a query in a table with user information. After user is authenticated through this query a separate screen with a data entry form opens. User would then populate the data entry fields and hit the SAVE button. This would call another method called 'accessDBADD' to store the data into another table. Here in accessDBADD I would like to connect to the same database instance again execute the necessary sql statement to insert the data. Now, for connecting to the database again in accessDBADD I would like to use the same user id, password and url that was selected from the logon screen. What would be a good way to go about doing this?
Shailesh Chandra
Ranch Hand

Joined: Aug 13, 2004
Posts: 1082

The good practice says don't leave your connection open, close them immediately after use. And if you want to use same connection in 'accessDBADD' method then you will have to leave your connection otherwise you would get an error.
Also if there are mulitiple user then there would be load on DB.I have two suggestion for you.
1. Close your connection after use.
2. Use connection pools(There are many open source project available for connection pools)


Gravitation cannot be held responsible for people falling in love ~ Albert Einstein
Phillip Koebbe

Joined: Jun 22, 2005
Posts: 27
The definition of "good practice" is oftentimes relative. I have an application that currently has over 150 classes and does little more than interact with the database. To close the connection after every use only to have to open it again would add an enormous amount of unecessary code and processing to the application. Much to the chagrin of the purists in the development world, I open a connection at the beginning of the application and close it at the end. I suppose that I can get away with this because the user base will be about 6-10. I have a class called Shared that contains the database connection and when I need to create a statement, I have code like:

If I had to create a new connection or pass around a single connection object, I'd go nuts. My advice is to balance "good practice" with common sense and the needs of your particular application/project.

I agree. Here's the link:
subject: Creating a database connection class
jQuery in Action, 3rd edition