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

struts database connection

Betsy Camel
Ranch Hand

Joined: Oct 02, 2003
Posts: 119
i need to connect to the database using struts. where shud i write the code?
i mean in the jsp or the java file
the code itself will be very much useful
Kerry Wilson
Ranch Hand

Joined: Oct 29, 2003
Posts: 254
Struts has support for a data source setup in struts-config.xml, however, it is not recommended for use (because you have to pass the datasource from the action to your data layer ). You should use jndi with tomcat. There are plenty of resources to see how to do this but just briefly.

In server.xml in the context tag:

<Resource name="jdbc/name" auth="Container" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
username="user" password="pass" driverClassName="com.mysql.jdbc.Driver"

In web.xml:

<description>Display name</description>

In Java Code:

Now use connection to execute query. You should lookup PreparedStatement or CallableStatement to use as opposed to just a straight query.
[ August 09, 2005: Message edited by: Kerry Wilson ]
SCJP 1.4
I agree. Here's the link:
subject: struts database connection
It's not a secret anymore!