2012-08-29 80 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); –