Computer Science 396S
Assignment 6
Due: October 25, 2004

For this assignment, add a JSP file and a Java bean to your project.  Your project should now be able to display, find, insert, delete, and update.  You should have done four of these for the previous assignment.  Do the fifth using JSP and beans.

For example, if you have already included display, find, insert, and delete, you are ready to add to your html page a form to update the database.  If so, you can add a form something like the following:
    h3>Change Price</h3>
         <form method = "get" action="../grocery/changePrice.jsp">
                  <input name="id" type="text" value = "" size = "10" /> Product ID<br />
                  <input name="price" type="text" value = "" size = "10" /> New Price<br />
                  <p><input type="submit" value="Change Price" /></p>
     </form>

Then in the same folder, put in a JSP file called changePrice.jsp.  An example file for changing a price follows:
<html>
<head><title> Change Price JSP. </title></head>

<body bgcolor="white">
<font size=4 color="blue">

<jsp:useBean id="changePriceBean" scope="session" class="produce.ChangePriceBean" />
<jsp:setProperty name="changePriceBean" property="*" />

<% changePriceBean.processRequest(); %>
<% if (changePriceBean.getSuccess () > 0)
{ %>
     <h4>Price Changed.
     <br />Produce Table</h4>
     <table border = "1" cellspacing = "5">
          <tr>
                  <td><% out.print (changePriceBean.getId()); %></td>
                  <td><% out.print (changePriceBean.getType()); %></td>
                  <td><% out.print (changePriceBean.getName()); %></td>
                  <td><% out.print (changePriceBean.getVariety()); %></td>
                  <td><% out.println (changePriceBean.getPrice()); %></td>
          </tr>
     </table>
<% } else %>
     <h4>The ID was not in the database.</h4>
</font>
</body></html>

This file refers to a Java bean called ChangePriceBean.java.  While the html and jsp files go in the main folder, the ChangePriceBean.java goes in the classes folder.  The ChangePriceBean example follows.
 

/* ChangePriceBean gets the product id from the request and tries to change the product's price.
It returns whether or not the change was successful and the data in the row.  If the change was
successful, the row values will be displayed by the jsp file.  Otherwise it will display an error message.
*/

package produce;

import java.sql.*;
import java.io.*;

/* ChangePriceBean finds a specific product and changes the price. */
public class ChangePriceBean
{
     private String id, type, name, variety, price;
     private int success;

     public String getId() {return id;}
     public String getType() {return type;}
     public String getName() {return name;}
     public String getVariety() {return variety;}
     public String getPrice() {return price;}
     public int getSuccess () {return success;}
 
     public void setId (String i) {id = i;}
     public void setPrice (String p) {price = p;}
 
     public void processRequest ()
     {
           try
          {
               // Get a jdbc-odbc bridge and connect to the deli database.
               Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver");
               Connection con = DriverManager.getConnection ("jdbc:odbc:produce");
               Statement stmt = con.createStatement ();
               String update =  "Update ProduceTable Set Price = '" + price + "' Where ID = '" + id + "'";
               success = stmt.executeUpdate (update);
               if (success > 0)
               {
                    stmt = con.createStatement ();
                    String query = "Select * From ProduceTable Where ID = '" + id + "'";
                    ResultSet rs = stmt.executeQuery (query);
                    rs.next ();
                    id = rs.getString ("ID");
                    type = rs.getString ("Type");
                    name = rs.getString ("Name");
                    variety = rs.getString ("Variety");
                    price = rs.getString ("Price");
               }
               stmt.close ();
          } catch (ClassNotFoundException e){System.out.println ("Class Not Found exception.");}
            catch (SQLException e){System.out.println ("SQL Exception");}
      } // processRequest
} // class ChangePriceBean