1

I am developing the Invoice printing System using the Jasper report, Now I have can print the report but it will take 2000ms to load the report, also 2000ms take to start the printing, These are I am using the JAR FILES,

commons-beanutils-1.4 commons-digester-1.7 commons-logging-1.0.3 commons-beanutils-1.4 groovy-all-1.7.5 batik-all-1.7 barcode4j-2.1 itextpdf-5.1.0 jasperreports-6.0.3 xercesImpl-2.11.0 xml-apis-ext-1.3.04

preparedStatement = conn.prepareStatement(sqlString);
    resultSet = preparedStatement.executeQuery();

    if (resultSet.next()) {

        HashMap<String, Object> hm = new HashMap<>();

        JasperDesign jasperDesign = JRXmlLoader.load(new File(
                "C:/Invoice/Invoice.jrxml"));

        JRDesignQuery designQuery = new JRDesignQuery();
        designQuery.setText(sqlString);
        jasperDesign.setQuery(designQuery);

        JasperReport jasperReport = JasperCompileManager
                .compileReport(jasperDesign);
        JasperPrint jasperPrint = JasperFillManager.fillReport(
                jasperReport, null, conn);

        for (int i = 0; i < copies; i++) {

            JasperPrintManager.printReport(jasperPrint, false);


        }

        JasperViewer.viewReport(jasperPrint); 

The above code I used to print the report but, getting too much time to load the report. Please suggest my fault and some other idea...

AngelJanniee
  • 613
  • 1
  • 11
  • 30

1 Answers1

0

I guess the SQL query could be the problem. Have you tried executing it in a sql query tool and check how long it runs? Optimizing it should yield a performance win.

You are also running the query twice: before the report and when you are filling it via the JasperFillManager.fillReport() call.

If there are no specific reasons to check wheter the query contains any records, I would recommand to let JasperReports handle the query and the case that no record is found. You can determine a specific behaviour in that case via the "When No Data Type" property in the reportdesign.

  • I already check the SQL, it is executing fast. "You are also running the query twice: before the report and when you are filling it via the JasperFillManager.fillReport() call." I did not get this point, can you explain me this, This is I put the sample code. So, I have validate the When No Data Type, in my project. When I debug this code, it takes too much time in these lines JRDesignQuery designQuery = new JRDesignQuery(); JasperPrint jasperPrint = JasperFillManager.fillReport( jasperReport, null, conn); – AngelJanniee May 13 '15 at 01:49
  • You should precompile the .jrxml into a .jasper file. This way, you can load it directly into an JasperReport object. Then you can pass the resultSet by wrapping it in an JRResultSetDataSource. – soylentgreen81 May 13 '15 at 07:51
  • It should look something like this: `preparedStatement = conn.prepareStatement(sqlString); resultSet = preparedStatement.executeQuery(); if (resultSet.next()) { resultSet.beforeFirst(); JasperReport jasperReport = JRLoader.load(new File( "C:/Invoice/Invoice.jasper")); JasperPrint jasperPrint = JasperFillManager.fillReport( jasperReport, null, new JRResultSetDataSource(resultSet));` – soylentgreen81 May 13 '15 at 07:55
  • but I couldn't able to get the .jasper compiled report in my application. but if I create the new sample projects are showing the .jasper why is that ? – AngelJanniee May 13 '15 at 08:18
  • 1
    [http://stackoverflow.com/questions/4456779/how-do-i-compile-jrxml-to-get-jasper](http://stackoverflow.com/questions/4456779/how-do-i-compile-jrxml-to-get-jasper) – soylentgreen81 May 13 '15 at 08:30
  • JasperReport jasperReport = JRLoader.load(new File( "C:/Invoice/Invoice.jasper")); Is this line working properly? because if I use this code then I got the below error The method load(File) is undefined for the type JRLoader I missed any jar? – AngelJanniee May 13 '15 at 09:00
  • `JasperReport jasperReport = (JasperReport) JRLoader.loadObject(new File( "C:/Invoice/Invoice.jasper"));` – soylentgreen81 May 13 '15 at 09:37
  • Actually resultSet.beforeFirst() method we cannot use here, bcz I will get an exception when I try to go back with beforeFirst(), I got the exception below The requested operation is not supported on forward only result sets. – AngelJanniee May 13 '15 at 10:00
  • Then don't use resultSet.next() to check wether it's empty. [You can use resulset.isBeforeFirst() without moving the resultset-cursor](http://stackoverflow.com/questions/867194/java-resultset-how-to-check-if-there-are-any-results/6813771#6813771) – soylentgreen81 May 13 '15 at 10:30