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 Methods for using JDBC Class in a web app? 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 "Methods for using JDBC Class in a web app?" Watch "Methods for using JDBC Class in a web app?" New topic

Methods for using JDBC Class in a web app?

Nate Leech
Ranch Hand

Joined: Feb 07, 2005
Posts: 43
This is my first time using JDBC and I am having a hard time firguring out the best way to implement my SQL queries in my web app.

Right now I have one class called DataBaseMethods

In this class, I have one method for each call to the database I make.


In each method, I open the database connection, make and execute the sql query and then close the database connection.

I then call these methods in my other buisiness logic classes.

I think this would end up making a LOT of opening and closing of the db. Is there a better way to do this?

Thanks for any help!!!

Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 33117

There is nothing inherently wrong with opening/closing connections. As you run more queries and have multiple users, you can look into connection pooling. The application server provides this. You just get your connection from a datasource in the JNDI instead of using driver manager. Then you aren't really closing the connection, you are just putting it back in the pool. Also, doing one query at a time gives you a better design for the future.

[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: Methods for using JDBC Class in a web app?
It's not a secret anymore!