在jasperReport文件中出现错误 [英] Error Occured in jasperReport File

查看:115
本文介绍了在jasperReport文件中出现错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先我在iReport 5.1.0中创建一个R_D1.jrxml文件。

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

我执行报告的Java代码如下所示:

My Java code to execute the report looks like:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.HashMap;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperFillManager;

public class DbReportFill{

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

  public static void main(String[] args) {
    new DbReportFill().generateReport();
  }

}

当我执行课程时,我得到以下例外:

When I execute the class I get the following exception:

Filling report...
net.sf.jasperreports.engine.JRException: Error loading object from file : /home/abcd/report/R_D1.jrxml  
at net.sf.jasperreports.engine.util.JRLoader.loadObject(JRLoader.java:127)  
at net.sf.jasperreports.engine.util.JRLoader.loadObject(JRLoader.java:99)   
at net.sf.jasperreports.engine.JasperFillManager.fillToFile(JasperFillManager.java:117) 
at net.sf.jasperreports.engine.JasperFillManager.fillReportToFile(JasperFillManager.java:666)
at DbReportFill.generateReport(DbReportFill.java:24)    
at DbReportFill.main(DbReportFill.java:56)
Caused by: java.io.StreamCorruptedException: invalid stream header: 3C3F786D    
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:802)   
at java.io.ObjectInputStream.<init>(ObjectInputStream.java:299) 
at net.sf.jasperreports.engine.util.ContextClassLoaderObjectInputStream.<init>(ContextClassLoaderObjectInputStream.java:58)
at net.sf.jasperreports.engine.util.JRLoader.loadObject(JRLoader.java:122)
... 5 more

我不确定我做错了什么,或者这个例外意味着什么。

I am not sure what I am doing wrong, or what this exception means.

推荐答案

这里的主要问题是你没有编译文件。将JRXML文件视为Java源文件。要运行您的java文件,您必须先编译它,然后才能运行。 jrxml文件只是人类可读的XML文件,用于描述您想要发生的事情。

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.

要编译您执行的文件:

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

这将返回你和 JasperReport ,这是已编译的文件。 (这通常写在.jasper文件中,因此您不必在每次运行时编译报告,但这超出了本问题的范围)。完成后,您可以填写报告。

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.

此外,无关,但值得一提的是,您应该在finally块中关闭数据库连接。在您当前的示例中,它永远不会关闭,因为抛出异常。 finally块将确保即使在异常情况下它也将被关闭。

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.

这篇关于在jasperReport文件中出现错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