我开发了一个小型应用程序,它必须使用 3D 条形图显示公司的十大畅销商品。这些项目将从 mysql 数据库中检索。
我确实了解如何从 mysql 数据库检索数据,但我如何才能让它与 Neteans 上的条形图一起使用?
我怎样才能实现这一目标,或者哪里有帮助我实现这一目标的最佳资源?
最佳答案
解决此问题的方法是首先使用以下代码建立数据库连接,如下所示:
//connects to the database
Class.forName("com.mysql.jdbc.Driver");
Connection con = (Connection)
DriverManager.getConnection("jdbc:mysql://localhost:3306/DBName","root","password");
//select statement calling data from the sales database
PreparedStatement stmt = con.prepareStatement("SELECT * FROM dbsales ORDER BY usold DESC LIMIT 10");
ResultSet rs = stmt.executeQuery();
然后您将需要使用以下代码在 netbeans 中创建和显示图表
//creates the graph object
DefaultCategoryDataset ddataset = new DefaultCategoryDataset();
while (rs.next())
{
//retrieves data from the database for the graph
ddataset.setValue(new Double(rs.getDouble("usold")), rs.getString("pbrand") + " " + rs.getString("pname"), rs.getString("pid"));
}
//generates the graph
JFreeChart chart = ChartFactory.createBarChart3D("Top 10 Selling Products", "Products", "Number of Units Sold", ddataset);
//creates the graph title
chart.getTitle().setPaint(Color.RED);
//plots the graph
CategoryPlot p = chart.getCategoryPlot();
p.setRangeGridlinePaint(Color.BLUE);
//creates the frame
ChartFrame frame2 = new ChartFrame("Top 10 Selling Products", chart);
//sets the frame visible
frame2.setVisible(true);
//sets the frame size
frame2.setSize(900,700);
您需要将其放入 try catch block 中。
请记住,您将需要一个 Jar 文件,它允许您从这些 JAR 文件中为图表导入必要的数据。
下面是完整的编码集:
//method for top ten graph
private void topten()
{
try
{
//connects to the database
Class.forName("com.mysql.jdbc.Driver");
Connection con = (Connection)
DriverManager.getConnection("jdbc:mysql://localhost:3306/DBName","root","password");
//select statement calling data from the sales database
PreparedStatement stmt = con.prepareStatement("SELECT * FROM dbsales ORDER BY usold DESC LIMIT 10");
ResultSet rs = stmt.executeQuery();
//creates the graph object
DefaultCategoryDataset ddataset = new DefaultCategoryDataset();
while (rs.next())
{
//retrieves data from the database for the graph
ddataset.setValue(new Double(rs.getDouble("usold")), rs.getString("pbrand") + " " + rs.getString("pname"), rs.getString("pid"));
}
//generates the graph
JFreeChart chart = ChartFactory.createBarChart3D("Top 10 Selling Products", "Products", "Number of Units Sold", ddataset);
//creates the graph title
chart.getTitle().setPaint(Color.RED);
//plots the graph
CategoryPlot p = chart.getCategoryPlot();
p.setRangeGridlinePaint(Color.BLUE);
//creates the frame
ChartFrame frame2 = new ChartFrame("Top 10 Selling Products", chart);
//sets the frame visible
frame2.setVisible(true);
//sets the frame size
frame2.setSize(900,700);
}
catch(Exception e)
{
//error message for when the graph cannot be generated
JOptionPane.showMessageDialog(null, "Error 111: Unable to identify and load best ten sellers for graph", "Database Error", JOptionPane.ERROR_MESSAGE);
}
}
关于mysql - 在 NetBeans 中创建 3D 条形图以显示数据库中的前 10 个项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36679273/