I'm currently trying to implement the use of Google Charts on my JSP page. I'm trying to return the result of an sql statement into an arraylist, from which I want to take the information from the arrayList and use it to populate a google chart.
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@page import="java.sql.SQLException"%>
<%@page import="java.util.ArrayList"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<%!public static String[] getLiveData() throws SQLException, ClassNotFoundException{
ArrayList<String> a = new ArrayList<String>();
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/project","root", "nbuser");
PreparedStatement ps = con.prepareStatement("SELECT department, projectname, priority, cores, disk_space FROM project.booking GROUP BY department");
ResultSet rs = ps.executeQuery();
while(rs.next())
{
a.add(rs.getString(1));
a.add(rs.getString(4));
}
return (String[]) a.toArray(new String[a.size()]);
}%>
<html>
<head>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("visualization", "1", {packages:["corechart"]});
google.setOnLoadCallback(drawChart);
function drawChart() {
var data = google.visualization.arrayToDataTable([ISSUE TO RETURN HERE]);
var options = {
title: 'Company Performance'
};
var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
</script>
</head>
continues to call the graph etc in the body
I honestly feel like I'm going about it the wrong way, but I'm unsure how to get the data in the array and return it to create a graph while on my jsp page.
I'm not very good with java so I apologise if the approach is completely off, any help would be appreciated.
EDIT
<head>
<script type="text/javascript">
var theData = [ // Start of JavaScript data object
<%
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/project", "root", "nbuser");
PreparedStatement ps = con.prepareStatement("SELECT department,SUM(cores) FROM project.booking GROUP BY department;");
ResultSet rs = ps.executeQuery();
while (rs.next()) {
%>
[" <%= rs.getString(1)%>",<%= rs.getInt(2)%>],
<%
};
// End of JavaScript object holding the data
%>
];
</script>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("visualization", "1", {packages:["corechart"]});
google.setOnLoadCallback(drawChart);
function drawChart() {
var data = google.visualization.arrayToDataTable([['Department', 'Cores']].concat(theData), false);
var options = {
title: 'Initial graph test'
};
var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
</script>
</head>
<body>
<div id="chart_div" style="width: 900px; height: 500px;"></div>
<a href="/ProjectAndBackend/System?action=livecharts">Live Charts</a>
</body>
</html>
Final fixed implementation for users viewing