Error Occured in jasperReport File

前端 未结 2 1135
栀梦
栀梦 2021-01-11 21:53

First I make one R_D1.jrxml file in iReport 5.1.0.

My Java code to execute the report looks like:

import java.sql.Connection;
import java.sql.DriverM         


        
相关标签:
2条回答
  • 2021-01-11 22:19

    Your main problem here is that you have not compiled the file. Think of the JRXML file as a Java source file. To run your java file you have to compile it first, and then you can run. The jrxml file is simply the human readable XML file that describes what you want to happen.

    To compile the file you do:

    JasperCompileManager.compileReport("/home/abcd/report/R_D1.jrxml");
    

    This is going to return you and instance of a JasperReport, which is the compiled file. (this is often written out to a .jasper file, so you do not have to compile the report on each run, but that is beyond the scope of this question). Once you have this you can then fill the report.

    Also, unrelated, but worth mentioning, is that you should be closing the you database connection in a finally block. As in your current example it is never closed, since an exception is thrown. A finally block will ensure that even in the event of an exception it would be closed.

    You sample method should look like:

    public void generateReport() {
      Connection con
      try {
        Class.forName("com.mysql.jdbc.Driver");
        con = DriverManager.getConnection("jdbc:mysql://localhost:3306/sentiment","root", "abcd");
        System.out.println("Compiling report...");
        JasperReport jasperReport = JasperCompileManager.compileReport("/home/abcd/report/R_D1.jrxml");
        System.out.println("Filling report...");
        JasperFillManager.fillReportToFile(jasperReport,new HashMap<String, Object> (), con);
        System.out.println("Done!");
      } catch (JRException e) {
        e.printStackTrace();
      } catch (ClassNotFoundException e) {
        e.printStackTrace();
      } catch (SQLException e) {
        e.printStackTrace();
      } finally {
        if (con != null){
          con.close();
        }
      }
    }
    

    Hope that helps. Good luck.

    0 讨论(0)
  • 2021-01-11 22:26

    If you are creating ".jrxml file" by using ireport tool then which will give you .jasper file ...If you don't want to compile then you can use already compiled .jasper file in your java program like this:

    JasperCompileManager.compileReport("/home/abcd/report/R_D1.jasper");
    

    Thanks, Krish

    0 讨论(0)
提交回复
热议问题