wood burning stoves 2.0*
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "connection to database with struts" Watch "connection to database with struts" New topic
Author

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
Greenhorn

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
<data-sources>
<data-source
type="org.apache.commons.dbcp.BasicDataSource">
<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" />
</data-source>
</data-sources>
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 ]

Merrill
Consultant, Sima Solutions
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: connection to database with struts
 
Similar Threads
Storing Image into database using Struts 1.2 + Hibernate + Oracle
Where do I put database calls in a Struts app?
Database Connection with Struts2.0.9
Struts
SCJWSD online Group study