VOTG
[Logo] JavaRanch » JavaRanch Saloon
  Search | FAQ | Recent Topics | Hot Topics
Register / Login


Reply Bookmark it! Watch this topic JavaRanch » Forums » Java » JDBC
 
RSS feed
 
New topic
Author

getting a database connection object in the most efficient way :confused:

Abhineet Kapil
Greenhorn

Joined: Feb 08, 2010
Messages: 2

Hi All,

There are various ways in which one can get a DB connection in a web-app.
What could be the best possible way of getting database connection. ?

Some of the ways I know are :

1> Create a java class and get the DB connection in that class. Now where ever one needs the connection , one can instantiate that class and get the connection.

2> Use a ServletContextListener ... >> One can use a listner.. A contextlistener is available application wide and can be used throughout the web-app.

3> Using hibernate one can define the connection params in the hbm-cfg.xml file..(if i correctly remmer) and get the connection..

4> DataSource using jndi -- (xxx-ds.xml) -- In server deploy folder one can create a xxx-ds.xml file.. and specify the connection PARAMS.. then using lookup (jndi name) one can get access to db obj..

Could smone explain which one is the best and why... And also mention other ways of get db connection that are in vogue..
Joe Ess
Bartender

Joined: Oct 29, 2001
Messages: 6879

This isn't really a Struts question. Struts is a front-end controller. It has nothing to do with back-end tasks like database connections. In any case, you will find this FAQ entry interesting.

"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
David Newton
Author
Bartender

Joined: Sep 29, 2008
Messages: 6867

5> Spring and injection.

Moving to... um... JDBC?

Consultant/Trainer | Polyglottal Developer | Struts Committer/PMC | Struts 2 Web Application Development
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Messages: 8544



3> Using hibernate one can define the connection params in the hbm-cfg.xml file..(if i correctly remmer) and get the connection..

This will just piggyback on either a DataSource or use the DriverManager directly depending on your environment. Choosing Hibernate just to get create a JDBC connection is not a sensible choice though.

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
 
 
 
Reply Bookmark it! Watch this topic JavaRanch » Forums » Java » JDBC
 
RSS feed
 
New topic