NetBeans Platform CRUD Tutorial Using Maven

This tutorial demonstrates how to use the Maven build framework to create a simple NetBeans Platform application that can read from and write to a database. In this document you will use Maven archetypes to create the NetBeans Platform application and module, and the Swing UI toolkit and "Matisse" GUI Builder to create window components.

This document is based on the Ant-based NetBeans Platform CRUD Application Tutorial and can be used to understand some of the differences between using Ant and Maven to develop NetBeans Platform applications. After you understand how Maven is different from Ant, you can easily proceed through other tutorials on the NetBeans Platform Learning Trail.

Contents

Content on this page applies to NetBeans IDE 7.2

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

Software or Resource Version Required
NetBeans IDE version 7.2
Java Developer Kit (JDK) version 7 or above

Note: You do not need to download Maven because it is bundled with the IDE. Optionally, use your own download of Maven, in which case use the Options window to configure your Maven settings.

Note: This is not a beginners tutorial. Before starting this tutorial, it is important that you familiarize yourself with the following introductory Maven documentation:

Watch this YouTube movie before beginning to work on the tutorial because it shows the steps you will take below:

Creating and Running the Sample Application

In this section, you set up a sample application provided by NetBeans IDE. The sample application access data and displays data in a Maven-based NetBeans Platform application, similar to the application you create in this tutorial. Successfully running the sample application and displaying data will be useful in determining whether the database connection is set up correctly.

  1. Choose File > New Project (Ctrl-Shift-N) to open the New Project wizard. Select the Samples | Maven category and then select "CRUD Sample Application":

    screenshot

    Click Next.

  2. Specify a location:

    screenshot

    Click Next.

  3. Change, if needed, the location of libraries that the sample will use:

    screenshot

    Click Finish.

    Once the project is opened, if you see projects with [unloadable] in the display name, that means something is wrong with the POM file or that plugins are missing:

    screenshot

    Developers seeing the above in the IDE are typically new to Maven or are setting up Maven for the first time on their system.

    If you see the above, wait for the Maven dependencies to download, which is a process that you can follow by looking in the status bar of the IDE. Once the downloading has completed, and not before, right-click each project and choose "Build with Dependencies". Also see this issue if problems relating to missing plexus-utils.jar occur. If problems continue, refer to the links at the start of this document, to set up your Maven repository correctly.

  4. Examine the project structure:

    screenshot

    The project structure above is explained here:

    • NetBeans Platform Application. This project is a Maven reactor project for the NetBeans Platform application, which lists the modules to include and the location of the project's repositories. This project does not contain any sources. The IDE generates the modules containing the sources and resources in sub-directories of this project.
    • NetBeans Platform based application. The NetBeans Platform application project enumerates included modules; permits interactive runs; produces various kinds of packaging, such as ZIP by default, but optionally JNLP, NBMs; holds functional tests. Each module project specifies its own compile dependencies, while the app project's dependencies are anything additional that should be present at runtime. For example, that includes, by default, the whole platform cluster, that is, the set of modules constituting the "platform" cluster. The app project could include other clusters, or subsets of clusters, or whatever plugins you want included in your app which are not used as compilation dependencies. At a minimum, core.startup and its transitive dependencies are included.
    • Platform application branding resources. This project contains the global resources used for branding the application, such as the splash screen.
    • sample NetBeans Module. The project that contains the source code for accessing the database and displaying it in a window.
  5. You need to have access to a database server. This tutorial uses the JavaDB database server, together with a sample database. Before continuing, start the JavaDB server in the Services window by connecting to the sample database. Right-click on the connection node shown below and choose Connect:

    screenshot

    If it is not already available, an easy way to make the JavaDB database server available is to register an instance of GlassFish that is bundled with the IDE. The JavaDB database server and a sample database are included with the GlassFish server.

  6. Right-click the NetBeans Platform based application () and choose Run. The application starts up and should show the following:

    screenshot

    Use Window | Customer to open the Customer window, wait a few seconds, and you should see your data displayed:

    screenshot

  7. Open the editor window and notice that the selected customer's details are shown in the editor. Make changes in the editor and save them and then notice that the viewer is automatically updated. Changes you make are stored in the database.

Note: If you were not able to complete the above steps, do not continue with the rest of the tutorial. If you encountered problems relating to Maven repositories and unresolved Maven dependencies, refer to the introductory Maven links at the start of this document.

