2013-06-19 73 views
5

我嘗試導出JRXML文件爲PDF格式,但我得到這個錯誤:提供的java.sql.Connection對象爲null

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。

謝謝。

回答

9

我解決了我的問題..我不得不爲我的報告指定數據庫連接!

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

謝謝。這工作:) –