Sun ONE logo     
JDBC SIMPLE Sample Application



JDBC SIMPLE Sample Application


This document describes how to utilize the JDBC SIMPLE sample application in conjunction with Sun ONE Application Server version 7.

This sample application document contains the following sections:



Overview

Like HelloWorld, let's assume that you want to create a small application to demonstrate web-based access to a stateless session bean via a servlet and JSP.  Beyond HelloWorld, you also want a quick way to exercise JDBC to insert and retrieve rows from a database.  So, the JDBC-Simple application extends the HelloWorld sample by storing the generated greetings in the database and allowing you to retrieve all greetings that have been issued.

The application look and feel is illustrated below.

  1. Bring up a web page with a form like the one displayed in Figure 1.



Figure 1   

  1. Enter a string and press the Process button.

  2. A servlet is invoked. This servlet is one of two "controllers" (there are two servlets) for the application.

  3. The controller servlet calls a stateless session bean to determine a greeting based upon the current system time (just like in the HelloWorld sample). This EJB fulfills the role of the "model" for our app.

  4. The servlet dispatches a JSP to deliver the greeting back to the browser. The JSP servers as the "view" component of our application.

  5. As a result of the JSP execution, you will see Figure 2



Figure 2

  1. Traverse the "here" link to invoke the other "controller" (a servlet) to retrieve the all records from the table and display them via a JSP.

  2. As a result of this second JSP's execution, you should see a page similiar to the one displayed in Figure 3



    Figure 3

  1. Click on the "here" link to return to the initial page and enter a new name for a greeting. Then, view the recorded greetings again to see that additional greetings are retrieved from the database:

    To better understand the sample application source code, refer to the related javadocs which can be accessed at s1as_install_dir/samples/jdbc/simple/javadocs/index.html after it is generated.


    Setting up the environment

    In order to successfully deploy and run the JDBC SIMPLE sample application, Database resources (PointBase) needs to be setup with the Application Server.

    If you want to run this sample on Oracle Database, please refer to (How to run sample applications with Oracle ) .



    Compiling and Assembling the Application



    This section contains instructions to build and assemble the JDBC SIMPLE sample application using a Command Line Interface (CLI).

    1. Go to s1as_install_dir/samples/jdbc/simple/src/

      Execute the command asant



      Note You must add s1as_install_dir/bin to your path.



      The default target core will be executed to build the EAR file.

    2. Deploy the application.

      After you have re-created the sample application from scratch, you may proceed to Deploying the Sample Application. Step 3 is optional.

    3. Clean the application project area (optional).

      Execute the command asant clean to remove the sample application assemble and build directories.

      Now you are ready to use this ear file for deployment.



    Deploying the Sample Application

    In this section, you will learn how to deploy the JDBC SIMPLE sample application.

    Command Line-based Deployment describes how to manually register the application in Sun ONE Application Server using a Command Line Interface (CLI). This is the fastest means of deploying the sample application.

    Since a complete EAR file is supplied, the fastest means of setting up the application is to use the command line utilities as described in this section.

    The pre-built jdbc-simple.ear file is a Enterprise Archive file that contains the JAR file of the application. Within the JAR file resides the XML deployment descriptor files, application class files, and other content required by the application.

    Deploying the pre-built jdbc-simple.ear file is simple. Deploy either by Using the asant script or by Using the asadmin command.


    Using the asant script

    1. Go to the src directory of the sample. For example:

      s1as_install_dir/samples/jdbc/simple/src

    2. Run asant using deploy task. For example:

      %asant deploy


    Using the asadmin command

    1. Go to s1as_install_dir/samples/jdbc/simple/

    2. Execute the following:

      asadmin deploy -u <user-name> -w <password> -H <hostname> -p <admin server port> --instance <instance name> jdbc-simple.ear

    The deployment process involves the following operations:

    • Authenticates against the local application server's administrative server

    • The EAR file is transferred to the administrative server

    • The administrative server begins the registration process:

      • Parses the EAR file and embedded JAR file

      • Explodes the bits into repository

      • Registers the J2EE application "jdbc-simple" by updating server.xml

    If you would like to verify the registration of the application, you may proceed to Verifying Deployment. Otherwise, proceed to Running the Sample Application.


    Verifying Deployment

    As an optional step, you can use the Sun ONE Application Server Administration Tool to verify that the application has been registered. Otherwise, proceed directly to Running the Sample Application.

    To verify the registration of the application, do the following:

    Execute the command asadmin list-components to look at applications deployed with a server instance. For example:

    asadmin list-components -u <username> -w <password> -H <host> -p <admin-port> <server instance name>.

    You will see jdbc-simple listed.


    Undeploy the application

    If you wish to undeploy the application after successful deployment, you may do so by either Using the asant script or by Using the asadmin command.


    Using the asant script

    1. Go to s1as_install_dir/samples/jdbc/simple/src

    2. Execute the following:

      asant undeploy


    Using the asadmin command

    1. Go to s1as_install_dir/samples/jdbc/simple/src

    2. Execute the following:

      asadmin undeploy -u <user-name> -w <password> -H <hostname> -p <admin server port> --instance <instance name> jdbc-simple



    Running the Sample Application

    The schema/table are prepopulated so you don't need to configure the database. When the application is deployed, the jdbc-connection-pool and jdbc-resource are already taken care of. If you want to understand how to create/delete the jdbc-connection-pool/jdbc-resource, please refer to build.xml.

    After the database is configured, you can run the appliction through the following URL:

    http://<AppServer hostname>:<port>/jdbc-simple/index.html



    Troubleshooting



    • Please make sure that you run asant undeploy before redeploying this sample. Thiss will ensure that all modules are undeployed and the resources are disabled.

    • If you encounter problems when running the application, review the log files at s1as_install_dir/domains/<domain-instance> /<server-instance>/logs/server.log to learn what exactly went wrong.



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

Last Updated July 11, 2002