2015-10-18 81 views
0

我想生成一個包含兩列的圖。一個將用於報價,另一個將用於發票(兩者將進行比較)。報價單和發票分爲兩個單獨的表格,分別稱爲quotedbinvoicedb。我的問題是我只能讓它產生報價。我是新來的圖,所以我的問題是,我如何操作下面的編碼,以便它可以顯示來自兩個圖的數據。如何在一張圖上生成來自兩個不同MySQL表的結果

這裏是編碼調用從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

*」如何在Netbeans IDE中的單個圖表**上生成兩個不同MySQL表格的結果?「***在Eclipse中執行相同的操作(您的IDE與該問題無關)。 –

+0

我對Eclipse的使用經驗與Netbeans相同,所以我不確定如何在 – Osiris93

+0

@AndrewThompson上做到這一點但是我怎樣才能讓它從兩張不同的表中讀取呢?我需要創建第二個'ResultSet'語句嗎? – Osiris93

回答

1

如果可能結合在SQL查詢中的兩個表,讓你得到一個單獨的結果與你所需要的所有數據。

如果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); 

會產生這樣的情節:

Plot of invoice and quote

如果沒有合理的投身其中一個可能只是做兩個查詢:

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); 

產生的情節是不容易比較的發票和報價:

enter image description here

+0

謝謝,這正是我想要做的,它的工作原理。 – Osiris93

相關問題