File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Struts and the fly likes connection to database with struts Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "connection to database with struts" Watch "connection to database with struts" New topic

connection to database with struts

vin Hari
Ranch Hand

Joined: Nov 16, 2006
Posts: 161
Hello all,
I have some problem in conncting with the database using struts application
please tell me how to connect for ms-access databse,
where to include that (jdbc dbc:Jdbcodbc) bridge ,where to include dsn.
iam confused please help me.
chetan kokil

Joined: Sep 02, 2006
Posts: 9
look you can access the database by entering this code in struts-config.xml
below is the code to enter
<set-property property="driverClassName"
value="com.mysql.jdbc.Driver" />
<set-property property="url"
value="jdbc:mysql://localhost/retail" />
<set-property property="username"
value="root" />
<set-property property="password"
value="root" />
in the place of driverclassName put your jdbc dbc bridge and on the url place put the database name with the required things.

and now in the action class you can use this.

DataSource dataSource=getDataSource(HttpServletRequest);
Connection con=dataSource.getConnection() and now the rest things are as usual...
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
I'd advise against using the Struts DataSource configuration utility. It's deprecated in later versions, so it would make your application more difficult to upgrade. It also has portability issues. I've seen instances where it worked fine in development, but didn't work when the application was ported to production.

The best practice for connecting to a JDBC DataSource in a Struts or any J2EE Web Application is to define the DataSource as a JNDI resource using your Application Server's preferred method and then using JNDI to look up the DataSource in your application code. If you're using Tomcat, this link explains how to do it.

Also see question 7 in the JavaRanch Struts FAQ.
[ March 20, 2007: Message edited by: Merrill Higginson ]

Consultant, Sima Solutions
I agree. Here's the link:
subject: connection to database with struts
It's not a secret anymore!