我正在尝试将 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 的新手,所以我有点迷茫.. 我必须指定连接字符串到 de 数据库还是什么?我应该在哪里添加它。
顺便说一句,我正在使用 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 - 提供的 java.sql.Connection 对象为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17183317/