Skip to main content

RESTful representation of "sakila" using GlassFish and NetBeans IDE

Posted by arungupta on January 24, 2008 at 4:44 AM PST

"sakila" is the
sample database shipped with MySQL (pronounced as
my ess-kew-ell). In the context of Sun

the agreement to acquire MySQL, I'd like to dedicate this entry
to show how this sample database can be exposed as a RESTful Web service
endpoint and deployed on GlassFish
Tooling Plugin
(0.4.1 with Jersey
0.4) in NetBeans IDE.

Lets get started!

  1. Install MySQL & the sample database "sakila".
    1. Download and
      MySQL Community Server.
    2. Download sakila
      sample database
    3. Install the database as described

    4. Start MySQL database by giving the command '
      mysqld-nt --user root 
      ' in bin directory on Windows or './bin/mysqld_safe'
      from MySQL directory on Unix flavors.
  2. Create the Project & Database Connection
    1. In NetBeans IDE, create a new Web project and name it as "sakila".
      Choose "GlassFish v2" as the "Server:".
    2. In the "Services" tab of NetBeans IDE, expand "Drivers" and add
      driver if it does not exist already.
    3. Create a new new database connection by right-clicking on "Drivers" and
      specifying the parameters as shown below:

  3. Create the Persistence Unit
    1. Right-click on the project and select "New", "
      Entity Classes from 
      ". In "Data Source", select "New Data Source..." and specify the
      values as shown below:

    2. Click on "film" in "Available Tables" and click on "Add >" as shown

      Click on "Next >".
    3. Click on "Create Persistence Unit..." and take all the defaults as shown

      Click on "Create".
    4. Enter the package name as "sakila" as shown below:

      and click on "Finish".
    5. In the NetBeans project explorer, expand "Configuration Files"
      and open "persistence.xml". Specify the username and password
      by replacing <properties/> with the following fragment:


        <property name="toplink.jdbc.user" value="root"/>

        <property name="toplink.jdbc.password" value=""/>


      Make sure to match the username and password to your MySQL installation.
  4. Create RESTful Web service endpoint
    1. In NetBeans IDE, click on "Tools", "Plugins", "Available Plugins", "
      Web Services
      " and then click on "Install". This installs the
      Jersey Tooling
      in the IDE.
    2. Right-click on the project, select "New", "
      RESTful Web Services from 
      Entity Classes...
    3. Click on "Add >>", take all other defaults as shown

      click on "Next >", take all defaults and then "Finish".
  5. Test RESTful Web Services
    1. Right-click on the project and select "Test RESTful Web Services".
      The following web page is presented in the browser:

    2. Click on "films" and then on "Test" as shown

      Clicking on "Test" button or the URL "http://localhost:8080/sakila/resources/films/"
      shows the RESTful representation of the "Film" table. The
      default representation shows 10 records from the table where each entry
      returns the "id" of the film and a reference to the detailed

      You can view more entries (say 40) by giving the URL "http://localhost:8080/sakila/resources/films/?max=40".
      Additional fields from the table can be displayed by adding getter methods
      to "converter.FilmRefConverter" class such as:


      public String getTitle() {

        return entity.getTitle();


      to return the film title in addition to the fields already returned. The
      different columns in the table can be viewed by going to the "Services" tab,
      expanding the sakila database connection created earlier as shown below:

      modified output (with film title included) looks as shown below:

Here are few more ideas for you to explore:

  • Create RESTful representations of other tables using the steps described
  • Display the data from different tables in a
    jMaki-wrapped Yahoo or Dojo data table as
    explained in
  • Display the data retrieved from the database in a JSP page as described
    in Hello JPA
  • Create a CRUD application using jMaki Data Table as described in

    TOTD #15

    Screencast #Web10

A JRuby-on-Rails application using MySQL is explained
explains how JDBC connection pooling in GlassFish can be used for a
JRuby-on-Rails application using


The key message here is MySQL can be very easily used with GlassFish and NetBeans
makes it possible! Once MySQL becomes part of Sun, this integration is going to
be much more seamless for the betterment of community.

All the entries on this blog using MySQL can be found
here. And last but not
the least, Welcome aboard MySQL!

A NetBeans project with all the source code can be downloaded from

. You will still need to setup the database connection and need to make
sure the correct version of Jersey plug-in as well :)

Technorati: glassfish

Related Topics >>



wasn't it pronounced like 'my see-kwall'?