Connecting JavaFX Application to Database

Another common scenario is connecting an application to a database.

In this recipe, we will see how to create a JavaFX Desktop application with form and index buttons.

Getting ready

To continue with this recipe, NetBeans and the set of plugins that bundle JavaFX must be installed.

If unsure how to proceed, please refer to the recipe, Creating a JavaFX Project, specifically the Getting ready section.

We also need JavaDB Database running.

To do this:

  1. Navigate to the Services window.
  2. Expand the Database section.
  3. Right-click on jdbc:derby://localhost:1527/sample [app on APP] and select Connect....
  4. The following screenshot shows the active connection to JavaDB:
Getting ready

How to do it...

We need to create a New Project:

  1. Click on File and then New Project (or Press Ctrl+Shift+N).
  2. Choose Project: In the New Project window, on the Categories side, choose JavaFX and on the Projects side, select JavaFX Desktop Business Application. Then click Next.
  3. Under Project Name, enter DesktopDBJavaFXApp, leave all the other fields with their default values and click Finish.
  4. On the Palette window, navigate to Data Sources and drag-and-drop the JDBC Data Source to Main.fx.
  5. On the Data Source Customizer dialog, click on Browse....
  6. Select jdbc:derby://localhost:1527/sample [app on APP] and click OK.
  7. Then click the Create... button.
  8. On the Create SQL Query dialog, select CUSTOMER under tables, and click OK.
  9. Back on the Create SQL Query dialog, click on Fetch Data to check that everything is fine, then click OK.
    How to do it...
  10. Navigate to the Palette window and under the Templates section, drag-and-drop the Desktop Form component inside Main.fx.
  11. On the Customize Template dialog, click on the Data Source drop-down and select jdbcDataSource, click on Fetch to see all the fields that are going to be added, and then click OK.
  12. On the Palette window, under the Templates section, drag-and-drop Index Buttons.
  13. On the Customize Template dialog, click on the Indexed Component drop-down, select jdbcDataSource, and click OK.
  14. We need to add JavaDB JARs to our project. Right-click on DesktopDBJavaFXApp and select Properties.
  15. Click on Libraries and then Add JAR/Folder.
  16. Navigate to the JavaDB installation folder inside the JDK 6 folder, and select derby.jar and derbyclient.jar.
  17. Click OK.

Save the file and click on Run the Main Project.

How it works...

The JDBC Data Source is responsible for connecting/fetching/updating our database. This is done by executing SQL queries in the background.

These actions are transparent for the developer since the JDBC Data Source is responsible for abstracting all of the work and only lets the developer customize the settings through the GUI.

The Create SQL Query dialog is used in our case, to perform a selection of all of the entries from the Customer table.

Different SQL queries could be used in conjunction with the Data Source Customizer, but as we only want to display the data, this is the query used in the recipe.

After configuring the connection to the Database, we will need to actually bind the connection to some component.

The Desktop Form component is responsible for showing the data. When customizing it, we add as our Data Source the jdbcDataSource, which we have configured with the Customer table. This will provide the application with all the data, presented in a well-formatted manner.

For our app to work, we need to add the Java DB JARs otherwise the app won't behave correctly.

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

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