我需要使用 Netbeans 中的数据库值绘制折线图。目前,我收到一个未使用的导入错误:
unused import java.sql.*
我不知道为什么,butif我删除了导入语句我得到了以下错误,并且我的图表没有生成。
Exception in thread "main" java.lang.NullPointerException
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1008) at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1008)
at java.lang.Double.valueOf(Double.java:504)
at org.jfree.data.jdbc.JDBCCategoryDataset.executeQuery(JDBCCategoryDataset.java:276)
at org.jfree.data.jdbc.JDBCCategoryDataset.executeQuery(JDBCCategoryDataset.java:191)
at newchart4.NewChart4.main(NewChart4.java:18)
Java Result: 1
BUILD SUCCESSFUL (total time: 22 seconds)
这是我的代码:
import java.sql.*;
import org.jfree.ui.*;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.jdbc.JDBCCategoryDataset;
public class NewChart4 {
public static void main(String[] args) throws Exception {
String query = "SELECT * from session_details_table";
JDBCCategoryDataset dataset = new JDBCCategoryDataset(
"jdbc:mysql://localhost:3306/kumararaja",
"com.mysql.jdbc.Driver",
"root",
"test123"
);
dataset.executeQuery(query);
JFreeChart chart = ChartFactory.createLineChart(
"u_id", "s_id", "cpm",
dataset, PlotOrientation.VERTICAL, true, true, false
);
ChartPanel chartPanel = new ChartPanel(chart);
chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
ApplicationFrame f = new ApplicationFrame("Chart");
f.setContentPane(chartPanel);
f.pack();
f.setVisible(true);
}
}