Channels ▼
RSS

JVM Languages

The JavaFX JDBC Data Source


Inside the Code

When you work with the JavaFX Data Source component, as well as the JavaFX Composer, you'll notice that you don't need to manually write any code. However, there may be times you'd like to go further than this example or what the Composer allows you to do. Fortunately, you can take a look at the raw JavaFX Script that's generated, which you can use as a starting point in the future. To do this, switch the file to Source mode in NetBeans, and expand the "Generated Code" section in the listing.

As you scroll through the code, you'll find the object declarations for the components you dragged onto the Scene. For instance, the code below is for the two list view controls you added, and includes the bind statements that were generated by the Composer:


public-read def listView: ListView = ListView {
   layoutX: 17.0
   layoutY: 35.0
   layoutInfo: __layoutInfo_listView
   items: bind for(record in jdbcBooks.getRecordSet().all()) /
          record.getString("NAME")
}
 
public-read def listView2: ListView = ListView {
   layoutX: 288.0
   layoutY: 35.0
   layoutInfo: __layoutInfo_listView2
   items: bind for(record in jdbcPatron.getRecordSet().all()) /  
          "{record.getString("FIRSTNAME")} {record.getString("LASTNAME")}"
}
 

Connecting the two JDBC data source components involves only two straightforward object declarations:

 
public-read def jdbcBooks: DbDataSource = DbDataSource {
   connectionString: "jdbc:derby:/db/LibraryApp/librarydb"
   user: "dbuser"
   password: "dbuser"
   query: "select * from APP.BOOK"
}
   
public-read def jdbcPatron: DbDataSource = DbDataSource {
   connectionString: "jdbc:derby:/db/LibraryApp/librarydb"
   user: "dbuser"
   password: "dbuser"
   query: "select * from APP.PATRON"
}
 

Since JavaFX Script is a declarative language (as opposed to an imperative language), you simply state what you want to do, and the run-time takes care of how to do it. The rest of the code includes labels and the button, along with some layout, but the JavaFX Script object declarations and the bind statements that connect them do all of the work. Listing 1 contains the the source for the entire application.


package desktopapplication;

/**
* @author ericjbruno
*/

public class Main {
    def __layoutInfo_listView: javafx.scene.layout.LayoutInfo = javafx.scene.layout.LayoutInfo {

        width: 254.0
    }
    public-read def listView: javafx.scene.control.ListView = javafx.scene.control.ListView {
        layoutX: 17.0
        layoutY: 35.0
        layoutInfo: __layoutInfo_listView
        items: bind for(record in jdbcBooks.getRecordSet().all()) record.getString("NAME")
    }
    def __layoutInfo_listView2: javafx.scene.layout.LayoutInfo = javafx.scene.layout.LayoutInfo {
        width: 178.0
    }
    public-read def listView2: javafx.scene.control.ListView = javafx.scene.control.ListView {
        layoutX: 288.0
        layoutY: 35.0
        layoutInfo: __layoutInfo_listView2
        items: bind for(record in jdbcPatron.getRecordSet().all()) "{record.getString("FIRSTNAME")} {record.getString("LASTNAME")}"
    }
    public-read def label: javafx.scene.control.Label = javafx.scene.control.Label {
        layoutX: 17.0
        layoutY: 13.0
        text: "Library Books:"
    }
    public-read def label2: javafx.scene.control.Label = javafx.scene.control.Label {
        layoutX: 288.0
        layoutY: 13.0
        text: "Library Patrons:"
    }
    public-read def btnCheckOut: javafx.scene.control.Button = javafx.scene.control.Button {
        layoutX: 395.0
        layoutY: 293.0
        text: "Check Out"
    }
    public-read def scene: javafx.scene.Scene = javafx.scene.Scene {
        width: 480.0
        height: 320.0
        content: getDesignRootNodes ()
    }
    public-read def jdbcBooks: org.netbeans.javafx.datasrc.DbDataSource = org.netbeans.javafx.datasrc.DbDataSource {
        connectionString: "jdbc:derby:/db/LibraryApp/librarydb"
        user: "dbuser"
        password: "dbuser"
        query: "select * from APP.BOOK"
    }
    public-read def jdbcPatron: org.netbeans.javafx.datasrc.DbDataSource = org.netbeans.javafx.datasrc.DbDataSource {
        connectionString: "jdbc:derby:/db/LibraryApp/librarydb"
        user: "dbuser"
        password: "dbuser"
        query: "select * from APP.PATRON"
    }
    init {
    }
    public-read def currentState: org.netbeans.javafx.design.DesignState = org.netbeans.javafx.design.DesignState {
        names: [ ]
        timelines: [
        ]
    }
    public function getDesignRootNodes (): javafx.scene.Node[] {
        [ listView, listView2, label, label2, btnCheckOut, ]
    }
    public function getDesignScene (): javafx.scene.Scene {
       scene
    }
}
function run (): Void {
    var design = Main {};
    javafx.stage.Stage {
        title: "Main"
        scene: design.getDesignScene ()
    }
}

Listing 1: The JavaFX JDBC Data Source sample application (Main.fx)

Conclusion

When working with JavaFX Script alone, performing tasks such as UI layout and database connectivity are very straightforward, and far less complicated compared with Java and Swing. Additionally, NetBeans and the JavaFX Composer take it a step further, and make building applications -- even database applications -- as simple as drag-and-drop. As with most frameworks, there's always some coding involved, but in this case it amounted to modifying two lines of code (the two bind statements). Working with files and web-based data in comma-delimited, line-deliminted, XML, or JSON formats are equally as straightforward. I plan to extend this article to cover those two formats in the near future.


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.
 

Video