File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
Author

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"
url="jdbc:mysql://127.0.0.1:3306/databaseName?autoReconnect=true"/>

In web.xml:

<resource-ref>
<description>Display name</description>
<res-ref-name>jdbc/name</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>

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 ]

http://www.goodercode.com
SCJP 1.4
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: struts database connection