Connecting your Application to a Database

Creating a Java Desktop application that communicates with a database using NetBeans is a breeze.

From the Project wizard, it is possible to create a basic GUI with the Create, Retrieve, Update, Delete (or CRUD) functionality that accesses a database.

The creation of such an application is simplified by the fact that NetBeans supports, out of the box, both Java Persistence API (JPA) and Beans Binding, besides supporting the Swing Application Framework as well.

The Java SDK 6 and GlassFish Server come bundled with a lightweight database, Java DB, which is a rebranded version of Apache Derby. Our recipes will be using Java DB since it's already bundled with JDK and is registered by NetBeans Services (so long as the chosen NetBeans installation package has GlassfIsh Server included). It is also possible to configure other Databases with NetBeans, which will cover in later chapters.

Java DB is not intended for production applications, which require databases with more features. For our purposes, however, the Java DB will more than suffice.

If you wish to learn more, please refer to:

Getting ready

If another version of NetBeans is downloaded, one without GlassFish Server, the Java SDK version 6 comes with Java DB bundled.

Find the Java SDK 6 on:

http://www.oracle.com/technetwork/java/javase/downloads/index.html

If GlassFish is already installed, then Java DB will be detected by NetBeans and it will be automatically configured. If that is not the case, it is necessary to manually download, install, and register it.

How to do it...

Follow the next steps to create the database-ready Java Desktop Application:

  1. Navigate to the Projects window.
  2. Either right-click on the window and select New Project... or use the shortcut Ctrl+Shift+N.
  3. On the right side of the New Project window under Categories, select Java.
  4. In the same window, but under Projects: select Java Desktop Application.
  5. Click Next, and Next again after the disclaimer.
  6. When the third step, Name and Location, is reached under Project Name, type DesktopApplicationDB.
  7. For the other fields, Project Location, Project Folder, and Application Class, leave the default information.
  8. Under Choose Application Shell, select Database Application.

Upon making this selection, two more steps are added that are database-specific. The following screenshot shows what the screen should look like then:

How to do it...

After the selection of the Database Application:

  1. Click Next >.
  2. Under Database Connection, select the Java DB Sample APP.
  3. Under Database Table, select CUSTOMER.
  4. Then on the left-hand side, on Columns to Include, select all of the following (to select multiple fields press and hold Ctrl while selecting them with the mouse):
    • CUSTOMER_ID
    • NAME
    • ADDRESSLINE1
    • PHONE
    • EMAIL
    • CREDIT_LIMIT
  5. Press< to send all the selected fields to Available Columns.
  6. Then Press Next.
  7. On the Detail Options, under Create Details Area As, select Text Fields.
  8. Click Finish.
How to do it...

When the configuration of the Java Desktop Database Application is complete, it is possible to execute the project either by pressing F6, or by right-clicking on the project and selecting Run.

How it works...

The project creation and database configuration are pretty straightforward steps. At this point, the IDE is not doing anything different from a normal Java Desktop Project.

The JPA magic starts when selecting the table columns that are going to be included in the GUI. NetBeans then creates an entity class, Customer.java in our case, which is used to map the contents from the database to the GUI. The persistence.xml file, under the META-INF package, is used to configure the database connection and is tied to the Customer entity class.

The generated table is rendered in Swing and populated with content from the database, thanks to Bean Binding.

NetBeans generates code in the GUI class which makes it possible to edit the fields, save and delete information from the database, and refresh it.

Some of these methods are:

  • newRecord()
  • deleteRecord()
  • refresh()
  • isSaveNeeded()
  • setSaveNeeded(boolean)

There's more...

Now let's talk about some other options, as well as some general information that may be useful for this task.

Registering a new database

If you wish to register a new database, (it is also possible to register other Databases besides Java DB), the next steps must be followed:

  1. Select the Services window.
    Registering a new database
  2. Right-click on Databases and select New Connection....
  3. In the New Database Connection window, under Driver Name, select the available driver or search for a new driver for your choice of database.
  4. Fill the rest of the information related to the configuration of your database.

After entering all the required information and following the given steps, a new database will be registered.

Checking the contents of Customer.java

It is worth taking a look at the Customer.java entity class in order to understand the work that the IDE performs.

The IDE automatically creates SQL queries, adds support for updating (when the values of the query are edited in the GUI), adds and removes listeners, implements hashCode and equals methods, and more.

Looking even further, in DesktopApplicationDBView.java, it is possible to discover more code that is not present in a normal Java Desktop Application.

Using the Inspector to check for Binding

Wondering how Bean Binding is working together with the Swing components?

  1. Select DesktopApplicationDBView.java and change to Design mode.
  2. Expand the nodes Form DesktopApplicationDBView, mainPanel, masterScrollPane, and then right-click on masterTable and select Properties.
  3. In Properties window, click on Binding.
  4. Expand the Preferred option.
  5. Inside the elements box list, click on the box.
  6. A Bind masterTable.elements window pops up.

This window will let you further configure the contents of the Binding.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.142.166.55