Creating a Custom Java Desktop Database Application

This tutorial guides you through the creation of a complete desktop database application that enables its user to browse and edit customer records and purchase history. The resulting application includes the following main features:

  • A main view that enables users to browse customer records and customer purchases.
  • A search field for the customer records.
  • Separate dialog boxes for entering new records or modifying existing records.
  • Code to interact with multiple related database tables.


Content on this page applies to NetBeans IDE 6.0 and 6.1

To follow this tutorial, you need the following software and resources.

Software or Resource Version Required
NetBeans IDE Version 6.1
Java Development Kit (JDK) Version 6 or
version 5
SQL script to create the database tables
zip file of utility classes

Note: You can download the final working project created in this tutorial at any time and open it in the IDE to view the classes. If you want to run the downloaded project, be sure to clean and build it before running.


This application takes advantage of the following technologies:

  • The Java Persistence API (JPA), which helps you interact with a database using Java code.
  • Beans Binding, which enables you to keep Swing component properties synchronized.
  • The Swing Application Framework, which simplifies basic application functions such as persisting session information, handling actions, and managing resources.

The tutorial makes use of IDE wizards and other code generation features to provide much of the boilerplate code. It also shows you how to customize the generated code and hand code other parts of the application.

This tutorial takes approximately 2 hours to complete. For a shorter tutorial that shows the creation of a less customized user interface, see Building a Java Desktop Database Application.

Below is a screenshot of the working application that you will have when you complete the tutorial.

A screenshot of an application with a
          main frame that has a master/detail view of customer and order records.
          There is a search field that has the letters jul entered, and the two
          displayed customer records both have the jul string. In addition,
          an Order Editor dialog is displayed and contains an open combo box
          showing items to select from.

Setting Up the Database

Before creating any application code, we will set up the database. We will then be able to take advantage of wizards that generate much of the application code based on the structure of the database.

The instructions in this tutorial are based using a MySQL database that you create with this SQL script.

Note: You can use other database management software, but doing so might require making some adjustments to the SQL script. In addition, you will need to create the database from outside of the IDE.

To set up the IDE to work with MySQL databases, see the Connecting to a MySQL Database page.

To create the database:

  1. In the Services window, right-click the MySQL Server node and choose Start.
  2. Right-click the MySQL Server node and choose Create Database.

    If the Create Database item isn't enabled, choose Connect. You might then need to enter a password. The Create Database item should then be enabled.

  3. For Database Name, type MyBusinessRecords and click OK.
  4. In the New Database Connection dialog, type nbuser as the password and click OK.
  5. If the Advanced tab of the dialog box opens, click OK to close the dialog box.
  6. Scroll down to the node for connection that you have just created. The node should have the connection node icon.
  7. Right-click the connection node and choose Execute Command.
  8. Copy the contents of the MyBusinessRecords SQL script and paste them into the SQL Command 1 tab of the Source Editor.
  9. Click the Run SQL button (Run SQL button)in the toolbar of the Source Editor to run the script.

    Output of the script should appear in the Output window.

  10. Right-click the connection node again and choose refresh.
  11. Expand the node, and expand its Tables subnode.

    You should see four database tables listed.

The database structure was designed with normalization and referential integrity in mind. Here are some notes on the structure:

  • The data is split among several tables to reduce duplication and the possibility for inconsistencies. Some tables are connected to each other through foreign keys.
  • All of the tables use MySQL's AUTO_INCREMENT attribute so that there is a unique identifier for each row in those tables. This identifier is created by the database management software, so your application and/or your application's user do not have to create this identifier. (For AUTO_INCREMENT to work properly within the application, you need to add the @GeneratedValue(strategy=GenerationType.IDENTITY annotation for that column in the table's entity class. Otherwise, the application does not try to submit a value for that column when you create a new record.)
  • The foreign key in the ORDERS table is there to link each order record with a customer. In the application's user interface, ORDER records are only displayed for the selected CUSTOMER.
  • The ON CASCADE DELETE attribute for the foreign key to the CUSTOMERS class ensures that a customer's orders are also deleted when a customer is deleted.
  • The foreign key in the CUSTOMERS table points to a COUNTRIES table. We will use this relationship in the application to enable the user to select a customer's country from a combo box.
  • The ORDERS table has a foreign key to the PRODUCTS table. When adding a new order record, the user will be able to choose a product from a combo box.
  • The COUNTRIES and PRODUCTS tables are pre-populated with data so that you can choose from those tables when the user of the application is adding customer and order records.
  • Though this tutorial does not cover it, you might find it useful to create separate applications to populate the COUNTRIES and PRODUCTS tables. Such applications could be created with the Java Desktop Application project template and would not require additional hand-coding.

Creating the Application Skeleton

We will use the IDE's Java Desktop Application project template to create much of the base code for the application. This template generates code for the following features:

  • Connection to the database.
  • A main application frame that contains tables for customer details and customer orders.
  • A main application class that handles basic application life-cycle functions, including persisting of window state between sessions and resource injection.
  • Actions (and corresponding buttons) for standard database application commands.

Read the rest of this tutorial.

For general tips and tricks on using the GUI Builder in NetBeans IDE, see the GUI Editor FAQ and Patrick Keegan's web log.


Get Started with Java Programming!

Now Facebook users have a page to lead them to content that can help them learn Java programming. Log in to your Facebook account and Become a Fan of  Get Started with Java Programming!


I really appreciate your help.
I was interested in finding out how i could generate results from a database based on combo box selection items. that is, a report based on queries generated from items specified in combo boxes.
It is a desktop application.

Posted by hilla on January 15, 2009 at 09:09 PM PST #

Post a Comment:
Comments are closed for this entry.

Java Technology Fundamentals are now covered as part of the Core Java Technology Tech Tips. Please go there for more tech tips.


« July 2016