提供的 java.sql.Connection 对象为 null
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17183317/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
The supplied java.sql.Connection object is null
提问by user1462933
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? and where should i add it.
我是 jasperreports 的新手,所以我有点迷茫..我必须指定连接字符串到数据库还是什么?我应该在哪里添加它。
BTW, i'm using JSF 2, intellij and maven.
顺便说一句,我正在使用 JSF 2、intellij 和 maven。
thanks.
谢谢。
回答by user1462933
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);
回答by Vlad Schnakovszki
If you want to use Java beans as source instead of a DB connection, you can do it like this to avoid the exception:
如果你想使用 Java bean 作为源而不是 DB 连接,你可以这样做以避免异常:
final JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, JRBeanCollectionDataSource(fields));