Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

struts database connection

 
Betsy Camel
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 254
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic