1
Student Name: __________________
Class and Section __________________
Total Points (20 pts) __________________
Due: Feb 28, 2011 before the class
Project: Accessing and updating a Staff table (Visual
JSF)
CSCI 5520 Rapid Java Application Development
Armstrong Atlantic State University
Problem Description:
Write a Visual JSF program that views, inserts, and
updates staff information stored in a database, as
shown in the following figure. The view button
displays a record with a specified ID. The Staff
table is created as follows:
create table Staff (
id char(9) not null,
lastName varchar(15),
firstName varchar(15),
Source code: (Name your program Exercise33_09)
2
Screen shot:
What to submit?
1. This document.
2. Fill in self-evaluation:
1. Can you display the form? _________
2. Can you connect to the database? ______
3. Is the View button implemented correctly? _______________
Solution:
<%@page contentType=”text/html” pageEncoding=”windows-1252″%>
<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”
}
%>
<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″>
<title>Exercise42_19</title>
4
>
<label for=’email’>Email: </label><input type=’text’ name=’email’
value = <jsp:getProperty name = “backend” property = “email” /> ><br />
</fieldset>
</td>
</tr>
<tr align=”center”>
<td>
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package chapter42;
import java.sql.*;
import javax.servlet.http.HttpServletRequest;
public class Exercise42_19 {
String id = “”;
public String getMi() {
return mi;
}
public void setMi(String mi) {
this.mi = mi;
6
public void setAddress(String address) {
this.address = address;
public void setState(String state) {
this.state = state;
}
String email = “”;
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public void view(HttpServletRequest request) {
Statement s = null;
try {
s = connection.createStatement();
s.executeQuery(“SELECT * FROM Staff WHERE id=” + id);
result = s.getResultSet();
}
} catch (Exception e) {
} finally {
}
}
public int update(HttpServletRequest request) {
PreparedStatement s = null;
int count = 0;
try {
s = connection.prepareStatement(“update Staff set lastName = ?, firstName = ?,
mi = ?, address = ?, city = ?, state = ?, telephone = ?, email = ? WHERE id = ?”);
s.setString(9, id);
s.setString(1, lastname);
count = s.executeUpdate();
s.close();
} catch (Exception ex) {
ex.printStackTrace();
} finally {
9
city = “”;
state = “”;
email = “”;
telephone = “”;
}
}