Mysql 在NetBeans中创建一个3D条形图,以显示数据库中的前10项
我开发了一个小应用程序,它必须使用3D条形图显示公司最畅销的十大商品。这些项目将从mysql数据库中检索 我确实了解如何从mysql数据库检索数据,但如何让它在Neteans上使用条形图呢Mysql 在NetBeans中创建一个3D条形图,以显示数据库中的前10项,mysql,netbeans,Mysql,Netbeans,我开发了一个小应用程序,它必须使用3D条形图显示公司最畅销的十大商品。这些项目将从mysql数据库中检索 我确实了解如何从mysql数据库检索数据,但如何让它在Neteans上使用条形图呢 我如何才能实现这一目标,或者哪里是帮助我实现这一目标的最佳资源?实现这一目标的方法是首先使用以下代码建立数据库连接,如下所示: //connects to the database Class.forName("com.mysql.jdbc.Driver"); Connection
我如何才能实现这一目标,或者哪里是帮助我实现这一目标的最佳资源?实现这一目标的方法是首先使用以下代码建立数据库连接,如下所示:
//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();
//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);
}
}
然后,您需要使用以下代码在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);
你需要把这样的东西放在试抓块里
请记住,您将需要一个Jar文件,它允许您从这些Jar文件中为图表导入必要的数据
以下是完整的编码集,如下所示:
//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();
//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);
}
}