Since your database is being accessed correctly and your Maven dependencies have been correctly resolved, you are now ready to continue creating your own CRUD application.

Creating the NetBeans Platform Application

In this section you use the New Project wizard to create a NetBeans Platform application from a Maven archetype.

  1. If it is still open, close the sample application you created in the previous section. You do not need the sample anymore and leaving it open may cause confusion while you work on your real application.

  2. Choose File > New Project (Ctrl-Shift-N) to open the New Project wizard. Select NetBeans Application from the Maven category:

    screenshot

    Click Next.

  3. Type MavenPlatformCRUDApp for the Project Name and set the Project Location:

    screenshot

    Click Next.

  4. Select the version of the NetBeans APIs you'd like to use and type the name of a NetBeans module that you'd like to be created together with the application, in this case MavenPlatformCRUDApp-dbaccess:

    screenshot

    Click Finish.

    Depending on the availability of JARs as specified by Maven dependencies in your repository, the IDE will spend some time downloading Maven dependencies. Once the downloading of Maven dependencies is completed, you should see the following:

    screenshot

    The above project structure is explained here:

    • app. This project is a Maven reactor project for the NetBeans Platform application, which lists the modules to include and the location of the project's repositories. This project does not contain any sources. The IDE generates the modules containing the sources and resources in sub-directories of this project.
    • branding. This project contains the global resources used for branding the application, such as the splash screen.
    • dbaccess. The project that will contain the Java sources you will create in the next section.
    • parent. The NetBeans Platform application project enumerates included modules; permits interactive runs; produces various kinds of packaging, such as ZIP by default, but optionally JNLP, NBMs; holds functional tests. Each module project specifies its own compile dependencies, while the app project's dependencies are anything additional that should be present at runtime. For example, that includes, by default, the whole platform cluster, that is, the set of modules constituting the "platform" cluster. The app project could include other clusters, or subsets of clusters, or whatever plugins you want included in your app which are not used as compilation dependencies. At a minimum, core.startup and its transitive dependencies are included.
  5. Right-click the "parent" project and choose "Build with Dependencies". After the process has completed, right-click the "app" project and choose Run. You should see the default splash screen and then the main window of the application:

You have successfully set up the application and are now ready to begin generating the application's model classes.

Creating the Model

In this section you generate Java Persistence API (JPA) entity classes from tables in the Java DB database. To create the entity classes and to use JPA in your application, you need to have access to a database server and the JPA persistence provider libraries.

Though this tutorial uses the JavaDB database server, you can later configure the application to use other database servers.

Generating Entity Classes From the Database

In this section you use a wizard to generate entity classes in the dbaccess module.

  1. Right-click the Source Packages of the dbaccess module and choose New > Other. Select Entity Classes from Database in the Persistence category:

    screenshot

    Click Next.

  2. Select the Java DB sample database from the Database Connection drop-down list. Select the Customer table from the Available Tables list and click Add. When you click Add, the related tables, such as DiscountCode, which could vary depending on your version of the database, are also added to the list of Selected Tables list:

    screenshot

    Click Next.

  3. Leave the package name as suggested, that is, com.mycompany.mavenplatformcrudapp. Make sure that the Create Perisistence Unit and Generate Named Query Annotations are selected:

    screenshot

  4. Click Finish. When you click Finish, the IDE generates an entity class for each selected table. The IDE also generates the persistence.xml file in the META-INF package under the Other Sources node in the src/main/resources directory:

    screenshot

  5. Right-click the dbaccess module and choose Build. Depending on their availability in your repository, several JARs may now start to be downloaded.

Adding the DerbyClient as a Runtime Dependency

In this section you will add derbyclient.jar library as a dependency.

  1. Right-click the Dependencies node of the dbaccess module and choose Add Dependency:

    screenshot

    The Add Library dialog appears.

  2. Specify the library by typing org.apache.derby for the "Group ID", derbyclient for the "Artifact ID", select runtime from the Scope drop-down, and choose the latest version available:

    screenshot

    Notice that you can use code completion to help you throughout the above dialog:

    screenshot

    Click Add.

  3. Expand the Runtime Dependencies node in the Projects window and you can see that the derbyclient library is listed as a dependency:

    screenshot

You have now registered the Derby database dependency for your module.

In this section, you created a module that now contains entity classes for the tables you'd like to access, together with a persistence.xml file providing the data access information, and new dependency declarations in the project's POM file.

