Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Struts Configuring Struts DataSource Manager on Tomcat 5

Related Tutorials


 
 

Share on Google+Share on Google+

Configuring Struts DataSource Manager on Tomcat 5

Advertisement
This tutorial shows you how you can configure Struts DataSource Manager on the Tomcat 5.5.9 server.

Configuring Struts DataSource Manager on Tomcat 5

     

This tutorial shows you how you can configure Struts DataSource Manager on the Tomcat 5.5.9 server. We will use struts 1.2.7 in this tutorial. In this tutorial we will configure Struts DataSource Manager to use MySQL Database and use the connection provided by Struts DataSource in action class.

Downloading and Installing Tomcat 5.5.9
Download jakarta-tomcat-5.5.9 from http://jakarta.apache.org/tomcat/. Install it on your machine. Run and test the pages that comes with the tomcat.

Download Struts
Download Struts struts-1.2.7 from http://struts.apache.org/download.cgi and unzip it to your favorite directory. Go to the struts-1.2.7\webapps directory and then unzip struts-blank.war file. We will use this file to write our tutorial.

Download MySQL JDBC Driver
Download mysql-connector-java-3.0.16-ga-bin.jar from here mysql-connector-java-3.0.16-ga-bin.jar or you can download and use the latest version of mysql jdbc driver. Copy the JDBC driver file (mysql-connector-java-3.0.16-ga-bin.jar or latest version) to the jakarta-tomcat-5.5.9\common\lib directory of your tomcat installation. This will add the MySQL JDBC driver to the tomcat server.

Creating MySQL Database
In this tutorial I am using MySQL server installed on my local machine. You can download and install MySQL on your local machine and use for this tutorial. If you already have MySQL server then you can use the existing MySQL server.

Create database "strutsdatabase" on the MySQL server and then run the following query to create test table.

CREATE TABLE `test` (
`username` varchar(20) NOT NULL default ''
) TYPE=MyISAM;

/*Data for the table `test` */

insert into `test` values ('rajesh'),('George'),('Vikas'),('Prakash'),('Mahesh');

Above query creates test table and then populates the table with data.

Configuring Struts Application
Now create a directory "strutsdatabase" in the jakarta-tomcat-5.5.9\webapps\ directory and copy the content of struts-blank application (unzipped above) in the strutsdatabase directory.

Now start the tomcat and try to access the strutsdatabase application by typing the url http://localhost:8080/strutsdatabase in browser. Your browser should display the welcome page. After testing shutdown the tomcat server.

Configuring Struts DataSource Manager
The Struts DataSource manager makes it easy for your Action class get the database connection. To configure the Stuts DataSource Manager we will uncomment the <data-sources> entry in the struts-config.xml.

Uncomment and then <data-sources> entry in the struts-config.xml and then change the line "org.apache.commons.dbcp.BasicDataSource" to "org.apache.tomcat.dbcp.dbcp.BasicDataSource". In tomcat 5.5.9 dbcp classes are packaged in naming-factory-dbcp.jar archieve, so we are using "org.apache.tomcat.dbcp.dbcp.BasicDataSource" instead of "org.apache.commons.dbcp.BasicDataSource". After this change the database dirver, database url and passwor in the <data-sources> tag.

You <data-source> element should look like:

<data-sources>
<data-source type="org.apache.tomcat.
dbcp.dbcp.BasicDataSource">
    <set-property
      property="driverClassName"
      value="com.mysql.jdbc.Driver" />
    <set-property
      property="url"
      value="jdbc:mysql://localhost:3306
/strutsdatabase?autoReconnect=true" />
    <set-property
      property="username"
      value="root" />
    <set-property
      property="password"
      value="" />
    <set-property
      property="maxActive"
      value="10" />
    <set-property
      property="maxWait"
      value="5000" />
    <set-property
      property="defaultAutoCommit"
      value="false" />
    <set-property
      property="defaultReadOnly"
      value="false" />
    <set-property
      property="validationQuery"
      value="SELECT COUNT(*) FROM test" />
</data-source>
</data-sources>
        

Create action Class to Test the DataSource
Now we will write the code of Action class for getting the connection form DataSource:

package test;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import java.sql.*;

public class TestDataSource extends Action
{
  public ActionForward execute(
  ActionMapping mapping,
  ActionForm form,
  HttpServletRequest request,
  HttpServletResponse responsethrows Exception{

 javax.sql.DataSource dataSource;
 java.sql.Connection myConnection=null;
 try {
  dataSource = getDataSource(request);
  myConnection = dataSource.getConnection();
  Statement stmt=myConnection.createStatement();
  ResultSet rst=stmt.executeQuery("select

 username from test");
  System.out.println("*************

*****************************");
  System.out.println("********Out

 Put from TestDataSource ******");
  while(rst.next()){
  System.out.println("User Name is:

 " + rst.getString("username"));
  }
  System.out.println("************

******************************");
  rst.close();
  stmt.close();
  // do what you wish with myConnection
 catch (SQLException sqle) {
  getServlet().log("Connection.process", sqle);
 finally {
  //enclose this in a finally block to make
  //sure the connection is closed
  try {
 myConnection.close();
  catch (SQLException e) {
 getServlet().log("Connection.close", e);
  }
 }


  return mapping.findForward("success");
  }

Following code is used to get the data source and then connection from Struts DataSource:

dataSource = getDataSource(request);
myConnection = dataSource.getConnection();

Save this file(TestDataSource.java)  into jakarta-tomcat-5.5.9\webapps\strutsdatabase\WEB-INF\src\java\test directory. Add the servlet API into class path. Then open dos prompt and navigate to jakarta-tomcat-5.5.9\webapps\strutsdatabase\WEB-INF\src\ directory and issue run ant. This will compile the action class (TestDataSource.java) and copy it to the classes directory of the webapplication.

Creating Action Mapping struts-config.xml
Now add the following action mapping into the struts-config.xml:

<action
  path="/DataSource"
  type="test.TestDataSource">
  <forward name="success" path="/success.jsp"/>
</action>

Running and testing
Start tomcat and browse the url http://localhost:8080/strutsdatabase/DataSource.do. Your browser should show the following output.

Now check tomcat console, it should display records fetched from database.

You can download my struts-config.xml from here.

Note: The DataSource manager is being retained in Struts 1.x for backward compatibility but may not be retained in Struts 2.x or later.

 

     

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: February 11, 2008

Related Tutorials

Discuss: Configuring Struts DataSource Manager on Tomcat 5   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:3
subrata chakraborty
May 24, 2011
struts data source configuration

very good eample
ravindra nagwanshi
December 14, 2011
java

i am getting problem to do connectivity using mysql,if anyone know please send me the code else explain me
tom
February 15, 2012
config xml file

Please reattach your compiled struts-config.xml file?
DMCA.com