Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
Python, Lua and Tcl - public course schedule [here]
Private courses on your site - see [here]
Please ask about maintenance training for Perl, PHP, Java, C, C++, Ruby, MySQL and Linux / Tomcat systems
JSP - example code

Posted by enquirer (enquirer), 6 March 2004
Can you give me an example of all the pieces of code I'll need to write a JSP that looks something up in a database?

Posted by admin (Graham Ellis), 6 March 2004
You'll need:
a) A form in which to enter any information needed to define your query
b) Your JSP page to perform the query and report
c) A Java bean to do the "real work" behind the JSP page

The form
<title>ISBN lookup!</title>
<h1>Find an author from an ISBN number</h1>
Please enter the ISBN number of a book in the Well House Consultants
library and we'll tell you the name of (one of) the Authors
<form action=getauthor.jsp>Enter ISBN <input name="isbn" /><br />
<input type="submit" /></form>

The JSP page (getauthor.jsp in the same directory as the form)
<title>Author of book from ISBN number</title>
<h1> Result page</h1>
<jsp:useBean id="fred" class="boox.Book" />
<jsp:setProperty name="fred" property="*" />
<% if (fred.getAuthor().equals("numerous")) { %>
That is not an ISBN number on our database<br>
<% } else { %>
Author is <jsp:getProperty name="fred" property="author" /><br>
<% } %>

The Java bean (in WEB-INF/classes/boox, compiled to .class)
package boox;
import java.sql.*;
public class Book {
String Isbn;
public Book () {
       Isbn = null;
public void setIsbn(String val) {
       Isbn = val;
public String getAuthor() {
       String Wanted = "numerous";
       if (Isbn == null) return "Not set";
       try {
       Connection books = DriverManager.getConnection
       Statement s = books.createStatement();
       ResultSet r = s.executeQuery
("Select authorfull from books where isbn = \""+Isbn+"\"");
       while ( {
               Wanted =  r.getString("authorfull");
       } catch (Exception e) {
               return e.toString();
       return Wanted;
public static void main (String [] args) {
       Book Desk = new Book();
       String By = Desk.getAuthor();


This is cut and pasted from a training server, and has been tested. In a live application you would add better feedback, error testing and exception handling as a minimum.  You would also use a better password on your database!

This page is a thread posted to the opentalk forum at and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2018: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01225 708225 • FAX: 01225 793803 • EMAIL: • WEB: • SKYPE: wellho