Creating the Viewer

In this section, we create a simple prototype GUI component that accesses our data and displays it.

  1. Right-click the dbaccess module and choose New | Window. Define the window to be opened in the "explorer" position and let it open when the application starts:

    screenshot

    Click Next.

  2. Set "Viewer" as the class name prefix.

    screenshot

    Click Finish.

  3. In the Source tab of the new window, redefine the constructor as follows:

    public ViewerTopComponent() {
    
        initComponents();
    
        setName(Bundle.CTL_ViewerTopComponent());
        setToolTipText(Bundle.HINT_ViewerTopComponent());
    
        setLayout(new BorderLayout());
        JTextArea area = new JTextArea();
        add(area, BorderLayout.CENTER);
    
        EntityManager entityManager = Persistence.createEntityManagerFactory("com.mycompany_MavenPlatformCRUDApp-dbaccess_nbm_1.0-SNAPSHOTPU").createEntityManager();
        Query query = entityManager.createNamedQuery("Customer.findAll");
        List<Customer> resultList = query.getResultList();
        for (Customer c : resultList) {
            area.append(c.getName() + " (" + c.getCity() + ")" + "\n");
        }
    
    }
  4. Right-click in the editor and choose Fix Imports (Ctrl-Shift-I). Make the choices below in the dialog that appears:

    screenshot

    Click OK. The import statements at the top of the class should now be as follows:

    import java.awt.BorderLayout;
    import java.util.List;
    import javax.persistence.EntityManager;
    import javax.persistence.Persistence;
    import javax.persistence.Query;
    import javax.swing.JTextArea;
    import org.netbeans.api.settings.ConvertAsProperties;
    import org.openide.awt.ActionID;
    import org.openide.awt.ActionReference;
    import org.openide.util.NbBundle.Messages;
    import org.openide.windows.TopComponent;
  5. Right-click the parent project and choose Clean. Right-click the app project and choose Build with Dependencies and, once the process has completed, right-click it again and choose Run.

    After the splash screen is shown, you should see this:

    screenshot

The simple prototype is finished. You're using very few NetBeans APIs at the moment, but you're able to retrieve data from your database and display it in your view component.

Creating the Editor

In this section, you add the start of an editor module to the application. You then continue with the instructions in the Ant based NetBeans Platform application tutorial to set up and listen to the selection and populate the editor with objects found there.

  1. In the New Project dialog, choose NetBeans Module in the Maven category:

    screenshot

    Click Next.

  2. Name the module MavenPlatformCRUDApp-editor. On the next page, set the project location to the folder where the other modules are found, which is within the parent folder. Click Next.

  3. Deselect the OSGi checkbox:

    screenshot

    Click Finish. Because you set the project location to be the folder where the parent project is found, you see that the new module is created and visible within the parent project:

    screenshot

  4. Though the new module is now part of the parent project, it is not part of the app project yet. Therefore, when you run the application, your module will currently not be included. Let's include it now. In the app project, right-click Dependencies, click Add Dependency, select the Open Projects tab, and choose the editor module.
  5. In the same way as you did in the previous section, create a new window, in the editor module. This time, it should be opened at start up in the "editor" position, with "Editor" as its class name prefix.

  6. Right-click the parent project and choose Clean. Right-click the app project and choose Build with Dependencies and, once the process has completed, right-click it again and choose Run.

    After the splash screen is shown, you should see this:

    screenshot

  7. Now that you have completed the steps above, you can refer to the Ant-based NetBeans Platform CRUD Application Tutorial, since all the Java code in that tutorial is applicable to the application you are creating here.

You have a basic understanding of how developing with Maven is different from Ant. You can easily proceed through other tutorials on the NetBeans Platform Learning Trail and apply them to your Maven scenarios.

 

See Also

This concludes the CRUD Tutorial. This document has described how to use the Maven build framework to create a new NetBeans Platform application with CRUD functionality. For more information about creating and developing applications, see the following resources.

If you have any questions about the NetBeans Platform, feel free to write to the mailing list, dev@platform.netbeans.org, or view the NetBeans Platform mailing list archive.

Project Features

Project Links

About this Project

Platform was started in November 2009, is owned by Antonin Nebuzelsky, and has 138 members.
By use of this website, you agree to the NetBeans Policies and Terms of Use (revision 20140418.2d69abc). © 2013, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo
 
 
Close
loading
Please Confirm
Close