Building A Java Desktop Database Application: Page 1 of 13
Building A Java Desktop Database Application: Page 1 of 13
Note: JSR-296 (Swing Application Framework) is no longer developed and will not become part of the
official Java Development Kit as was originally planned. You can still use the Swing Application
Framework library as it is, but no further development is expected. If you are looking for a Swing-
based application framework, consider using the NetBeans Platform, which is a full-featured platform
suitable for creating complex and scalable desktop applications. The Platform contains APIs that
simplify the handling of windows, actions, files, and many other typical application elements.
Refer to the NetBeans Platform CRUD Application Tutorial, which describes a similar scenario.
This tutorial shows how to create a desktop Java application through which you can access and update
a database. The tutorial takes advantage of support in NetBeans IDE for the following technologies:
• The Java Persistence API (JPA), which helps you use Java code to interact with databases.
• Beans Binding (JSR-295), which provides a way for different JavaBeans components to have
property values that are synchronized with each other. For example, you can use beans binding
to keep the values of cells in a JTable visual component in synch with the values of fields in an
entity class. (In turn, the entity class represents the database table.)
• The Swing Application Framework (JSR-296), which provides some useful building blocks for
quickly creating desktop applications.
We will create a database CRUD (create, read, update, delete) application with a custom component
used for visualizing the data (car design preview).
This tutorial is largely based on a screencast that was based on a development build of a previous
version of the IDE. Some of the user interface has changed since that demo was made, so you might
notice some differences between this tutorial and the demo. You can view the demo (about 9 minutes)
now or download a zip of the demo.
Expected duration: 45 minutes
Contents
Page 1 of 13
• Building and Deploying the Application
• Next Steps
To complete this tutorial, you need the software and resources listed in the following table.
Software or Resource Version Required
NetBeans IDE version 6.9 or 7.0
Java Development Kit (JDK) version 6
Java DB database server (Java DB is included with JDK
6)
Car database SQL script
CarPreview project
Page 2 of 13
folder there if you wish.
Once the server is started, Java DB Database Process tab opens in the Output window and displays a
message similar the following:
Apache Derby Network Server - 10.6.2.1 - (999685) started and ready
to accept connections on port 1527
Page 3 of 13
3. Expand the connection node and note that there are several schema subnodes. Right-click the
APP node and choose Set as Default Schema.
4. Expand the APP schema node, right-click its Tables subnode, and choose Execute Command.
5. Copy the contents of the car.sql file and paste them into the SQL Command tab of the SQL
Source Editor.
This is the SQL script which will populate the database with data about cars.
6. Click the Run SQL button ( )in the toolbar of the SQL Editor to run the script.
7. Right-click the Tables node of the APP schema and choose Refresh to view the new Car table.
Page 4 of 13
• Persistence of its window state between sessions. When you close the application, the window
position and size are remembered. So when you reopen the application, the window opens in the
same position as it was when you closed it.
• An About dialog box, which you can easily customize.
• .properties files containing the labels in the user interface. Using .properties files is a
good way to keep the logic of your code separate from the text that appears in the user interface
of your application. Such separation is useful for making it easier to localize your program,
among other reasons.
To see some of the features that are already built into the application, follow these steps:
1. Right-click the project's node and choose Run.
After a few seconds, the application starts and a window called Database Application Example
appears. This window contains a table and several controls that enable you to edit the CARS
database.
2. Select the first record in the table (for the Acura).
3. Select the Price text field and replace the existing value with 46999. Then press Enter.
The value should appear updated in the table. (However, that value will not be reflected in the
database until you click Save.)
Similarly, you can update any other values in the table.
4. Click New to create a new record. Then fill in values for each of the fields (Make, Model, Price,
Body Style, Color). For example, you can fill in Trabant, Classic, 1000, wagon, and
blue. Click Save to save the entry in the database.
5. Click the Database Application Example title bar and drag the application to a different place on
your screen.
6. Click the left border of the Database Application Example window and drag to the left to
increase the size of the window.
7. In the Database Application Example menu bar, choose File > Exit.
8. In the IDE, right-click the project's node and choose Run Project.
The Database Application Example window will open in the same size and position it was in
when you closed the application.
Page 5 of 13
• The entityManager, query, and list objects, which are defined in the CarsView class
and which are listed in the Inspector window.
• The entity manager object is used to retrieve and commit data within the defined
persistence unit scope.
• The query object defines how the particular data collection is retrieved from the entity
manager. (You can change the way that the query object works by selecting the query
object in the Inspector window and changing the query property in the property sheet.
The query property uses JPA query language.
• The list object is an observable collection that holds the data from the query. An
observable collection is a special kind of collection on which you can place a listener to
find out when changes to the collection have been made.
Using the Inspector window and the property sheet, you can follow these steps to see how the JTable is
bound to data:
1. In the Inspector window, select the mainPanel[JPanel] > masterScrollPane [ScrollPane] >
masterTable [JTable] node. Then click the Binding tab in the Properties window.
2. Look at the elements property to confirm that it is bound to a list.
3. Click the ellipsis [...] button to open the Bind masterTable.elements customizer, where you can
further customize the binding between the table and the database. For example, you can see that
the customizer enables you to specify which table columns are bound.
Besides the Binding category in property sheet you can also use the Bind menu in context menu.
Note: If you drop the component in a place you do not want and thus cause several undesired
layout changes, you can use the Undo command to reverse the changes. Choose Edit > Undo or
press Ctrl-Z.
2. If necessary, stretch the slider to the left to align it with the left side of the text field
components.
Page 6 of 13
3. Stretch the slider to the right to span the whole form width.
4. Add a label to the left of the slider and set its text to Tire Size. (Click the label to make it
editable.)
5. Add another slider below the first slider, and adjust its width and alignment where necessary.
6. Add another label below the Tire Size label and set its text to Modernness.
7. Add two checkboxes below the sliders. Set their text to Spoiler and Sun Roof. (Make the
display text editable by clicking the checkbox once, pausing, and then clicking the checkbox
again. You can also right-click the checkbox and choose Edit Text.)
The form should look like the screenshot shown below.
Page 7 of 13
3. In the Binding Expression drop-down list, select the selectedElement > tiresize node
and click OK.
Page 8 of 13
3. Right-click the Databases > jdbc:derby;//localhost:1527/car_database >
APP > Tables > CAR node and choose View Data.
4. Look at the SUN_ROOF, SPOILER, TIRE_SIZE, and MODERNNESS values for the first
record.
5. Choose Run > Run Main Project to execute the application.
The running application should look similar to the screenshot shown below.
Page 9 of 13
To make the CarPreview component available for the CarsApp project:
1. If you have not already done so, download the CarPreview.zip file.
2. Using a standard zip tool, extract the archives of the zip file.
3. Choose File > Open Project and navigate into the extracted contents of the zip file and select the
CarPreview project.
4. Click Open Project.
The project opens in the IDE.
5. Right-click the CarPreview node and choose Clean and Build.
This compiles the files in the project so that you can use the CarPreview class as a bean that can
be dragged and dropped directly on to the form.
This component was written as a JavaBeans component, so you could add it to the Palette, which
would be convenient for adding the component to multiple applications. But for now we will simply
drag the component directly into your application directly from the Projects window.
To add the CarPreview component to the application:
1. In the Projects window, expand the Car Preview > Source Packages > carpreview node.
2. Drag the CarPreview.java class to the form. To insert it properly just below the menus,
place it over the table left aligned with the other controls and snapped to the bottom of the menu
bar, as shown in the image below.
Page 10 of 13
Building and Deploying the Application
Once you are satisfied that your application works properly, you can prepare the application for
deployment outside of the IDE. In this section you will build the application's distributable outputs and
run the application from outside of the IDE.
Page 11 of 13
Notes: There is also a Build command, which does not delete old build artifacts, but this command is
disabled by default. See the Compile on Save section of the Creating, Importing, and Configuring Java
Projects guide for more information.
To build the application:
• Choose Run > Clean and Build Main Project (Shift-F11).
Output from the Ant build script appears in the Output window. If the Output window does not appear,
you can open it manually by choosing Window > Output > Output.
When you clean and build your project, the following things occur:
• Output folders that have been generated by previous build actions are deleted ("cleaned"). (In
most cases, these are the build and dist folders.)
• build and dist folders are added to your project folder (hereafter referred to as the
PROJECT_HOME folder).
• All of the sources are compiled into .class files, which are placed into the
PROJECT_HOME/build folder.
• A JAR file containing your project is created inside the PROJECT_HOME/dist folder.
• If you have specified any libraries for the project (in addition to the JDK), a lib folder is
created in the dist folder. The libraries are copied into dist/lib.
• The manifest file in the JAR is updated to include entries that designate the main class and any
libraries that are on the project's classpath.
Page 12 of 13
Distributing the Application to Other Users
Now that you have verified that the application works outside of the IDE, you are ready to distribute
the application.
To distribute the application:
1. On your system, create a zip file that contains the application JAR file (CarsApp.jar) and
the accompanying lib, which contains the other JAR files that the application needs.
2. Send the file to the people who will use the application. Instruct them to unpack the zip file,
making sure that the CarsApp.jar file and the lib folder are in the same folder.
3. Instruct the users to follow the steps in the Running the Application Outside of the IDE section
above.
Page 13 of 13