This article shows you how to integrate a Java DB database into a NetBeans Platform application. We start by exploring a Java DB database, from which we create entity classes. Next, we wrap the entity classes into a module, together with modules for the related JPA JARS.
Note: These instructions are not applicable to Java DB only. Rather, they are relevant to any relational database, such as Oracle or MySQL. Several applications on the NetBeans Platform, many of which are listed here, use these databases too. Java DB was chosen for this article because it is easiest to get started with, since it comes with the JDK.
Once the above modules are part of our application, we create a new module that provides the user interface for our application. The new module gives the user a tree hierarchy showing data from the database. We then create another module that lets the user edit the data displayed by the first module. By separating the viewer from the editor in distinct modules, we will enable the user to install a different editor for the same viewer, since different editors could be created by external vendors, some commercially and some for free. It is this flexibility that the modular architecture of the NetBeans Platform makes possible.
When we have a module for our editor, we begin adding CRUD functionality. First, the "R", standing for "Read", is handled by the viewer described above. Next, the "U" for "Update" is handled, followed by the "C" for "Create", and the "D" for "Delete".
At the end of the article, you will have learned about a range of NetBeans Platform features that help you in creating applications of this kind. For example, you will have learned about the UndoRedo.Manager and the ExplorerManager, as well as NetBeans Platform Swing components, such as TopComponent and BeanTreeView.
- Setting Up the Application
- Integrating the Database
- Creating Entity Classes from a Database
- Wrapping the Entity Class JAR in a Module
- Creating Other Related Modules
- Designing the User Interface
- Setting Dependencies
- Running the Prototype
- Integrating CRUD Functionality
The application you create in this article will look as follows:
Source code: http://kenai.com/projects/nbcustomermanager
Once you're at the stage shown above, you can simply download a NetBeans module that provides Office LAF support (described here), add it to your application, and then when you redeploy the application, you will see this:
Note: It is advisable to watch the screencast series Top 10 NetBeans APIs before beginning to work on this article. Many of the concepts addressed in this article are discussed in more detail within the screencast series.
Setting up the Application
Let's start by creating a new NetBeans Platform application.
- Choose File > New Project (Ctrl+Shift+N). Under Categories, select NetBeans Modules. Under Projects, select NetBeans Platform Application. Click Next.
- In the Name and Location panel, type DBManager in the Project Name field. Click Finish.
The IDE creates the DBManager project. The project is a container for all the other modules you will create.
Run the application and notice that you have quite a few features out of the box already. Open some of the windows, undock them, and get to know the basic components that the NetBeans Platform provides without you doing any work whatsoever:
Integrating the Database
In order to integrate the database, you need to create entity classes from your database and integrate those entity classes, together with their related JARs, into modules that are part of your NetBeans Platform application.
Creating the Entity Classes
In this section, you generate entity classes from a selected database.
- For purposes of this example, use the Services window to
connect to the sample database that is included with
Note: Alternatively, use any database you like and adapt the steps that follow to your particular use case. In the case of MySQL, see Connecting to a MySQL Database.
- In the IDE, choose File | New Project, followed by Java | Java Class Library to create a new library project named CustomerLibrary.
- In the Projects window, right-click the library project
and choose File | New File, followed by Persistence | Entity
Classes from Database. In the wizard, select your database and the tables you
need. Here we choose "Customer", and then "Discount Code" is added automatically, since
there is a relationship between these two tables.
- Specify the persistence strategy, which can
be any of the available options. Here, since we need
to choose something, we'll
- Specify "demo" as the name of
the package where the entity classes will be generated.
- Click Finish. Once you have completed this step, look at the generated code and notice
that, among other things, you now have a persistence.xml file in a folder
called META-INF, as well as entity classes for each of your tables:
- Build the Java Library and you will have a JAR file in the library
project's "dist" folder, which you can view in the Files window: