This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes JDBC 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

Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Databases » JDBC
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
internet detective

Joined: May 26, 2003
Posts: 29237

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.

[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
I agree. Here's the link:
subject: Methods for using JDBC Class in a web app?
Similar Threads
Weblogic pool connection Vs JDBC Connection
JDBC Coonection Pooling in a multithreaded Web application
JDBC Connection Utility: static class?
Putting ResultSet into an Array list
Connecting to multiple databases using different jdbc drivers