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