Der Weg, um darüber zu gehen, zunächst wird die Datenbank-Verbindung mit der folgenden Codierung Festlegung, wie unten dargestellt:
//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();
Ein brauchen dann werden Sie folgende Codierung für die Erstellung und Anzeige des Diagramms zu verwenden, in 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);
Sie müssen solche in einem Versuch Catch Block setzen.
Denken Sie daran, dass Sie eine Jar-Datei benötigen, mit der Sie die erforderlichen Daten aus diesen JAR-Dateien für die Diagramme importieren können.
Dies ist die komplette Reihe von unten Codierung:
//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);
}
}