首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

Jfreechart连接接数据库解决方法

2012-01-30 
Jfreechart连接接数据库我想用jsp连接oracle数据库后取得数据,然后将数据通过当前jsp传给一个java文件,这

Jfreechart连接接数据库
我想用jsp连接oracle数据库后取得数据,然后将数据通过当前jsp传给一个java文件,这个java文件实现jfreechart的 画图功能,再将图显示到jsp当前的调用界面上
和数据库中数据到底是怎么对应生成图表的
好难哦~
给贴几个测试通过的例子参考呗
先谢拉~

[解决办法]
给你个通过的实例,比较简单。
java:

package com.kettas.xiezq;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.io.PrintWriter;
import java.text.DecimalFormat;
import java.text.NumberFormat;

import javax.servlet.http.HttpSession;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartRenderingInfo;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.entity.StandardEntityCollection;
import org.jfree.chart.labels.PieToolTipGenerator;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.labels.StandardPieToolTipGenerator;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.servlet.ServletUtilities;
import org.jfree.chart.title.TextTitle;
import org.jfree.chart.urls.StandardPieURLGenerator;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.ui.RectangleInsets;

public class PieDemo {

private DefaultPieDataset dataset=new DefaultPieDataset();
public void setValue(String key,double value){
dataset.setValue(key,value);
}
public String generatePieDemo(String title,HttpSession session,PrintWriter pw){
String filename=null;
try{

JFreeChart chart=ChartFactory.createPieChart( "饼形图 ",
dataset,
true,
false,
false);
chart.setBackgroundPaint(Color.red);
Font font=new Font( "黑体 ",Font.CENTER_BASELINE,16);
TextTitle _title=new TextTitle(title);
_title.setFont(font);
chart.setTitle(_title);

PiePlot plot=(PiePlot) chart.getPlot();
plot.setBackgroundPaint(Color.blue);
RectangleInsets insets=new RectangleInsets(5,5,5,5);
plot.setInsets(insets);

plot.setURLGenerator(new StandardPieURLGenerator( "link.jsp ", "section "));
plot.setSectionPaint(PiePlot.DEFAULT_LABEL_BACKGROUND_PAINT);
//指定section轮廓线的颜色
// plot.setDefaultOutlinePaint(Color.green);
//
plot.setExplodePercent(2,0.1);
plot.setStartAngle(270);

plot.setToolTipGenerator(new StandardPieToolTipGenerator());
/*


JFreeChart chart = ChartFactory.createPieChart(
"Pie Chart Demo 1 ", // 图表标题
dataset, // 数据集
true, // 是否为一个图例
true, //是否使用工具条,工具条就是将鼠标移动到图表不同部分后,出现的一个小提示框
false //图片是否有URL连接
);
//生成PiePlot
PiePlot plot = (PiePlot) chart.getPlot();
plot.setSectionOutlinesVisible(false); //是否显示分界线
plot.setLabelFont(new Font( "SansSerif ", Font.PLAIN, 12)); //Label的字体
plot.setNoDataMessage( "No data available "); //如果没有数据该如何显示
plot.setCircular(false); //是否为正圆
plot.setLabelGap(0.02); //这个不太清楚,应该是设置椭圆内部的间距,如果改的很大的话图形会变得很奇怪。。
plot.setForegroundAlpha(Float.parseFloat( "0.4 ")); //图片前景的透明度,图片的前景就是这里的饼状图,透明度为0.0~1.0
plot.setLabelGenerator(new StandardPieSectionLabelGenerator( "{0}: ({1}M, {2}) ",
NumberFormat.getNumberInstance(),


new DecimalFormat( "0.00% "))); //设置图片是否显示出百分比,0是数据,1是数值,2是百分比,
//后面两个参数是设置百分比取值到小数点几位,这里取到小数点后两位,如果百分比要设置为整数,就将这两个参数去掉即可
plot.setToolTipGenerator((PieToolTipGenerator) new StandardPieSectionLabelGenerator( "{0}: ({1}M, {2}) ",
NumberFormat.getNumberInstance(),
new DecimalFormat( "0.00% "))); //设置工具条是否显示出百分比,其它同上,其它同上
*/
//吧生成的图片放到指定目录
ChartRenderingInfo info=new ChartRenderingInfo(new StandardEntityCollection());

filename=ServletUtilities.saveChartAsPNG(chart,500,300,info,session);
ChartUtilities.writeImageMap(pw,filename,info,true);
pw.flush();
}catch(Exception e){
System.out.println( "Exception "+e.toString());
}

return filename;
}
}
jsp:
<%@ page contentType= "text/html;charset=GBK "%>
<%@ page import = "java.io.PrintWriter " %>
<%@ page import= "com.kettas.xiezq.PieDemo "%>
<%@ page import= "java.sql.* "%>

<%!int sa1=0;%>
<%!int sa2=1;%>
<%!int sa3=1;%>

<%
Class.forName( "com.mysql.jdbc.Driver ");
Connection conn = DriverManager.getConnection( "jdbc:mysql://localhost:3306/xiezq ", "root ", "xiemoran ");
Statement stmt = conn.createStatement();

String sql6= "select salary from student where name= 'li ' ";
String sql7 = "select salary from student where name= 'liukai ' ";
String sql8 = "select salary from student where name= 'zhangchi ' ";
ResultSet rs6 = stmt.executeQuery(sql6);
while(rs6.next()){
sa1=rs6.getInt( "salary ");
}



ResultSet rs7 = stmt.executeQuery(sql7);
while(rs7.next()){
sa2 =rs7.getInt( "salary ");
}

ResultSet rs8 = stmt.executeQuery(sql8);
while(rs8.next()){
sa3 =rs8.getInt( "salary ");
}


out.println(sa1);
out.println(sa2);
out.println(sa3);
PieDemo chart = new PieDemo();
chart.setValue( "六月 ",sa1);
chart.setValue( "七月 ",sa2);
chart.setValue( "八月 ",sa3);


String filename = chart.generatePieDemo( "Pie Chart Demo 1 ", session, new

PrintWriter(out));
String graphURL = request.getContextPath() + "/servlet/DisplayChart?filename= " + filename;
%>
<HTML>
<HEAD>
<TITLE> www.sentom.net </TITLE>
</HEAD>
<BODY>
<P ALIGN= "CENTER ">
<img src= " <%= graphURL %> " width=500 height=300 border=0 usemap= "# <%=

filename %> ">
</P>
</BODY>
</HTML>

其中我用的jfreechart的两个包为jfreechart-1.0.9。jar,jcommen-1.0.2.jar.
在web.xml中添加如下配置:
<servlet-name> DisplayChart </servlet-name>
<servlet-class> org.jfree.chart.servlet.DisplayChart </servlet-class>
</servlet>
<servlet-mapping>
<servlet-name> DisplayChart </servlet-name>
<url-pattern> /servlet/DisplayChart </url-pattern>
</servlet-mapping>

我用的是mysql数据库,你把驱动和url,name,password改成你的oracle数据库就可以应用了。

热点排行