[英]How to generate results from two different MySQL tables on a single graph
我想生成一個具有兩列的圖形。 一個將用於報價,另一個將用於發票(將比較兩個)。 該報價和發票是兩個所謂的分離表quotedb
和invoicedb
。 我的問題是我只能讓它生成報價。 我是圖形新手,所以我的問題是,如何處理下面的編碼,以便它可以顯示兩個圖形中的數據。
這是從quotedb
調用數據的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());
}
如果可能,請在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);
生成的圖不容易比較發票和報價:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.