HomeJava & J2EE Page 6 - The JSP Files (part 5): No Forwarding Address
No Forwarding Address - Java
This week, find out how to connect your JSPs to a database and build dynamic, data-driven Web pages. This primer covers different techniques to select, insert and delete records, and uses a simple Web-based address book to illustrate the Connection, Statement, and ResultSet objects.
Next up, updating records. In order to demonstrate this, the first order of business is to modify the next-to-last example so that each entry displayed has an edit option next to it. Here's the modified code:
<html>
<head>
<basefont face="Arial">
</head>
<body>
<center>
<%
// check submit state
String submit = request.getParameter("submit");
// form not yet submitted
// display initial page
if(submit == null)
{
%>
<form action="view.jsp" method="GET">
Enter your name:<input type="text" name="name" size="10">
<input type="submit" name="submit" value="Go">
</form>
<%
}
// form submitted, display result
else
{
%>
<%@ page language="java" import="java.sql.*" %>
<%
// get username
String uid = request.getParameter("name");
// define database parameters
String host="localhost";
String user="us867";
String pass="jsf84d";
String db="db876";
String conn;
%>
<h2><% out.println(uid); %>'s Little Black Book</h2>
<hr>
<table border=1 cellspacing=4 cellpadding=4>
<tr>
<td><b>First name</b></td>
<td><b>Last name</b></td>
<td><b>Tel</b></td>
<td><b>Fax</b></td>
<td><b>Email address</b></td>
<!-- one cell added HERE -->
<td></td>
</tr>
<%
Class.forName("org.gjt.mm.mysql.Driver");
// create connection string
conn = "jdbc:mysql://" + host + "/" + db + "?user=" + user + "&password=" +
pass;
// pass database parameters to JDBC driver
Connection Conn = DriverManager.getConnection(conn);
// query statement
Statement SQLStatement = Conn.createStatement();
// generate query
String Query = "SELECT * FROM abook WHERE uid = '" + uid + "'";
// get result
ResultSet SQLResult = SQLStatement.executeQuery(Query);
// display records
// if available
while(SQLResult.next())
{
String FName = SQLResult.getString("fname");
String LName = SQLResult.getString("lname");
String Tel = SQLResult.getString("tel");
String Fax = SQLResult.getString("fax");
String Email = SQLResult.getString("email");
// get the record number HERE
String ID = SQLResult.getString("id");
// add an edit link to each record with the ID
out.println("<tr><td>" + FName + "</td><td>" +
LName + "</td><td>" + Tel
+ "</td><td>" + Fax + "</td><td>" +
Email + "</td><td><a href=edit.jsp?id="
+ ID + ">edit this entry</a></td></tr>");
}
// close connections
SQLResult.close();
SQLStatement.close();
Conn.close();
}
%>
</table>
</center>
</body>
</html>
Clicking this link will activate the script "edit.jsp"
and pass the record number to it via the URL GET method.
As you can see, once "edit.jsp" receives the record
number, it connects to the database, extracts the record, and then generates a simple form with the values already filled in (note our usage of the shortcut <%= %>construct to display variable values). The user is then free to modify the information displayed in the form; once done, the form is submitted to "edit_res.jsp", which takes care of the UPDATE operation.