2008年 12月 15日 月曜日
How to connect Data Base from JavaFX UI with NetBeans.
This is English version of how to connect DataBase from JavaFX UI with Netbeans.
JavaFX is very compatible with Java class (JavaFX code will be compiled to Java Byte Code), and can use Java class from JavaFX script easily. So, you may would like to connect to DataBase from JavaFX UI, when you create cool UI by using JavaFX. Followings are very easy example of how to connect DataBase from JavaFX UI by using NetBeans. NetBeans can provide the many helps to create the connection to DataBase from Java, you can implement the database handling without any code. This example, shows, create a text field and button, and if you input the key in the textfield and click button, the application returns the related value, like input the ID and return the name of the user.
- From "Services" tab in NetBeans, you can find "jdbc:derby://localhost:1527/..." entry in "DataBases". This is the bundled Derby schema.
- Right click "[app on APP]" and select "Connect".

- Right click "Java DB (Network)" in "Services" -> "Drivers" and Select "Connect Using"
- "New Database Connection" dialog is displayed. Input following values in that dialog, and click "OK" button.
o Host: localhost
o Port: your database port (typically 1527)
o Database: sample
o User name: app
o Password: app
- For schema selection, please chose APP and click OK

Now you complete the Database setting.
- Create some Java Application Project, by selecting "File" -> "New Project", chose "Java Application". Default is fine for all settings.
- The created project contains java package. Right click it and select "New" -> "Entity class from Database"
- Select the target database (in this example, use [App on APP]) in Database Connection, and select and add "CUSTOMER" from "Available Tables".

- Click "Next"
- You may see the warning like "The project does not have a persistence unit. You need a persistence unit to persist entity classes.". So, click "Create Persistence Unit" button
- Select "TopLink" from Persistence Library in "Create Persistence Unit" dialog, and clieck "Create" button.
- Back to "Net Entity Classes from Database" dialog, and click "Complete" button.
- Right click the package in project and select "New" -> "Other".
- Select "JPA Controller class from Entity class" from "Persistence", and click "Next".
- Select all entity class from "Available Entity Classes" and add them to "Selected Entity Classes". Then, click "Next" button.

- Click "Finish" button

public static void main(String[] args) {
// TODO code application logic here
CustomerJpaController test = new CustomerJpaController();
Customer idOne = test.findCustomer(1);
System.out.println(idOne.getName());}
- "File" -> "New Project" -> JavaFX -> JavaFX Script Application
Followings are considering JavaFX application, to access database.

Use HBox to land textfield and button horizontally, and create a group with this textfield and button. Then, use VBox to land the group and Text vertically. The code in Stage will be as follows:
scene: Scene {
content: [
Group {
content: VBox {
content: [
Group {
content: HBox {
content: [
SwingTextField {
columns: 10
text: "textfield"
editable: true
},
SwingButton {
text: "OK"
}
]
}
},
Text {
font: Font {
size: 18
}
x: 10,
y: 50
content: "ResultText"
}
]
}
}
]
}
Then, to include Java class, created before, you need to include following libraries:
o JRE libraries
o Toplink libraries
o Java DB drivers libraries.
For Toplink and Java DB drivers, you can add them by the same way as step 4. But, JRE is not defined as default libraries set (Because JDK platform in NetBeans contains it by default. So, NetBeans did not provide JRE as libraries set). So, you need to add jar files in JRE.
- Right click "Libraries" in your JavaFX project and select "Add JAR/Folders".
- Select all jars in JRE lib (for example, C:\Program Files\Java\jdk1.6.0_11\jre\lib), and click "Open" button.
Then, you also need to include the jar file, you created in your java project.
- Right click "Libraries" in your JavaFX project and select "Add Project".
- Select the java project created in step 2, and click "Add project Jar file".

var tmpid = "1";
:
:
SwingTextField {
columns: 10
text: bind tmpid with inverse
editable: true
},
var DB = new CustomerJpaController();
SwingButton {
text: "OK"
onMouseClicked: function( e: MouseEvent ):Void {
CustomerId = Integer.parseInt(tmpid);
CustomerName = DB.findCustomer(CustomerId).getName();
}
}
import java.lang.*;
Text {
font: Font {
size: 18
}
x: 10,
y: 50
content: bind Name
}
That's all!

# But, this code dose not have any exception handling, so if you input the invalid ID, it throws Exception. JavaFX can handle exceptions almost the same way as Java, like try - catch sentence.
The whole source file for JavaFX is here .
Posted at 04:49午後 12 15, 2008 by kenji in JavaFX の素人 | 投稿されたコメント[3]
You do know that every single JavaFX app runs inside the EDT right? meaning that the onMouseClicked handler will call the JPA controller inside the EDT, that is a big no-no! please consider using an AsyncCallback or any other threading mechanism.
While this trivial example shows the integration of JavaFX with Java classes it also propagates bad coding techniques, same as it happened when Swing started 10 years ago.
Posted by Andres Almiray on 12月月 16日, 2008年 at 01:09 午前 JST #
SwingButton {
text: "OK"
onMouseClicked: function( e: MouseEvent ):Void {
CustomerId = Integer.parseInt(tmpid);
CustomerName = DB.findCustomer(CustomerId).getName();
}
}
That gives me nightmares. NO NO BAD BAD.
Posted by Gregg Bolinger on 12月月 16日, 2008年 at 01:16 午前 JST #
Hello Expertise,
Thank you very much for your comments and you are right... I think I need to revise the program. May seems that some good blog is exit to refer, like http://blogs.sun.com/clarkeman/entry/javafx_async_operations. So, if you also have good source to refer, please point it to me.
Regards,
Kenji
Posted by kenji on 12月月 17日, 2008年 at 09:41 午後 JST #