简体   繁体   English

如何将JRDataSource用于Jasper报告

[英]How to use JRDataSource for jasper reports

Previously I am sending the connection object to the fillReport() of JasperFillManager class. 以前,我将连接对象发送到JasperFillManager类的fillReport() It is working fine and it is generating the data also. 它工作正常,并且还在生成数据。 The below is the code I am using for database connection. 以下是我用于数据库连接的代码。

Connection conn = null; 
Class.forName("com.mysql.jdbc.Driver"); 
conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/dbname", "username", "password");

But now I don't want to pass the connection object to the fillReport() instead I want to pass the JRDataSource. 但是现在我不想将连接对象传递给fillReport()而是想传递JRDataSource。 How can I achieve this? 我该如何实现? How to configure this JRDataSource in my spring configuration file with mysql database information? 如何在我的Spring配置文件中使用mysql数据库信息配置此JRDataSource Can any one please tell me. 谁能告诉我。

I am using iReport 4.5.0 and Spring 3.0.5 RELEASE . 我正在使用iReport 4.5.0Spring 3.0.5 RELEASE If you want anymore information I will give you. 如果您需要更多信息,我会给您。

You can get the result set from database with the conn. 您可以使用conn从数据库中获取结果集。 Then you will use JRResultSetDataSource to instantiate a data source. 然后,您将使用JRResultSetDataSource实例化数据源。

        Connection con = getConnection();
        PreparedStatement statement = con.prepareStatement(jdbcString.toString());
        ResultSet rs = statement.executeQuery();
        jrDataSource = new JRResultSetDataSource(rs);
JasperReport report 
   = (JasperReport)JRLoader.loadObject(
       "F:\\Tomcat 7.0\\webapps\\ivmreports\\jasper\\FailedInsertionEventsPDF.jasper");
// Map<String, Object> parameterMap = new HashMap<String, Object>();
JasperPrint print = JasperFillManager.fillReport(report, parameterMap, reportData);

Here reportdata is JRDatasource 这里的reportdata是JRDatasource

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM