我想生成一个包含两列的图表。一个用于报价,另一个用于发票(两者将进行比较)。引号和发票分别位于两个名为quotedb
和invoicedb
的表中。我的问题是我只能用它来生成引号。我是图表的新手,所以我的问题是,如何操作下面的编码,以便它可以显示两个图形的数据。
这是从quotedb
:
//declaring the type of category in the column
String text =cboQIMonth.getSelectedItem().toString();
String empno = cboEmployee.getSelectedItem().toString();
String year = cboQIYear.getSelectedItem().toString();
//connecting the database
Class.forName("com.mysql.jdbc.Driver");
Connection con = (Connection)
DriverManager.getConnection("jdbc:mysql://localhost:3306/salventri","root","password");
//select statement to retrieve data for the graph
PreparedStatement stmt = con.prepareStatement("select * from quotedb where EXTRACT(MONTH from quote_date)='" + monthnum + "' and EXTRACT(YEAR from quote_date) ='" + year + "' and username=" + empno +"");
ResultSet rs = stmt.executeQuery();
DefaultCategoryDataset ddataset = new DefaultCategoryDataset();
while (rs.next() )
{
//data to be displayed on the graph
ddataset.setValue(rs.getInt("quote_total"),
rs.getString("quote_number") + " by " + rs.getInt("username"),
monthnum + " " + cboQIYear.getSelectedItem().toString());
}
答案 0 :(得分:1)
如果可能的话,在SQL查询中组合两个表,以便获得包含所需数据的单个ResultSet。
如果quote_db包含这个:
+----------+-------------+
| QUOTE_ID | QUOTE_TOTAL |
+----------+-------------+
| quoteA | 90 |
| quoteB | 190 |
| quoteC | 290 |
| quoteD | 390 |
+----------+-------------+
和invoice_db包含:
+------------+---------------+----------+
| INVOICE_ID | INVOICE_TOTAL | QUOTE_ID |
+------------+---------------+----------+
| invoiceA | 100 | quoteA |
| invoiceB | 200 | quoteB |
| invoiceC | 300 | quoteC |
| invoiceD | 400 | quoteD |
+------------+---------------+----------+
然后这段代码:
PreparedStatement stmt = con.prepareStatement("select * from INVOICE_DB as iv, QUOTE_DB as qt where qt.QUOTE_ID = iv.QUOTE_ID");
ResultSet rs = stmt.executeQuery();
DefaultCategoryDataset ddataset = new DefaultCategoryDataset();
while (rs.next()) {
ddataset.setValue(rs.getInt("invoice_total"),
"invoice_total",
rs.getString("invoice_id"));
ddataset.setValue(rs.getInt("quote_total"),
"quote_total",
rs.getString("invoice_id"));
}
JFreeChart chart = ChartFactory.createBarChart3D("Compare Invoice and Quote", "Invoice ID's", "total", ddataset);
chart.getTitle().setPaint(Color.RED);
CategoryPlot p = chart.getCategoryPlot();
p.setRangeGridlinePaint(Color.BLUE);
ChartFrame frame2 = new ChartFrame("Compare Invoice and Quote", chart);
frame2.setVisible(true);
frame2.setSize(450, 350);
会产生这种情节:
如果加入他们是不合理的,可以只做两个查询:
PreparedStatement stmt = con.prepareStatement("select * from INVOICE_DB");
ResultSet rs = stmt.executeQuery();
DefaultCategoryDataset ddataset = new DefaultCategoryDataset();
while (rs.next()) {
ddataset.setValue(rs.getInt("invoice_total"),
"invoice_total",
rs.getString("invoice_id"));
}
stmt = con.prepareStatement("select * from QUOTE_DB");
rs = stmt.executeQuery();
while (rs.next()) {
ddataset.setValue(rs.getInt("quote_total"),
"quote_total",
rs.getString("quote_id"));
}
JFreeChart chart = ChartFactory.createBarChart3D("Compare Invoice and Quote", "Invoice ID's", "total", ddataset);
chart.getTitle().setPaint(Color.RED);
CategoryPlot p = chart.getCategoryPlot();
p.setRangeGridlinePaint(Color.BLUE);
ChartFrame frame2 = new ChartFrame("Compare Invoice and Quote", chart);
frame2.setVisible(true);
frame2.setSize(450, 350);
结果图不容易比较发票和报价: