nikitha Posted October 21, 2008 Share Posted October 21, 2008 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 Link to comment Share on other sites More sharing options...
dprogrammer Posted October 21, 2008 Share Posted October 21, 2008 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); } }} Link to comment Share on other sites More sharing options...
crusty_collins Posted October 21, 2008 Share Posted October 21, 2008 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. Link to comment Share on other sites More sharing options...
nikitha Posted October 22, 2008 Author Share Posted October 22, 2008 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 reportString 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 driverClass.forName(dbDriver); // Get the connectionConnection 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 fileJasperExportManager.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 Link to comment Share on other sites More sharing options...
dprogrammer Posted October 23, 2008 Share Posted October 23, 2008 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 Link to comment Share on other sites More sharing options...
shatireazam Posted November 8, 2008 Share Posted November 8, 2008 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 errorand 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(); } Link to comment Share on other sites More sharing options...
imby Posted May 5, 2012 Share Posted May 5, 2012 Hi, i found the following java program to call Jasper ReportIts 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:10Post Edited by imby at 05/05/2012 15:26 Link to comment Share on other sites More sharing options...
jani99harsh Posted March 20, 2017 Share Posted March 20, 2017 @ dprogrammer...How to solve this man ?Start ...Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/digester/Digester at net.sf.jasperreports.engine.JasperCompileManager.compileReportToFile(JasperCompileManager.java:115) at javaapplication2.javaApplication2.main(javaApplication2.java:29)Caused by: java.lang.ClassNotFoundException: org.apache.commons.digester.Digester at java.net.URLClassLoader.findClass(URLClassLoader.java:381) at java.lang.ClassLoader.loadClass(ClassLoader.java:424) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331) at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ... 2 moreJava Result: 1BUILD SUCCESSFUL (total time: 0 seconds) Link to comment Share on other sites More sharing options...
hortegavb Posted January 23, 2018 Share Posted January 23, 2018 descarga y referencia en tu proyecto la siguiente libreria:commons-digester-2.1.jarsi te salen otras excepciones descarga las siguientes:commons-beanutils-1.9.3.jarcommons-collections-3.2.2.jarcommons-discovery-0.2.jarcommons-logging.jarcom.lowagie.text-2.1.7.jar Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now