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:
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.
Follow the next steps to create the database-ready Java Desktop 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:
After the selection of the Database Application:
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.
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)
Now let's talk about some other options, as well as some general information that may be useful for this task.
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:
After entering all the required information and following the given steps, a new database will be registered.
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.
Wondering how Bean Binding is working together with the Swing components?
DesktopApplicationDBView.java
and change to Design mode.This window will let you further configure the contents of the Binding.
3.142.166.55