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:

+-------------------+
| Tables_in_company |
+-------------------+
| customer          |
| nationality       |
+-------------------+
'Customer' Table
+----------------+--------------+------+-----+---------+----------------+
| Field          | Type         | Null | Key | Default | Extra          |
+----------------+--------------+------+-----+---------+----------------+
| Sn             | int(11)      | NO   | PRI | NULL    | auto_increment |
| Name           | varchar(200) | NO   |     | NULL    |                |
| nationality_id | int(4)       | NO   |     | NULL    |                |
+----------------+--------------+------+-----+---------+----------------+
'Nationality' Table
+-------+--------------+------+-----+---------+----------------+
| Field | Type         | Null | Key | Default | Extra          |
+-------+--------------+------+-----+---------+----------------+
| id    | int(11)      | NO   | PRI | NULL    | auto_increment |
| type  | varchar(100) | NO   |     | NULL    |                |
+-------+--------------+------+-----+---------+----------------+

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.

public class DBConnect {

    private static Connection conn;
    private static String url = "jdbc:mysql://localhost/company";
    private static String user = "root";
    private static String pass = "root";

    public static Connection connect() throws SQLException{
        try{
            Class.forName("com.mysql.jdbc.Driver").newInstance();
        }catch(ClassNotFoundException cnfe){
            System.err.println("Error: "+cnfe.getMessage());
        }catch(InstantiationException ie){
            System.err.println("Error: "+ie.getMessage());
        }catch(IllegalAccessException iae){
            System.err.println("Error: "+iae.getMessage());
        }

        conn = DriverManager.getConnection(url,user,pass);
        return conn;
    }

    public static Connection getConnection() throws SQLException{
        if(conn !=null && !conn.isClosed())
            return conn;
        connect();
        return conn;

    }
}

* 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.

public class PieChartDemo extends Application {

    //PIE CHART DATA
    private ObservableList data;

    //MAIN EXECUTOR
    public static void main(String[] args) {
        Application.launch(PieChartDemo.class, args);
    }

    //CONNECTION DATABASE SAVING DATA
    public void buildData(){
          Connection c ;
          data = FXCollections.observableArrayList();
          try{
            c = DBConnect.connect();
            //SQL FOR SELECTING NATIONALITY OF CUSTOMER
            String SQL = "SELECT COUNT(nationality_id), "
                    + "type FROM CUSTOMER c,NATIONALITY na"
                    + " WHERE na.id=c.nationality_id GROUP BY type";

            ResultSet rs = c.createStatement().executeQuery(SQL);
            while(rs.next()){
                //adding data on piechart data
                data.add(new PieChart.Data(rs.getString(2),rs.getDouble(1)));
            }
          }catch(Exception e){
              System.out.println("Error on DB connection");
              return;
          }

      }

      @Override
      public void start(Stage stage) throws Exception {
        //PIE CHART
        PieChart pieChart = new PieChart();
        buildData();
        pieChart.getData().addAll(data);

        //Main Scene
        Scene scene = new Scene(pieChart);        

        stage.setScene(scene);
        stage.setVisible(true);
      }
}

Now here we go the compiled version output:

PieChart Demo with JDBC