Article

The JSP Files - Parts 1 to 8: Tagged and Bagged

Page: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 Next

What's Your Name?

Now that you know how to connect to a database, let's begin developing the bare bones of the address book application. This first script asks for a user name and then connects to the database to display entries owned by that user.

We'll be using a single page for the entire operation - the "submit" variable (you remember this technique, don't you?) is used to decide whether to display the initial form or the result page. Take a look:

<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:&nbsp;                        
<input type="text" name="name" size="10"> &nbsp; <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>                        
</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");                        
                       
   out.println("<tr><td>" + FName + "</td><td>" +                          
LName + "</td><td>" + Tel + "</td><td>" + Fax + "</td><td>" +                          
Email + "</td></tr>");                        
 }                        
// close connections                        
SQLResult.close();                        
SQLStatement.close();                        
Conn.close();                        
                       
}                        
%>                        
                       
</table>                        
</center>                        
</body>                        
</html>

As you can see, by checking the value of the "submit" variable, we've successfully combined both the initial page and the results page into a single JSP script. This script simply accepts a user name, connects to the database, and displays records for that user (assuming any exist). Log in as "bill", "joe" or "john" to view the records available for that user.

If you don't like the word "null" being displayed in columns which have no data, you can add a few "if" loops to replace it with an empty space.

<%                        
if (Fax.equals("null"))                        
{                        
Fax = "&nbsp;";                        
}                        
%>

Copyright Melonfire, 2000. All rights reserved.

If you liked this article, share the love:
Print-Friendly Version Suggest an Article

Sponsored Links