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 = "{book.name} (author: {book.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 {book.name}, 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 =
patronArray[listviewPatrons.selectedIndex];
var booksSelected = getBooksSelected();
// Create a native array to pass to Java class
var books : nativearray of Book = [ {
for ( book in booksSelected ) {
book;
}
} ] as nativearray of Book;
db.checkoutBooks(patron.id, 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:
DriverManager.getConnection(
"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:


