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:
1 2 3 4 5 6 |
+-------------------+ | Tables_in_company | +-------------------+ | customer | | nationality | +-------------------+ |
1 2 3 4 5 6 7 8 |
'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 | | +----------------+--------------+------+-----+---------+----------------+ |
1 2 3 4 5 6 7 |
'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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
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:
May you explaim me this line detailed ?
data.add(new PieChart.Data(rs.getString(2),Double.valueOf(rs.getString(1))));
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)
-Narayan
Thank you very much.
Thank you.
thank you, very helpful for the newby
hi , any idea about how to integrate @EJB on FXController . As it simply isnot taking ejb injection?
Hello sarad,
Your issue seems similar to this: https://forums.oracle.com/forums/thread.jspa?threadID=2296308&tstart=0
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?
ThanQ
Type your comment here
Dear ngopal,
Salam!
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.
Hi tanvir,
Ok You can send me the file in zip at [email protected]
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.
Hi Rey,
I have posted a comment in one of the post of oracle forum is rrlatedvwith the manual focus of the control. Take a lookbat this: https://forums.oracle.com/forums/thread.jspa?threadID=2399747
Thanks
thx so much
@baiyu
You are welcome 🙂
-Narayan
please mention the steps in details
when i run the project it give me a message : “there is no main class”
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.
Thanks
Narayan
Спасибo
thx from Ukraine
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
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
@Farhan,
refer this link: https://blog.ngopal.com.np/tiny-forum/?mingleforumaction=viewtopic&t=4
Thank you so much Narayan……….
Sir Can you please tell me how to make line chart and Bar Chart by using JDBC.
Well It is similar to this PieChart , Take a look at JavaFX Tutorial for charts.
http://docs.oracle.com/javafx/2/charts/jfxpub-charts.htm
Ok..But i would like to know how to connect Line Chart and Bar Chart with Database(MySql).
Pingback: Java desktop links of the week, June 20 – Jonathan Giles
Thank you!