sumukha.prasanna Posted October 2, 2017 Share Posted October 2, 2017 Hi all,I created a Java console app which takes a .jrxml file with no data source as input and renders a pdf to a specific folder. Now, I am trying to use a .jrxml which was generated with a data source.How do I add a data source in place of empty data source? (JRDataSource dataSource = new JREmptyDataSource();)Any answer is appreciated.Thanks in advance. Link to comment Share on other sites More sharing options...
reportdev Posted October 2, 2017 Share Posted October 2, 2017 import java.sql.Connection;provide the proper jdbc driver in the classpath and build your connection string. For example: Connection con = DriverManager.getConnection("jdbc:oracle:thin:@database", "username", "password");Map parameters = new HashMap();JasperReport report = (JasperReport) JRLoader.loadObjectFromFile("fileLocation");JasperPrint jasperPrint = JasperFillManager.fillReport(report, parameters, con); Link to comment Share on other sites More sharing options...
sumukha.prasanna Posted October 2, 2017 Author Share Posted October 2, 2017 This is the whole program.package jasperReports;import java.io.File;import java.io.IOException;import java.util.HashMap;import java.util.Map; import net.sf.jasperreports.engine.JRDataSource;//import net.sf.jasperreports.engine.JREmptyDataSource;import net.sf.jasperreports.engine.JRException;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 net.sf.jasperreports.engine.JasperReport;import net.sf.jasperreports.engine.JRResultSetDataSource;//import jasperSQLConn.*; public class PdfFromXmlFile { public static void main(String[] args) throws JRException, IOException { // Compile jrxml file. JasperReport jasperReport = JasperCompileManager .compileReport("C:/jasperreport/CustomerDataReport/CustomerData.jrxml"); // Parameters for report Map<String, Object> parameters = new HashMap<String, Object>(); // DataSource // This is simple example, no database. // then using empty datasource. JRDataSource dataSource = new JRResultSetDataSource(null); JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, dataSource); // Make sure the output directory exists. File outDir = new File("C:/jasperoutput"); outDir.mkdirs(); // Export to PDF. JasperExportManager.exportReportToPdfFile(jasperPrint, "C:/jasperoutput/CustomerData.pdf"); System.out.println("Pdf generated!"); }}Please tell me how to add a datasource. Link to comment Share on other sites More sharing options...
hozawa Posted October 2, 2017 Share Posted October 2, 2017 If you don't know Java, you should post to a forum such as StackOverflow instead of here.To create a jdbc data source just look at the following page:https://alvinalexander.com/java/edu/pj/jdbc/jdbc0003Just need to set result set as follows.JRDataSource dataSource = new JRResultSetDataSource(rs); 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