aspose file tools*
The moose likes Servlets and the fly likes Best way to access database from Servlet 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 » Java » Servlets
Bookmark "Best way to access database from Servlet" Watch "Best way to access database from Servlet" New topic
Author

Best way to access database from Servlet

Aryeh Golob
Ranch Hand

Joined: Nov 12, 2006
Posts: 37
Was wondering if anyone could advise me regarding the best was to access a database from Servlet's.

I was thinking about initializing DB servlet in contextInitialized method of ServletContextListener and setting it as an attribute in the ServletContext.

From this point, I would use a static class, say DbUtil to perform all interactions with the database.

Is this a good approach or is there a better way to do things?

Can I initialize database using web.xml file?

Any advice, suggestions is greatly appreciated ...

Thanks in advance!

-Aryeh
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61434
    
  67

It is a very poor way to approach the problem. In a properly structured web application, all database access should be performed in the Model layer, not the Controller.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Aryeh Golob
Ranch Hand

Joined: Nov 12, 2006
Posts: 37
Hey Bear,

Appreciate the reply ...

Where did you get the idea that I was using Control layer?

Placing application in ServletContext was for access reasons.

Can you be more specific regarding the proper way to structure DB access through Model layer.

A link would be fine as well ....

Thanks for the help ....

Be Well,
Aryeh
Stefan Evans
Bartender

Joined: Jul 06, 2005
Posts: 1018
I'm not 100% sure what your DB servlet would be doing.
But your mention of servlet context attributes and static classes sound sub-optimal.

The current best practice is to obtain database connections from a JNDI Datasource. Most servers support this approach now. For example Tomcat

This lets you
- change the database connection details in a configuration somewhere
- gives you connection pooling and other nice features for free :-)

Whenever you want a database connection, look up the JNDI datasource, and call getConnection() on it.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61434
    
  67

Aryeh Golob wrote:Where did you get the idea that I was using Control layer?


Your own words: you said "servlet". If your Model knows anything about servlets, or your servlets know anything about the database, you're doing it wrong.
Aryeh Golob
Ranch Hand

Joined: Nov 12, 2006
Posts: 37
Thanks Guys!

Looks like the JNDI is the right way to go ....

I appreciate the help!

-Aryeh
Rudy Gireyev
Ranch Hand

Joined: May 03, 2011
Posts: 39
Aryeh Golob wrote:
From this point, I would use a static class, say DbUtil to perform all interactions with the database.
Aryeh just to clarify, the DbUtil class need not be a Servlet, in fact should not be a Servlet, as it will not be interacting with the HTTP client, it will only be interacting with your servlets.
Stefan Evans wrote:But your mention of servlet context attributes and static classes sound sub-optimal.
Stefan why do you feel that using the static methods in the DB Access class is sub-optimal?

Rudy
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Best way to access database from Servlet