繁体   English   中英

提供的 java.sql.Connection 对象为 null

[英]The supplied java.sql.Connection object is null

我试图将 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。

谢谢。

我解决了我的问题..我必须为我的报告指定一个数据库连接!

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) {

}

然后在这一行添加连接:

JasperPrint jasperPrint = JasperFillManager.fillReport(report, new HashMap<String, String>(), conn);

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

final JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, JRBeanCollectionDataSource(fields));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM