2015-10-18 80 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

相关问题