java swing / mysql / Jasper report - how to pass parameter?

0

I am doing a student project  using java swing/mysql and jasper reports.

I did the follwing:

1. created a mysql database dis

2. created JDBC connection.

3.created a report , farmer_details, using jaspersoft ireport designer.

4.connected the report using the following java swing code

using the following code i am getting the Jasper report(without passing parameters to sql query).

=========================================================================

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
       try{
           JasperDesign jd=JRXmlLoader.load("/home/john/NetBeansProjects/dis/reports/farmer_details.jrxml");
           String sql="select * from farmers where farmer_id=1";
            JRDesignQuery newQuery= new JRDesignQuery();
            newQuery.setText(sql);
            jd.setQuery(newQuery);
            JasperReport jr=JasperCompileManager.compileReport(jd);
            JasperPrint jp=JasperFillManager.fillReport(jr, null, conn);
            JasperViewer.viewReport(jp);
       }catch(Exception e){
           
       }
        
    }    

============================================================================

5. I would like to pass farmer_id as a parameter to the Jasper report from Java swing in the above listed code.

please help me

john simon

Attachments: 
john.ambat's picture
Joined: Jun 26 2013 - 10:04am
Last seen: 6 years 4 months ago

1 Answer:

0

Build a parameter "MyId" map and pass it to the engine in the call:

JasperPrint jp=JasperFillManager.fillReport(jr, parameters, conn);

Your SQL-Query should be then:

String sql="select * from farmers where farmer_id=$P{MyId}";

Cheers, Thomas

http://www.thomaszimmer.net

Thomas Zimmer's picture
Joined: Oct 2 2012 - 1:35am
Last seen: 5 days 12 hours ago
Feedback
randomness