my dog learned polymorphism*
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 Spring in Action this week in the Spring 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
Author

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.

getChildren();
getPersonIds();
.
.
.
etc...

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!!!

-Nate
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30764
    
156

Nate,
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: http://aspose.com/file-tools
 
subject: Methods for using JDBC Class in a web app?