JavaFX provides a rich set of UI controls, which simplify the development of visually immersive front ends for database-driven applications. When combined with the PostgreSQL database and the Hibernate ORM tool, JavaFX can handle the presentation layer for large-scale, data-driven business applications (JavaFX PostgreSQL) as well as robust desktop applications (JavaFX Hibernate).
In this article, I demonstrate how to integrate JavaFX, Hibernate and PostgreSQL using the MVC pattern and present a sample CRUD application with a data-navigation feature.
To follow the demo in this article, simply:
- Install the Java SDK+JavaFX2 SDK
- Unpack the Eclipse IDE
- Install the PostgreSQL DB
- Unpack Hibernate
- Save the PostgreSQL JDBC driver
JavaFX / Hibernate / PostgreSQL Project Setup
From the File menu in Eclipse, choose Java Project. It is better to set up User Library of all JAR files required in the project. This is not a necessary procedure but it will come in quite handy for future reference. Let's make an individual library for the Hibernate JavaFX PostgreSQL driver and include it in our project, as shown in Figure 1.
|Figure 1. Project Library: Make a library for the Hibernate JavaFX PostgreSQL driver.|
PostgreSQL Table Structure
You don't need to worry about the structure of the sample table, Contact. It will be created automatically when the application runs. However, if you want to create the table separately, you can but keep in mind the following comment for the code snippets in the rest of the article
In this case, testdb is the table space. If you opt for creating your table through SQL or through pgAdmin III, either give the name of tablespace as testdb or change the appropriate portion of code in the HiberateUtil.java file.
Also keep in mind the
update value. You may also put
create in place of
update. This would recreate the table every time the application runs, deleting all stored information. So use
Alternatively, you may set the above property as
The table created in PostgreSQL is as follows:
CREATE TABLE contacts
contactid integer NOT NULL,
email character varying(255),
firstname character varying(255),
lastname character varying(255),
phone character varying(255),
CONSTRAINT contacts_pkey PRIMARY KEY (contactid )
WITH ( OIDS=FALSE );
ALTER TABLE contacts OWNER TO postgres;