Navigation Bar

Wednesday, October 21, 2009

Jakarta Struts - connecting to the database - code and configuration file changes

For building a basic struts application you can go through the link below

Building your first struts application

Below is the link for an explanation of changes to the struts application for connecting to the database

Once this is done, below are the changes to be implemented in the code for connecting to mysql database.

The updated struts-config.xml
<?xml version="1.0" encoding="ISO-8859-1" ?>

<!DOCTYPE struts-config PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
          "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">


<struts-config>

	<data-sources>
		<data-source type="org.apache.commons.dbcp.BasicDataSource">
			<set-property property="driverClassName"
			  value="com.mysql.jdbc.Driver" />
			<set-property property="url"
			  value="jdbc:mysql://localhost/stocks" />
			<set-property property="username"
			  value="root" />
			<set-property property="password"
			  value="mysql" />		
		</data-source>
	</data-sources>	
	<form-beans>
		<form-bean  name="lookupForm"
               type="src.LookupForm"/>
	</form-beans>
	<action-mappings>
		<action path="/Lookup"
			type="src.LookupAction"
			name="lookupForm" >
		<forward name="success" path="/quote.jsp" />
		<forward name="failure" path="/index.jsp" />
		</action>	
	</action-mappings>
	<plug-in className="org.apache.struts.tiles.TilesPlugin" >
		<set-property property="definitions-config" value="/WEB-INF/tiles-defs.xml" />
		<set-property property="moduleAware" value="true" />
		<set-property property="definitions-parser-validate" value="true" />
	</plug-in>
</struts-config>
Modified LookupAction.java
package src;

import java.io.IOException;
import javax.servlet.ServletException;
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 javax.sql.DataSource;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class LookupAction extends Action {
    protected Double getQuote ( HttpServletRequest request, String symbol) {
        
		Double price = null;
		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;
		DataSource dataSource = null;
		
		try {
			dataSource = getDataSource(request);
			conn = dataSource.getConnection();
			stmt = conn.createStatement();
			rs = stmt.executeQuery ("select * from stocks where symbol = '" + symbol + "'");
			if (rs.next()){
				double tmp = 0;
				tmp = rs.getDouble("price");

				price = new Double(tmp);
				System.err.println ("price : " + price);
			} //end rs.next
			else {
				System.err.println("Symbol not found returning null");
			}	
				
		}	//end try
		catch (SQLException sqle) {
					System.err.println (sqle.getMessage());
		}
		finally {
			if (rs != null) {
				try {
					rs.close();
				}
				catch (SQLException sqle) {
					System.err.println (sqle.getMessage());
				}
				rs = null;	
			}
		
		
			if (stmt != null) {
				try {
					stmt.close();
				} //end try
				catch (SQLException sqle) {
					System.err.println (sqle.getMessage());
				}
				stmt = null;	
			} // end stmt not null
			
			if (conn != null) {
				try {
					conn.close();
				} //end try
				catch (SQLException sqle) {
					System.err.println (sqle.getMessage());
				}
				conn = null;	
			} // end conn not null
		
		} //end finally	
		if ( symbol.equalsIgnoreCase ("SUNWWWW") ) {
            return new Double (25.00);
        }
		
		return price;
    } //end Action
    public ActionForward execute (ActionMapping mapping,
            ActionForm form, 
            HttpServletRequest request,
            HttpServletResponse response )
                throws IOException, ServletException {
            Double price = null;
            String target = new String ("success");
            if (form != null ) {
                //Use the LookupForm to get the request parameters
                LookupForm lookupForm = (LookupForm) form;
                String symbol = lookupForm.getSymbol();
                price = getQuote(request, symbol);
            }
            //Set the target to failure
            if (price == null) {
                target = new String ("failure");
            }
            else {
                request.setAttribute ("PRICE", price);
            }
            return (mapping.findForward(target));
    } //execute
}
Finally we add the correct version of the .jar files in the lib folder of the getquote project for the version of Struts, Java, Tomcat server and mysql datababase - namely .
mysql.jar
commons-pool-1.4.jar
commons-dbcp-1.2.2.jar

For the version of struts I am using I have taken the corresponding version of the jar files from the links below



God's Word for the day
Humility is an abomination to the proud
  Likewise the poor are an abomination to the rich
When the rich person totters, he is supported by friends,
  But when the humble falls, he is pushed away even by friends.
Sirach 13:20-21

Concerning Prayer 
"When you are praying do not heap up empty phrases as the Gentiles do
  For they think that they will be heard because of their many  words.
Do not be like them, for your Father knows what you need before you ask him.
Pray then in this way :
  Our Father in Heaven,
    Hallowed be your name,
  Your kingdom come,
    Your will be done 
  On Earth as it is in Heaven
    Give us this day our daily bread
  And forgive us our debts,
    As we also have forgiven our debtors
  And do not bring us to the time of trial
    but rescue us from the evil one.

For if you forgive others their trespasses,
  Your heavenly Father will also forgive you
But if you do not forgive others,
  Neither will your Father forgive your trespasses."
Mathew 6:7-14

No comments:

Post a Comment