Mysql
 sql >> Teknologi Basis Data >  >> RDS >> Mysql

sebar plot di jfreechart dari database

Contoh lengkap ini membuat tabel database yang sesuai di memori, menanyakannya ke JDBCXYDataset dan menampilkan dataset dalam plot pencar. Perhatikan bagaimana kolom pertama menjadi domain, sedangkan kolom yang berurutan menjadi deret tersendiri.

import java.awt.EventQueue;
import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Calendar;
import java.util.Random;
import javax.swing.JFrame;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.jdbc.JDBCXYDataset;

/**
 * @see http://stackoverflow.com/a/24592754/230513
 */
public class JDBCTest {

    private static final int N = 30;
    private static final Random r = new Random();

    private void display() {
        JFrame f = new JFrame("JDBCTest");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JDBCXYDataset jds = createDataset();
        JFreeChart chart = ChartFactory.createScatterPlot("Inventory",
            "Date", "Count", jds, PlotOrientation.VERTICAL, true, true, false);
        XYPlot plot = chart.getXYPlot();
        plot.setDomainAxis(new DateAxis("Date"));
        f.add(new ChartPanel(chart));
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
        for (int i = 0; i < jds.getItemCount(); i++) {
            System.out.println(new Date(jds.getX(0, i).longValue()));
        }
    }

    private JDBCXYDataset createDataset() {
        try {
            Connection conn = DriverManager.getConnection(
                "jdbc:h2:mem:test", "", "");
            Statement st = conn.createStatement();
            st.execute("create table inventory(when date, n1 integer, n2 integer)");
            PreparedStatement ps = conn.prepareStatement(
                "insert into inventory values (?, ?, ?)");
            Calendar c = Calendar.getInstance();
            for (int i = 0; i < N; i++) {
                ps.setDate(1, new Date(c.getTimeInMillis()));
                ps.setInt(2, N / 3 + r.nextInt(N / 3));
                ps.setInt(3, N / 2 + r.nextInt(N / 3));
                ps.execute();
                c.add(Calendar.MONTH, 1);
            }
            JDBCXYDataset jds = new JDBCXYDataset(conn);
            jds.executeQuery("select when, n1, n2 from inventory");
            return jds;
        } catch (SQLException ex) {
            ex.printStackTrace(System.err);
        }
        return null;
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new JDBCTest().display();
            }
        });
    }
}



  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. batas subquery mySQL

  2. MySQL ORDER OLEH IN()

  3. Kesalahan Hibernasi saat mempertahankan tipe data TEXT

  4. Haruskah MEDIUMINT dihindari di MySQL?

  5. KONDISI MASUKKAN KE MySQL - DI MANA TIDAK ADA