简体   繁体   English

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

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

im trying to export a jrxml file to pdf, but i get this error:我试图将 jrxml 文件导出为 pdf,但出现此错误:

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

i only get a blank pdf file..我只得到一个空白的pdf文件..

This is my method to export to 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();
}

I'm new with jasperreports so i'm a little lost.. do i have to specify the connection string to de database or what?我是 jasperreports 的新手,所以我有点迷茫.. 我必须指定连接字符串到数据库还是什么? and where should i add it.我应该在哪里添加它。

BTW, i'm using JSF 2, intellij and maven.顺便说一句,我正在使用 JSF 2、intellij 和 maven。

thanks.谢谢。

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

如果你想使用 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