Sun Java System Application Server 

Samples Index

The JDBC-Simple Sample Application

The JDBC-Simple application demonstrates a servlet and JavaServer Pages (JSP) accessing a stateless session bean. It also demonstrates using JDBC to insert and retrieve rows from a database. The JDBC-Simple application extends the HelloWorld sample application by storing the generated greetings in a database, enabling you to retrieve all greetings that have been issued.

Setting Up the Environment

Before you deploy or run the application, you must ensure that a database resource has been configured for Application Server. The included asant deployment script automatically performs this step for you. But if you are using another deployment mechanism, you must perform it manually.

You can use any JDBC-compliant relational database server, but specific instructions are provided for the following:

Building, Deploying, and Running the Application

The general instructions for building, deploying, and running sample applications are provided in Building, Deploying, and Running Sample Applications. In reading this document, use the following application-specific variables for this application:

Follow this general procedure:

  1. Build the Application. The build file, build.xml, is in the app_dir directory, specifically, install_dir/samples/jdbc/apps/simple.
  2. Deploy the Application. The EAR file is install_dir/samples/jdbc/apps/simple/jdbc-simple.ear. Do not run asant unsetup or asadmin delete-jdbc-connection-pool ... if you are running the jdbc-transactions sample.
  3. Run the Application as described in the next section.

Running the Application

After building and deploying the application, follow these steps to run and use it:

  1. Load the URL http://hostname:port/jdbc-simple/
  2. You will see the main page that contains a simple form with a field labeled "Enter Name" followed by a button labeled "Process."
  3. Enter a name in the text field, and click the Process button. Clicking the button submits the form, which invokes a servlet. This servlet is one of two servlet "controllers" for the application. The controller servlet calls a stateless session bean to determine a greeting based on the current system time, just like in the HelloWorld sample. This Enterprise JavaBean (EJB) serves as the "model" for this application. The servlet dispatches a JSP component to deliver the greeting back to the browser. The JSP serves as the "view" component of this application.
  4. The web page displays the message such as "Good afternoon, Fred. Enjoy your afternoon. This greeting has been recorded in the database. To see all the recorded greetings, click here." The word "here" is a link.
  5. Click the "here" link to invoke the other controller servlet, which retrieves the records from the table and displays them using a JSP. The resultant web page shows the contents of the database that consists three columns: Timestamp, Name, and Message. A line of instructions includes a link to enter another greeting.
  6. Click the "here" link to return to the first page and enter a new name for a greeting.
  7. View the recorded greetings again to see that the additional greetings are retrieved from the database.

Copyright © 2006 Sun Microsystems, Inc. All rights reserved.