This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
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


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
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
 
jQuery in Action, 2nd edition
 
subject: struts database connection