将PieChart链接到DB

时间:2017-12-28 21:18:14

标签: java database charts jfreechart

我在使用数据库创建饼图时遇到问题,一切看起来都是正确的,但它不会生成图表。我已正确设置SELECT语句,将Text转换为Integer,使用与DB的正确链接设置Chart,但它仍然无效。

有人知道发生了什么事吗?感谢。

import java.awt.event.ActionEvent;
import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.data.general.DefaultPieDataset;

public class pie2 {
    JTextField open = null;
    JTextField close = null;

    public void pie2() {

        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();

        } catch (Exception e) {
        }
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;

        try {

            String serverName = "127.0.0.1";
            String mydatabase = "test";
            String url = "jdbc:mysql://" + serverName + "/" + mydatabase;
            String usernameConn = "root";
            String passwordConn = "";
            conn = DriverManager.getConnection(url, usernameConn, passwordConn);

            stmt = conn.createStatement();

            if (stmt.execute("select count(*) as Open from tb_tickets where status = 'open'")) {
                rs = stmt.getResultSet();
                rs.next();
                open.setText(rs.getString("Open"));

            }
            if (stmt.execute("select count(*) as Close from tb_tickets where status = 'closed'")) {
                rs = stmt.getResultSet();
                rs.next();
                close.setText(rs.getString("Close"));
            }

            int openE = Integer.parseInt(open.getText());
            int closedE = Integer.parseInt(close.getText());

            DefaultPieDataset pieDataset = new DefaultPieDataset();
            pieDataset.setValue("Open", new Integer(openE));
            pieDataset.setValue("Close", new Integer(closedE));

            JFreeChart chart = ChartFactory.createPieChart("CSC408 Mark Distribution", pieDataset, true, true, false);
            // Title
            // Dataset
            // Show legend
            // Use tooltips
            // Configure chart to generate URLs?
            try {
                ChartUtilities.saveChartAsJPEG(new File("/Users/Levys/Documents/workspace/chart.jpg"), chart, 500, 300);
            } catch (Exception e) {
                System.out.println("Problem occurred creating chart.");
            }

        } catch (SQLException ex) {
            // handle any errors
            System.out.println("SQLException: " + ex.getMessage());
            System.out.println("SQLState: " + ex.getSQLState());
            System.out.println("VendorError: " + ex.getErrorCode());
        }
    }

    public static void main(String[] args) {
        new pie2();
    }
}

0 个答案:

没有答案