6

我试图将 jrxml 文件导出为 pdf,但出现此错误:

WARN query.JRJdbcQueryExecuter  - The supplied java.sql.Connection object is null.

我只得到一个空白的pdf文件..

这是我导出为 PDF 的方法:

public void printReport(ActionEvent event) throws JRException, IOException {

    String reportPath = FacesContext.getCurrentInstance().getExternalContext().getRealPath("/test.jrxml");
    JasperReport report = JasperCompileManager.compileReport(reportPath);
    JasperPrint jasperPrint = JasperFillManager.fillReport(report, new HashMap<String, String>());
    HttpServletResponse httpServletResponse = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
    httpServletResponse.addHeader("Content-disposition", "attachment; filename=report.pdf");
    ServletOutputStream servletOutputStream = httpServletResponse.getOutputStream();
    JasperExportManager.exportReportToPdfStream(jasperPrint, servletOutputStream);
    FacesContext.getCurrentInstance().responseComplete();
}

我是 jasperreports 的新手,所以我有点迷路了。我必须指定连接字符串到数据库还是什么?我应该在哪里添加它。

顺便说一句,我正在使用 JSF 2、intellij 和 maven。

谢谢。

4

2 回答 2

11

i solved my issue.. i had to specify a DB connection for my report!

Connection conn;
try {
    Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
    conn = DriverManager.getConnection("jdbc:sqlserver://localhost:55334;databaseName=Frutemu;integratedSecurity=true","","");
} catch (SQLException ex) {
} catch (ClassNotFoundException ex) {

}

and then in this line add the connection:

JasperPrint jasperPrint = JasperFillManager.fillReport(report, new HashMap<String, String>(), conn);
于 2013-06-19T17:54:21.793 回答
1

如果你想使用 Java bean 作为源而不是 DB 连接,你可以这样做以避免异常:

final JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, JRBeanCollectionDataSource(fields));
于 2018-10-10T15:36:52.113 回答