use*_*933 6 java jasper-reports
即时尝试将jrxml文件导出为pdf,但我收到此错误:
WARN query.JRJdbcQueryExecuter - The supplied java.sql.Connection object is null.
Run Code Online (Sandbox Code Playgroud)
我只得到一个空白的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();
}
Run Code Online (Sandbox Code Playgroud)
我是jasperreports的新手,所以我有点迷失..我必须指定连接字符串到数据库或什么?我应该在哪里添加它.
顺便说一下,我正在使用JSF 2,intellij和maven.
谢谢.
use*_*933 11
我解决了我的问题..我必须为我的报告指定数据库连接!
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) {
}
Run Code Online (Sandbox Code Playgroud)
然后在这一行添加连接:
JasperPrint jasperPrint = JasperFillManager.fillReport(report, new HashMap<String, String>(), conn);
Run Code Online (Sandbox Code Playgroud)