Statistics with JAVA EE using Google Chart
in this tutorial ,we will see how to set statistics in an JAVA EE applicationWe need 3 ihe website of mportants things :
a page called getData.jsp : this page will connect to our Mysql and store the query results into 2 variables
a page called googleChart.html : this page will connect to the previous page and also to googleChart Website to load Graphics , and transform data to satistics
Remarque : you must have Internet Connexion !
I will comment the code to better understand : https://developers.google.com/chart/
1. googlechart.html
data.addColumn('string', 'name');
data.addColumn('number', 'empid');
for(var i=0;i
{
var name = queryObject.empdetails[i].name;
var empid = queryObject.empdetails[i].empid;
data.addRows([
[name,parseInt(empid)]
]);
}
var options = {
title: 'Employee Information',
};<--to barchart="" br="" have="" just="" line="" next="" replace="" the="" this="" with=""> --to>var chart = new google.visualization.BarChart(document.getElementById('chart_div'));
for colum Chart var chart = new google.visualization.ColumChart(document.getElementById('chart_div'));
for line chart: var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
-->
for colum Chart var chart = new google.visualization.ColumChart(document.getElementById('chart_div'));
for line chart: var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
-->
var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
chart.draw(data,options);
}
-->
2.getdata.jsp
<%@page import="java.sql.*" %>
<%@page import="java.util.*" %>
<%@page import="org.json.JSONObject" %>
<%
Connection con= null;
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/empdb","root","password");
ResultSet rs = null;
List empdetails = new LinkedList();
JSONObject responseObj = new JSONObject();
String query = "SELECT id,name from employee";
PreparedStatement pstm= con.prepareStatement(query);
rs = pstm.executeQuery();
JSONObject empObj = null;
while (rs.next()) {
String name = rs.getString("name");
int empid = rs.getInt("id");
empObj = new JSONObject();
empObj.put("name", name);
empObj.put("empid", empid);
empdetails.add(empObj);
}
responseObj.put("empdetails", empdetails);
out.print(responseObj.toString());
}
catch(Exception e){
e.printStackTrace();
}finally{
if(con!= null){
try{
con.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
%>
Create table with name employee...
CREATE TABLE IF NOT EXISTS `employee` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(50) DEFAULT NULL,
`email` varchar(50) DEFAULT NULL,
`date` date DEFAULT NULL,
`working_days` int(11) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `email` (`email`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=10 ;
the code was uploaded