Channels ▼


JavaFX Database Programming with Java DB

JavaFX Native Arrays

Passing an object from JavaFX code to Java code is straightforward. However, when passing an array of Java objects, you need to explicitly declare the array as native. For instance, assume we modified the Library application where multiple books can be selected from the books list, and then borrowed by a single patron at once. We simply create an array of Book objects to be passed to the Database object. This array is declared as a nativearray, with the Java type specified as in:

var books: nativearray of Book;

To populate this array, we must use the { } syntax, such as with the following String variable:

var bookInfo: String = "{} (author: {})";

The expressions between the braces are evaluated, and their values placed into the String. For example, the above line of code may evaluate to the following:

Java Messaging (author: Eric Bruno)

Notice that the name of the book is inserted into the string in place of {}, and so on. The same concept and syntax works in other areas of JavaFX Script as well, such as when populating native arrays. For instance, the code below is used to check out multiple books to one patron:

function checkoutMultipleBooks(): Void {
    // Get the selected patron
    var patron: Patron = 
    var booksSelected = getBooksSelected();

    // Create a native array to pass to Java class
    var books : nativearray of Book = [ {
        for ( book in booksSelected ) {
    } ] as nativearray of Book;

    db.checkoutBooks(, books);

Remember that all objects between the square brackets [ ] are treated as an array. This, combined with the braces { } used to wrap the for loop, takes each book object from the booksSelected array and inserts them into the books native array.

Java DB Clean-up

Since only one application may connect to the Library database at a time, it's important to disconnect when the Library application terminates. To accomplish this, the onClose method of the main window's Stage calls the Database class to execute the following:

    "jdbc:derby:;shutdown=true", usr, pwd );

The result is that the current database connection is broken, and it's safe to shut down the application. If you fail to do this, or the application terminates abruptly -- i.e., it crashes or its process is killed externally -- you'll need to remove the database lock manually in order to connect to it again. To do this, locate the database files (by default, it's in the same directory you execute the application from) and remove the file named db.lck.

Conclusion and Further Reading

Because JavaFX applications are also Java applications, integrating with Java classes, JAR files, and other Java APIs and technologies is straightforward. This includes JDBC. However, working with Java code from JavaFX Script requires some special consideration in some cases. This article has shown that JDBC programming with Java and JavaFX Script is possible and straightforward. It also served as a tutorial on how to use Java DB/Derby.

For more information on Java DB, Derby, and JavaFX, check out the following resources:

Related Reading

More Insights

Currently we allow the following HTML tags in comments:

Single tags


<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags


<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

