2012-08-29 74 views
0

我正在嘗試創建JR以start_date和end_date爲參數的報告。如何將日期作爲參數傳遞給碧玉報告

查詢:

SELECT * FROM emp WHERE joining_date BETWEEN $P{frm_date} AND $P{to_date} 

代碼:

Date from_date = dt_from_date.getDate(); 
Date to_date = dt_to_date.getDate(); 
java.sql.Date frm_dte = new java.sql.Date(from_date.getTime()); 
java.sql.Date to_dte = new java.sql.Date(to_date.getTime()); 
try { 
    HashMap map = new HashMap(); 
    map.put("$P{frm_date}", frm_dte); 
    map.put("$P{to_date}", to_dte); 
    JasperPrint jp = JasperFillManager.fillReport(is, map, con); 
    JRViewer jv = new JRViewer(jp); 
    JFrame jf = new JFrame(); 
    jf.getContentPane().add(jv); 
    jf.validate(); 
    jf.setVisible(true); 
    jf.setSize(new Dimension(800, 600)); 
    jf.setLocation(300, 100); 
    jf.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE); 
} catch (JRException ex) { 
    ex.printStackTrace(); 
} 

我們可以通過兩個參數來同列在表中?例如:

map.put("joining_date", frm_dte); 
map.put("joining_date", to_dte); 
+0

你有什麼錯誤嗎? – Ami

+0

如果您在同一列上傳遞兩個日期,那麼這兩個日期將並排顯示,因此我沒有收到任何記錄 –

+0

。但是你面臨的確切問題是什麼? –

回答

1

你的代碼是錯誤的。

您應該傳遞的參數如下:

Map<String, Object> map = new HashMap<String, Object>(); 
map.put("frm_date", frm_dte); 
map.put("to_date", to_dte); 

你不需要P${}添加到參數的名稱。


有在JasperReports的分發包很多樣品。

有關更多詳細信息,請參閱this sample

2

可以傳遞日期字符串格式類型如下,

if(from_date!=null) 
{ 
     formattedEndDate=new SimpleDateFormat("yyyy-MM-dd").format(from_date); 
} 

if(getStartDate()!=null) 
{ 
    formattedStartDate=new SimpleDateFormat("yyyy-MM-dd").format(to_date); 
} 
0
private JasperPrint generateReport() { 
    Connection conn = null; 
    JasperPrint myJPrint = null; 
    try { 

     conn =yourconnectionName; 

     // parameters to be passed to the report 
     Map<String, Object> params = new HashMap(); 

     // Loading my jasper file 
     JasperDesign jasperDesign = null; 
     JasperReport jasperReport = null; 

     params.put("REPORT_DIR",yourClassName.class.getClassLoader() 
         .getResource("yourJasperFileName.jrxml").toString().replace("yourJasperFileName.jrxml", "")); 
     jasperDesign = JasperManager.loadXmlDesign(yourClassName.class 
       .getClassLoader().getResourceAsStream("yourJasperFileName.jrxml")); 
     params.put("joining_date", frm_dte); 
     params.put("leaving_date", frm_dte); 
     jasperReport = JasperCompileManager.compileReport(jasperDesign); 

     /* 
     * Filling the report with data from the database based on the 
     * parameters passed. 
     */ 
     myJPrint = JasperFillManager.fillReport(jasperReport, params, conn); 
     params.clear(); 

    } catch (JRException ex) { 
     ex.printStackTrace(); 
    } 

    return myJPrint; 

} 
+0

@niraj確保參數名稱在您的ireport中定義的正確。 params.put(「joining_date」,frm_dte); params.put(「leaving_date」,frm_dte); –