Example of java program to call Jasper Report

-1

Hi All,

Could anyone give an simple example to call a jasper report from a java program and fill the report with data from database.

Please help me.

 

Thanks and Regards,

Naveen

 

naveenjava's picture
Joined: Oct 21 2008 - 2:51am
Last seen: 6 years 2 days ago

6 Answers:

2

Naveen, hope this helps. /*  * User links:  * http://jasperreports.sourceforge.net/tutorial/index.html  * http://jasperreports.sourceforge.net/documentation.html  * http://jasperreports.sourceforge.net/quick.how.to.html  */ package com.mycompany.generator;

import java.sql.Connection; import java.sql.DriverManager; import java.util.HashMap;

import net.sf.jasperreports.engine.JasperCompileManager; import net.sf.jasperreports.engine.JasperExportManager; import net.sf.jasperreports.engine.JasperFillManager; import net.sf.jasperreports.engine.JasperPrint;

// import com.mycompany.helper.* ; // import com.mycompany.dbi.*;

public class ReportGenerator {  public static void main(String[] args) {   HashMap hm = null;   // System.out.println("Usage: ReportGenerator ....");

  try {    System.out.println("Start ....");    // Get jasper report    String jrxmlFileName = "C:/reports/C1_report.jrxml";    String jasperFileName = "C:/reports/C1_report.jasper";    String pdfFileName = "C:/reports/C1_report.pdf";

   JasperCompileManager.compileReportToFile(jrxmlFileName, jasperFileName);        // String dbUrl = props.getProperty("jdbc.url");    String dbUrl = "jdbc:oracle:thin:@localhost:1521:mydbname";    // String dbDriver = props.getProperty("jdbc.driver");    String dbDriver = "oracle.jdbc.driver.OracleDriver";    // String dbUname = props.getProperty("db.username");    String dbUname = "mydb";    // String dbPwd = props.getProperty("db.password");    String dbPwd = "mydbpw";

   // Load the JDBC driver    Class.forName(dbDriver);    // Get the connection    Connection conn = DriverManager      .getConnection(dbUrl, dbUname, dbPwd);

   // Create arguments    // Map params = new HashMap();    hm = new HashMap();    hm.put("ID", "123");    hm.put("DATENAME", "April 2006");

   // Generate jasper print    JasperPrint jprint = (JasperPrint) JasperFillManager.fillReport(jasperFileName, hm, conn);

   // Export pdf file    JasperExportManager.exportReportToPdfFile(jprint, pdfFileName);        System.out.println("Done exporting reports to pdf");       } catch (Exception e) {    System.out.print("Exceptiion" + e);   }  } }  

 

dprogrammer's picture
Joined: Jul 19 2006 - 7:37am
Last seen: 8 months 1 week ago
0

Nice but I get sax parse errors.

Code:
ERROR - Digester.error(1538) | Parse Error at line 189 column 46: Element type "pen" must be declared.
org.xml.sax.SAXParseException: Element type "pen" must be declared.</td></tr></tbody></table>
crusty_collins's picture
Joined: Jul 18 2006 - 2:24am
Last seen: 8 years 3 months ago
1

By reference of your example, i have just changed the existing code as:

import java.sql.Connection;

import

java.sql.DriverManager;

import

java.util.HashMap;

import

java.sql.ResultSet;

import

java.sql.SQLException;

import

java.sql.Statement;

import

net.sf.jasperreports.engine.JasperCompileManager;

import

net.sf.jasperreports.engine.JasperExportManager;

import

net.sf.jasperreports.engine.JasperFillManager;

import

 

net.sf.jasperreports.engine.JasperPrint;

public

 

HashMap hm =

 

System.

 

class ReportGenerator {public static void main(String[] args) {null;try {out.println("Start ....");// Get jasper report

String jrxmlFileName =

String jasperFileName =

String pdfFileName =

 

JasperCompileManager.compileReportToFile(jrxmlFileName, jasperFileName);

 

 

"F:/TestDic/Test.jrxml";"F:/TestDic/Test.jasper";"F:/TestDic/Test.pdf";// String dbUrl = props.getProperty("jdbc.url");

String dbUrl =

 

"jdbc:oracle:thin:@localhost:1521:xe";// String dbDriver = props.getProperty("jdbc.driver");

String dbDriver =

 

"oracle.jdbc.driver.OracleDriver";// String dbUname = props.getProperty("db.username");

String dbUname =

 

"tad";// String dbPwd = props.getProperty("db.password");

String dbPwd =

String query =

 

"softpro";"select word, description from dictionary";// Load the JDBC driver

Class.forName(dbDriver);

 

// Get the connection

Connection conn = DriverManager.getConnection(dbUrl, dbUname, dbPwd);

Statement stmt= conn.createStatement();

ResultSet rs = stmt.executeQuery(query);

String word=

String des=

 

word=rs.getString(1);

System.

des=rs.getString(2);

System.

hm =

hm.put(

hm.put(

}

 

System.

"";"";while(rs.next()){out.println("Words are"+word);out.println("des are"+des);new HashMap();"WORD", word);"DESCRIPTIN", des);out.println("Filling report...");

JasperPrint jprint = (JasperPrint) JasperFillManager.fillReport(jasperFileName, hm, conn);

 

 

// Export pdf file

JasperExportManager.exportReportToPdfFile(jprint, pdfFileName);

 

System.

 

}

System.

}

}

}

 

out.println("Done exporting reports to pdf");catch (Exception e) {out.print("Exceptiion" + e);

How can i display my result set data in to the report, what exactly i have to give in the jrxml template,

Is it mandatory to write again, report query in the template ? Please  find the attached jrxml file.

Please help me.

 

Thanks and Regards,

Naveen

naveenjava's picture
Joined: Oct 21 2008 - 2:51am
Last seen: 6 years 2 days ago
0

Its been a while since I used that code with version 1.2. I was able to compile it and run it through command prompt and was able to generate a pdf. Make sure you have all the jars in your path. I think these jars should be used:

commonsxxx, and itext-1.3.1.jar

dprogrammer's picture
Joined: Jul 19 2006 - 7:37am
Last seen: 8 months 1 week ago
0

Hello, I am a newbie to Java. I'm trying to generate reports with jasper in netbeans 6.1. My code given below, runs well in Java desktop application but in jsp it does not give any error and simply render the html but no report shows up (not even empty) nor any error message. I know i'm missing a very simple and stupid step but does not know what. Can somebody help me with Java code and explain why does it work in java desktop application but not in JSP? Thanks in advance.

 

Code:
try{
 
            Map m=new HashMap();
            m.put("proName", "General"); //Parameters to be sent to report
            m.put("spID", 37); //Parameter to be sent to report
            String reportSource = "/report/report1.jrxml"; //report folder is in WebPages folder
            String reportDest = "/report/report1.html";
            JasperReport jasperReport = JasperCompileManager.compileReport(reportSource);
            Class.forName("com.mysql.jdbc.Driver");
            Connection con =DriverManager.getConnection("jdbc:mysql://localhost:3306/lisdb", "root", "samsung");
            JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, m, con);
            JasperExportManager.exportReportToHtmlFile(jasperPrint, reportDest);
            JasperViewer.viewReport(jasperPrint);
        }
        catch (JRException e){
            e.printStackTrace();
        }
        catch (ClassNotFoundException ex){
            ex.printStackTrace();
        }
        // Exception handling for the DriverManager.getConnection method.
        catch (SQLException ex){
            ex.printStackTrace();
        }
 
</td></tr></tbody></table>
shatireazam's picture
Joined: Nov 8 2008 - 4:09am
Last seen: 1 year 4 months ago
0

Hi, i found the following  java program to call Jasper Report Its very good and helpful.

www.javatips.net/blog/2011/09/call-jasper-report-ireport-from-java-application

Post Edited by imby at 05/05/2012 15:10



Post Edited by imby at 05/05/2012 15:26
imby's picture
1
Joined: May 5 2012 - 8:06am
Last seen: 2 years 5 months ago
Feedback