Jump to content
Changes to the Jaspersoft community edition download ×

Add Datasource in Java console app


sumukha.prasanna

Recommended Posts

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

  • Replies 3
  • Created
  • Last Reply

Top Posters In This Topic

Popular Days

Top Posters In This Topic

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

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...