Code Hungry
Code Hungry

Reputation: 4000

How to pass Date as parameter to jasper report

I am trying to create JR report which is taking start_date and end_date as parameters.

The query:

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

The code:

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

Can we pass Two Parameters to same Column in the table? Eg:

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

Upvotes: 1

Views: 19874

Answers (3)

Mohammod Hossain
Mohammod Hossain

Reputation: 4114

You can pass date as string format type as follow,

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

Upvotes: 2

Alex K
Alex K

Reputation: 22857

Your code is wrong.

You should pass parameters as below:

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

You don't need to add P${} to the parameter's name.


There are a lot of samples in JasperReports distribution package.

You can look at this sample for more details.

Upvotes: 1

Dinup Kandel
Dinup Kandel

Reputation: 2505

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;

}

Upvotes: 0

Related Questions