关于数据库的条形图

时间:2012-05-28 06:06:18

标签: java mysql jsp jfreechart

这是我的表(在mysql中):

mysql> select device from user_management;
+--------+
| device |
+--------+
| APPLE  |
| HTC    |
| HTC    |
| NOKIA  |
| APPLE  |
| APPLE  |
+--------+
6 rows in set (0.00 sec)

我的代码是

<%
String query1 = "select device,count(device) from user_management where  device='"+APPLE+"'";
JDBCCategoryDataset dataset = new JDBCCategoryDataset("jdbc:mysql://localhost:8080/apps","com.mysql.jdbc.Driver","root","root");
dataset.executeQuery(query1);
System.out.println("query1");
JFreeChart chart = ChartFactory.createBarChart3D("Device  Statictics","Device","Count",dataset,PlotOrientation.VERTICAL,true,true,false);
try
{
ChartUtilities.saveChartAsJPEG(new File("D:/dvc.png"),chart,500,400);
}
catch(IOException e)
{
System.out.println(".....there is a problem in your chart. ");
}
%>

根据设备名称(APPLE,NOKIA,SAMSUNG),我想创建一个条形图,显示“没有设备VS设备”。 我想在jsp页面中显示它。

您的任何意见都将受到高度赞赏。

1 个答案:

答案 0 :(得分:2)

在查询中使用GROUP BY

SELECT device, count(device) FROM user_management GROUP BY device;

使用查询从org.jfree.data.jdbc构建合适的JDBC数据集,并使用数据集创建图表。