简体   繁体   English

使用java从数据库中检索数据并在条形图中显示

[英]Retrieving data from database and displaying in a barchart using java

I am developing a java application using net beans.我正在使用 net bean 开发一个 java 应用程序。 It should retrieve data from database and display it in the form of a chart.它应该从数据库中检索数据并以图表的形式显示它。 Please give me suggestions how to do this in netbeans.请给我建议如何在 netbeans 中做到这一点。

I got the answer for how to retrieve data from database and display in the form of a bar chart.我得到了如何从数据库中检索数据并以条形图的形式显示的答案。 The below code is developed using net beans and variable name may vary depending upon the names you used.以下代码是使用 net beans 开发的,变量名称可能会因您使用的名称而异。

You need to import the below packages also:您还需要导入以下包:

import java.awt.*;
import java.sql.Connection;
import java.sql.DriverManager;
import javax.swing.JOptionPane;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.jdbc.JDBCCategoryDataset;

try
      {
          String host="Database address here";
               Connection conn;
               conn = DriverManager.getConnection(host,"username","password");
         final String SQL = "SELECT column1, column2 FROM table_name";
         final CategoryDataset dataset = new JDBCCategoryDataset(conn, SQL);
         JFreeChart chart = ChartFactory.createBarChart("Report","X-Axis","Y-Axis", dataset, PlotOrientation.VERTICAL, false, false, false);
        CategoryPlot catplot = chart.getCategoryPlot();
        catplot.setRangeGridlinePaint(Color.BLACK);
        ChartPanel chartpanel = new ChartPanel(chart);
       jPanel1.removeAll();
        jPanel1.add(chartpanel, BorderLayout.CENTER);
        jPanel1.validate();    
      }
    catch(Exception e){
        JOptionPane.showMessageDialog(null, e);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM