The Java EE 5 Tutorial

Building and Running the order Application

This section describes how to build, package, deploy, and run the order application. To do this, you will create the database tables in the Java DB server, then build, deploy, and run the example.

Creating the Database Tables in NetBeans IDE

To create the database tables in Java DB, the database server included with Application Server, you need to create the database connection and execute the SQL commands in tut-install/examples/common/sql/javadb/tutorial.sql.

Creating the Database Connection

    To create the database connection do the following:

  1. Click the Services tab.

  2. Right-click the Databases node and select New Connection to open the New Connection dialog.

  3. Under Name, select Java DB (Network).

  4. Set Database URL to the following:


    jdbc:derby://localhost:1527/sun-appserv-samples
  5. Set User Name to APP.

  6. Set Password to APP.

  7. Select the Remember Password during this Session box.

  8. Click OK.

Creating the Tables

    To create the tutorial tables, do the following:

  1. Select File->Open File.

  2. Navigate to tut-install/examples/common/sql/javadb/ and open tutorial.sql.

  3. In the editor pane, select the connection URL to Java DB:


    jdbc:derby://localhost:1527/sun-appserv-samples
  4. Click the Run SQL button at the top of the editor pane.

    You will see the output from the SQL commands in the Output tab.

Deleting the Tables

    To delete the tutorial tables, do the following:

  1. Select File->Open File.

  2. Navigate to tut-install/examples/common/sql/javadb/ and open delete.sql.

  3. In the editor pane, select the connection URL to Java DB:


    jdbc:derby://localhost:1527/sun-appserv-samples
  4. Click the Run SQL button at the top of the editor pane.

    You will see the output from the SQL commands in the Output tab.

Creating the Database Tables Using Ant

    The database tables are automatically created by the create-tables task, which is called before you deploy the application with the ant deploy task. To manually create the tables, do the following:

  1. In a terminal window, navigate to tut-install/javaeetutorial5/examples/ejb/order/.

  2. Type the following command:


    ant create-tables
    

    Note –

    The first time the create-tables task is run, you will see error messages when the task attempts to remove tables that don’t exist. Ignore these error messages. Subsequent calls to create-tables will run with no errors and will reset the database tables.


Building, Packaging, Deploying, and Running order In NetBeans IDE

    Follow these instructions to build, package, deploy, and run the order example to your Application Server instance using NetBeans IDE.

  1. In NetBeans IDE, select File->Open Project.

  2. In the Open Project dialog, navigate to tut-install/javaeetutorial5/examples/ejb/.

  3. Select the order folder.

  4. Select the Open as Main Project and Open Required Projects check boxes.

  5. Click Open Project.

  6. In the Projects tab, right-click the order project and select Run.

You will see the following output from the application client in the Output tab:


...
Cost of Bill of Material for PN SDFG-ERTY-BN Rev: 7:  $241.86
Cost of Order 1111:  $664.68
Cost of Order 4312:  $2,011.44

Adding 5% discount
Cost of Order 1111:  $627.75
Cost of Order 4312:  $1,910.87

Removing 7% discount
Cost of Order 1111:  $679.45
Cost of Order 4312:  $2,011.44

Average price of all parts:  $117.55

Total price of parts for Vendor 100:  $501.06

Ordered list of vendors for order 1111
200 Gadget, Inc. Mrs. Smith
100 WidgetCorp Mr. Jones

Counting all line items
Found 6 line items

Removing Order 4312
Counting all line items
Found 3 line items

Found 1 out of 2 vendors with ’I’ in the name:
Gadget, Inc.
run-order-app-client:
run-ant:
run:
BUILD SUCCESSFUL (total time: 22 seconds)

Building, Packaging, Deploying, and Running order Using Ant

To build the application components of order, enter the following command:


ant

This runs the default task, which compiles the source files and packages the application into an EAR file located at tut-install/examples/ejb/order/dist/order.ear.

To deploy the EAR, make sure the Application Server is started, then enter the following command:


ant deploy

After order.ear is deployed, a client JAR, orderClient.jar, is retrieved. This contains the application client.

To run the application client, enter the following command:


ant run

You will see the following output:


...
run:
    [echo] Running appclient for Order.

appclient-command-common:
    [exec] Cost of Bill of Material for PN SDFG-ERTY-BN Rev: 7:
         $241.86
    [exec] Cost of Order 1111:  $664.68
    [exec] Cost of Order 4312:  $2,011.44

    [exec] Adding 5% discount
    [exec] Cost of Order 1111:  $627.75
    [exec] Cost of Order 4312:  $1,910.87

    [exec] Removing 7% discount
    [exec] Cost of Order 1111:  $679.45
    [exec] Cost of Order 4312:  $2,011.44

    [exec] Average price of all parts:  $117.55

    [exec] Total price of parts for Vendor 100:  $501.06

    [exec] Ordered list of vendors for order 1111
    [exec] 200 Gadget, Inc. Mrs. Smith
    [exec] 100 WidgetCorp Mr. Jones

    [exec] Counting all line items
    [exec] Found 6 line items

    [exec] Removing Order 4312
    [exec] Counting all line items
    [exec] Found 3 line items

    [exec] Found 1 out of 2 vendors with ’I’ in the name:
    [exec] Gadget, Inc.

BUILD SUCCESSFUL

Note –

Before re-running the application client, you must reset the database by running the create-tables task.


The all Task

As a convenience, the all task will build, package, deploy, and run the application. To do this, enter the following command:


ant all

Undeploying order

To undeploy order.ear, enter the following command:


ant undeploy