PieChart Data from Database in Javafx 2.0

Hello Today I’m going to demonstrate the PieChart of Javafx 2.0 beta release. This PieChart does  gets it’s data from the database. I’ll be using mysql as the database for instance.

The requirement for this demonstration:

Database Structure:
The database structure of the demostration is given below:

Mysql JDBC Connectivity
Let’s start first JDBC in Javafx. We don’t need to worry of Database connectivity in Javafx , after all javafx is now apart of Java. You must have good knowledge of SQL for this demonstration too.Please add mysql connection in the library. The JDBC connectivity for mysql can be done from the below code.

* I’ve connected the database as ‘company’ with username ‘root’ and password ‘root’

JDBC inside JavaFX 2.0

Now Let’s implement our JDBC inside JavaFX 2.0. We are going to first connect our database and grab the data for our piechart.

Now here we go the compiled version output:

PieChart Demo with JDBC

28 thoughts on “PieChart Data from Database in Javafx 2.0”

  1. May you explaim me this line detailed ?
    data.add(new PieChart.Data(rs.getString(2),Double.valueOf(rs.getString(1))));

  2. data.add(new PieChart.Data(String, double)) ;
    API ref: http://download.oracle.com/javafx/2.0/api/javafx/scene/chart/PieChart.Data.html#PieChart.Data%28java.lang.String,%20double%29

    and I’ve given two arguments :
    1. rs.getString(2) : which gives the type of nationality like : nepali, italian
    2. rs.getString(1) : which give me the no of people of specific nationality

    rs.getString() gives the data grabbed from database in String form.
    rs.getString(int column): API ref: http://download.oracle.com/javase/1.5.0/docs/api/java/sql/ResultSet.html#getString%28int%29

    * Finally I will edit code with rs.getDouble() instead of Double.valueOf(String)


  3. Great tutorial on jdbc and javafx. It works for me. But when i try to run the application as a web browser app I get an error. Can you help on how to deploy in a web browser?

  4. Dear ngopal,


    I build your PieChartDemo in netBeans IDE, when i run the project i get DB error, but when i run just PieChart file alone it get the data from the data base.

    Could u please help me i am sending u the netBeans project which i made from your data and help.

  5. Hi Sir!

    My question is not related to your tutorial above but javafx related.

    Is there a way of transferring the focus from one textfield into another by using the ENTER key? Doing it in a flexible way, not explicitly identifying the textfield name and requesting a focus.

    Thank you in advance.

  6. Hi @rabei,
    Seems like you have something wrong with the execution.
    Please make sure the function main() is declared.
    If you are using netbeans IDE then you need to set the run main-class as well.


  7. Great tutorial on jdbc and javafx. It works for me. But when i try to run the application as a web browser app I get an error. Can you help on how to deploy in a web browser? plss help me as soon as possible
    Thanx in Advance Narayan

  8. Hiii Narayan
    I have to ask one thing when i am trying to run html file from dist folder it is running perfectly when i pasted three files jar,jnlp and html and i am trying to run it from localhost then it is not comming and somewhere i am getting Runtime error massage.please help me Thanx in Advance

    Thank you

  9. Pingback: Java desktop links of the week, June 20 – Jonathan Giles

Leave a Reply