⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jdbc_xy_demo.java

📁 JFreeChart开发全攻略,JFreeChart开发全攻略
💻 JAVA
字号:
package com.jfreechart;

import java.awt.Color;
import java.io.IOException;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.Writer;
import java.sql.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.jfree.chart.*;
import org.jfree.chart.entity.StandardEntityCollection;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.servlet.ServletUtilities;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.jdbc.JDBCXYDataset;
import org.jfree.data.xy.XYDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;

public class JDBC_XY_Demo extends ApplicationFrame
{

	public JDBC_XY_Demo(String s)
	{
		super(s);
	}

	private XYDataset readData()
	{
		JDBCXYDataset jdbcxydataset = null;
		// 设置数据库地址
		String s = "jdbc:mysql://localhost/jfreechartdb";
		try
		{
			//设置JDBC驱动
			Class.forName("com.mysql.jdbc.Driver");
		}
		catch (ClassNotFoundException classnotfoundexception)
		{
			System.err.print("找不到相应的class文件:");
			System.err.println(classnotfoundexception.getMessage());
		}
		try
		{
			// 建立连接
			Connection connection = DriverManager.getConnection(s, 
					"root", // 用户
					"root" // 密码
			);
			//使用已经建立好的连接创建新的JDBCXYDataset 
			jdbcxydataset = new JDBCXYDataset(connection);
			String s1 = "SELECT * FROM XYDATA1;";
			jdbcxydataset.executeQuery(s1);
			connection.close();
		}
		catch (SQLException sqlexception)
		{
			System.err.print("SQL错误: ");
			System.err.println(sqlexception.getMessage());
		}
		catch (Exception exception)
		{
			System.err.print("错误: ");
			System.err.println(exception.getMessage());
		}
		return jdbcxydataset;
	}

	public  void JfreeChart(HttpServletRequest request,HttpServletResponse response) throws IOException {
		Writer out = response.getWriter();
		HttpSession session = request.getSession();
		
		//获得图形对象
		JFreeChart chart = createChart();

		// 把生成的图片放到临时目录
		StandardEntityCollection sec = new StandardEntityCollection();
		ChartRenderingInfo info = new ChartRenderingInfo(sec);

		// 500是图片长度,300是图片高度,session 为HttpSession对象
		String filename = ServletUtilities.saveChartAsPNG(chart, 500, 300,info, session);
		
		//输出图片
		PrintWriter pw = new PrintWriter(out);
		ChartUtilities.writeImageMap(pw, "map0", info, false);
		
		String graphURL = request.getContextPath()
				+ "/DisplayChart?filename=" + filename;
		request.setAttribute("graphURL", graphURL);
	}
	
	private  JFreeChart createChart()
	{		
		XYDataset xydataset = readData();
		JFreeChart jfreechart = ChartFactory.createTimeSeriesChart("Java IDE各日均使用统计图", "时间", "使用率(%)", xydataset, true, true, false);
		jfreechart.setBackgroundPaint(Color.yellow);
		return jfreechart;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -