Pet Catalog - Java EE 6 Sample Application

The Pet Catalog application is a Java EE 6 sample that demonstrates usage of JavaServer Faces 2.0 and the Java Persistence API. Setup instructions here assume that you are using the following software components:

For more information on the Pet Catalog Sample Application, see:

Setup Instructions

  1. Setting up the Database
  2. Configuring a Connection Pool and JDBC Resource on GlassFish
  3. Running the Sample Application

Setting up the Database

Setup instructions use 'root' / 'nbuser' as the user account / password combination for access to the MySQL server. Do not use an empty database password.

Change the Root Account Password to 'nbuser'

  1. Open a command-line prompt and navigate to the bin directory within your MySQL installation.
    shell> cd <install-dir>/bin
    (Where <install-dir> is the path to your MySQL installation directory.)

  2. Type in the following:
    shell> mysql -u root
    mysql> UPDATE mysql.user SET Password = PASSWORD('nbuser') WHERE User = 'root';
    For more information, see the official MySQL Reference Manual: Securing the Initial MySQL Accounts.

Register the Server in the IDE

  1. In the Services window (Ctrl-5; ⌘-5 on Mac), right-click the Databases node and choose Register MySQL Server.
    Services window - right-click options on Databases node
  2. In the MySQL Server Properties dialog, under the Basic Properties tab, you can see the default settings for the MySQL server installation. These are:
    • Server Host Name: localhost
    • Server Port Number: 3306
    • Administrator User Name: root
    • Administrator Password: nbuser
  3. Select the Save Password option.
    MySQL Server Properties dialog
  4. Click OK. The IDE connects to your MySQL database server, and lists database instances that are maintained by the server.

Create a Database Instance

  1. Right-click the MySQL Server node and choose Create Database.
  2. In the dialog that displays, type in petcatalog. Select the 'Grant Full Access to' option, then select from the drop-down field.
    Create MySQL Database dialog
  3. Click OK. When you do so, an empty database named petcatalog is created, and a connection to the database is automatically established. Connections are displayed in the Services window using a connection node ( Database connection node ).

Create database tables and populate them with sample data

  1. In the Projects window, expand the Server Resources node and double-click catalog.sql to open it in the IDE's editor.
  2. In the Connection drop-down above the editor, select the jdbc:mysql://localhost:3306/petcatalog connection that you created in the previous step.
  3. Click the Run SQL ( Run SQL button ) button in the editor's toolbar. The script runs on your MySQL server.

Configuring a Connection Pool and JDBC Resource on GlassFish

The sun-resources.xml file, included in the sample application, is a GlassFish-specific deployment descriptor.* If you open the file in the editor (from the Projects window, Server Resources > sun-resources.xml), you can see that the file instructs the GlassFish server to create a connection pool named petCatalogPool using the 'root' / 'nbuser' combination and jdbc:mysql://localhost:3306/petcatalog connection you configured in the previous steps:

<jdbc-connection-pool allow-non-component-callers="false"

  <property name="URL" value="jdbc:mysql://localhost:3306/petcatalog"/>
  <property name="User" value="root"/>
  <property name="Password" value="nbuser"/>

The sun-resources.xml file also instructs the GlassFish server to configure a data source that uses the petCatalogPool connection pool and sets its JNDI name to 'jdbc/petcatalog':

<jdbc-resource enabled="true"

* The sun-resources.xml file is not included in the sample application bundled with IDE versions 6.8 and 6.9. To create the file, perform the following steps.

  1. Open the Files window (Ctrl-2; ⌘-2 on Mac).
  2. Create a new folder in the PetCatalog project named setup. To do so, right-click the project node and choose New > Folder. (If Folder is not displayed, choose Other. Select the Other category, then select the Folder file type. Click Next.)
  3. Name the folder setup, then click Finish.
  4. Save sun-resources.xml to your computer.
  5. Copy (Ctrl-C; ⌘-C on Mac) the sun-resources.xml file from the location on your computer, then paste (Ctrl-V; ⌘-V on Mac) it into the setup folder.
  6. Switch to the Projects window (Ctrl-1; ⌘-1 on Mac) and note that the sun-resources.xml file is displayed in the Server Resources node within the project.

Running the Sample Application

  1. In the Services window, expand the Servers > GlassFish Server 3 node and choose Properties. In the Servers window that displays, make sure the 'Enable JDBC Driver Deployment' option is selected. This option will ensure that the Connector/J driver is deployed to GlassFish when your project is deployed. (If the server is already running, you'll need to restart the server.)
  2. In the Projects window, right-click the PetCatalog project node and choose Run. The project is compiled, packaged and deployed to GlassFish. The deployed application then opens in your default browser.
    Pet Catalog application displayed in browser