-1

I want to view report by using customerId.

JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, ?, ?);// How to fill?

I tried :
BLManager.java

public void report(int custId) throws JRException, FileNotFoundException {
        Session session = sessionFactory.openSession();
        Criteria criteria = session.createCriteria(Customer.class);
        criteria.add(Restrictions.eq("custId", custId));
        Customre customer = (Customer) criteria.uniqueResult();

        FileInputStream fis = new FileInputStream("src/com/customer/reports/report.jrxml");
        BufferedInputStream bis = new BufferedInputStream(fis);

        JasperReport jasperReport = (JasperReport) JasperCompileManager.compileReport(bis);
        JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, ?, ?);// How to fill?

        JasperViewer.viewReport(jasperPrint, false);
    }

Further I calling this method on buttonClick
Client Class

@FXML
private void viewReport(ActionEvent e) {
    Customer customer = customerTable.getSelectionModel().getSelectedItem();
    if (customer != null) {
        int custId = customer.getCustId();
        try {
            bLManager.report(custId);
        } catch (FileNotFoundException | JRException ex) {
            Logger.getLogger(FollowUpController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
kleopatra
  • 51,061
  • 28
  • 99
  • 211
Swapnil
  • 115
  • 1
  • 12
  • 2
    You have 2 options, 1. put the Customre in the parameter map (? one) and pass JREmptyDatasource() in ? two, then use the for example the title band and display the data in parameter. 2. Pass an empty parameter map in ? one and create a JRBeanCollectionDatasource (adding Customre in a List), then use the detail band to display data. – Petter Friberg Oct 11 '19 at 07:54
  • 1
    Overall currently this question is a bit unclear/too broad, you need to study some and understand both of your question marks. First ? is the the parameter map, study what it does and how to use it in jrxml, second is a connection or a JRDatasource, study how to use it in jrxml. Once you have the basic, try and if still problem post also your jrxml creating a [mcve] – Petter Friberg Oct 11 '19 at 07:55
  • Thanks for attention. I don't need to use a List since I'm looking for unique field. – Swapnil Oct 11 '19 at 07:58
  • I understand either use the parameter map then, or create the List anyway so you can easily create the JRBeanCollectionDatasource, seems useless to create the List but I would probably have gone for this solution, since maybe 1 day I need to print more then one, hence report will also work if you need to print multiple – Petter Friberg Oct 11 '19 at 08:00
  • To answer this question see: http://jasperreports.sourceforge.net/api/net/sf/jasperreports/engine/JasperFillManager.html#fillReportToFile-java.lang.String-java.util.Map-net.sf.jasperreports.engine.JRDataSource- :) – Petter Friberg Oct 11 '19 at 08:01
  • I tried with list but it is helpless since I want to retrieve an unique value. – Swapnil Oct 11 '19 at 09:27
  • I need to specify connection in second ?. – Swapnil Oct 11 '19 at 09:28
  • No, you don't need connection since you have the bean, if you are doing it with a List create a JRBeanCollectionDatasource from the List and pass that. – Petter Friberg Oct 11 '19 at 09:38
  • I shouldn't use List cause I want unique value to generate report. for example want to generate report of 1st number id. That I'll give input 1 from textfield – Swapnil Oct 11 '19 at 09:40
  • that's why at second ? I'm thinking to specify connection – Swapnil Oct 11 '19 at 09:41
  • Why?, listen, I know you have only 1, but trust me pass a JRBeanCollectionDataSource, (that you create from a List, yes only 1 record), but then your report is easy to make, create fields with name as your bean, put the textField in detail band (this will iterate 1 time, since unique).. The day you wan't to print 2 records you pass 2 records in List. – Petter Friberg Oct 11 '19 at 09:44
  • There is the other solution to pass the bean in parameter map and not specify anything in 3 parameter, but this is more complex, since you will not have detail band, you need to put all fields in title or in summary band, then you need to call the methods on your parameter, you need to set report to display all even if datasource is empty etc... It's more work then creating a normal/starndard datasource... Yes it's only 1 record but who cares and maybe one day you like to display more then 1 record. – Petter Friberg Oct 11 '19 at 09:47
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/200700/discussion-between-swapnil-and-petter-friberg). – Swapnil Oct 11 '19 at 09:53

1 Answers1

1

When you use fillReport method of JasperFillManager, you can pass a parameterMap.

A parameterMap is a HashMap where you put as key the name of paramter managed by the report, and as value the object instance.

This is my code to fill parameterMap (you can use as example for your case):

Map<String, Object> parameterMap = new HashMap<String, Object>;
parameterMap.put("datasource", jRdataSource);
parameterMap.put("MyComplexObject", myComplexObject); // you can pass a pojo
parameterMap.put("Title", "My report title");

You can see the documentation about JasperFillManager methods here

Joe Taras
  • 15,166
  • 7
  • 42
  • 55
  • Well I had used map but it was difficult to do, I mean I was getting an exception as no such method from where I was designing a query in jrxml that was not matching with pojo fields. – Swapnil Oct 15 '19 at 12:49
  • When you use a parameter map, in Jasper you must use Parameter object to process. If you want, post your code to further answer – Joe Taras Oct 15 '19 at 12:51
  • I used JDBC instead but I'm facing slower speed of report generation. [Jasper Reports 6.7.0 generating report is slow](https://stackoverflow.com/questions/58389318/jasper-reports-6-7-0-generating-report-is-slow) I've asked here. – Swapnil Oct 15 '19 at 12:51
  • @Swapnil: I've written a comment to your question about report performance – Joe Taras Oct 15 '19 at 12:54
  • I've checked it. Thanks for attention and time – Swapnil Oct 15 '19 at 12:55