如何使用java将参数传递给JasperReport以便稍后在SQL查询中使用 [英] How to pass parameters to JasperReport with java to use later in SQL query

查看:1076
本文介绍了如何使用java将参数传递给JasperReport以便稍后在SQL查询中使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了6个Jasper Report模板,所有静态文本字段都使用SQL查询填写。 SQL查询使用我传入的2个参数: FirstName LastName 。我还传递了另外两个参数,这些参数只会被添加到Report中。

I already have 6 Jasper Report templates created, with all of the static text fields to be filled out using a SQL query. The SQL query uses 2 parameters that I am passing in: FirstName and LastName. I am also passing in 2 other parameters that will just get added to Report.

这是我到目前为止将带有参数的HashMap传递给Report的代码。但我很遗憾,因为我找不到任何好的文档或示例。

This is the code i have so far to pass the HashMap with the parameters to the Report. But I am lost as there isnt really any good documentation or examples that I can find.

package print;
import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.engine.export.*;
import java.util.*;

public class PrintCertificate  
{   
   public PrintCertificate(String output, String certType, String firstName, String lastName, String confirmDate, String pastorName)
   {
    if(certType=="rci_eng")
    {
        String fileName = "/RCI_Eng.jasper";
        output = "C:/Users/User/Desktop/Test/";

        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("FirstName",firstName);
        map.put("LastName",lastName);
        map.put("PastorName", pastorName);
        map.put("DateOfConfirmation", confirmDate);
        try
        {
            JasperPrint print = JasperFillManager.fillReport(fileName, map);
            JRDocxExporter exporter = new JRDocxExporter();
            exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
            exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, "test.docx");
            exporter.exportReport(print);

        }
        catch (Exception e)
        {
            e.printStackTrace();
            System.exit(1);
        }
    }
  }
}

I知道这可能远非正确,但如果有人可以指出我正确的文档或示例方向,或者指出我做错了什么,那将会有很大的帮助!

I know this is probably far from correct, but if someone can point me in the right direction of good documentation or examples, or point out what I've done wrong, it would be of great help!

推荐答案


以下是将Jasper报告与Java应用程序一起使用的简要说明

Here is a brief description to use Jasper Report with your Java Application




  • 首先,您必须设置与报告的数据库连接。

    • First you have to set database connection to the Report.

      • 然后您可以为报告设计SQL查询。



        如果需要通过查询过滤数据,可以向SQL查询添加参数。只需使用新参数按钮添加参数,即可拖动将文本区域内显示的参数拖放到查询中。

      • Then you can design your SQL query for the report.

        You can add parameters to the SQL query in case you need to filter data through your query.Just add parameters using New Parameter button and you can drag and drop parameters displaying inside the text area to your query.

      在报表检查器中,您可以在<下查看我们查询的所有列名称。 strong>字段类别以及参数类别下定义的所有参数。


      In your report inspector you can see all the column names of our query under Fields category and all the parameters defined under Parameters category.


      • 您可以设计如下的基本报告


        通过拖放字段名称和您可以根据需要设计报告报告设计器的参数。 标题带(此处为您的标题),列标题带(列名称),详细信息带(此处为迭代数据)和摘要带(如同Grand_Total,日期,发布者和图表元素可添加到此部分)足以用于基本报告。



      • 然后您可以声明 ReportGenarator 类来生成
        报告

      • You can design a basic report like below

        By dragging and dropping field names and parameters to your Report Designer you can design your report as you desire. Title Band(Your Title Here) , Column Header Band(Column Names) , Detail Band(Iterative Data here) and Summary Band(Like Grand_Total,Date,Issued By and chart elements can be added to this section) are enough for basic report.

      • Then you can declare ReportGenarator class to generate your report

      public class ReportGenarator {
      
      public static String OUT_PUT = "your_output_file_path/myreport.docx";
      public static String REPORT = "your_report_path/myreport.jrxml";
      
      public void genarateReport(String reportPath,
              Map<String, Object> map, Connection con) {
          try {
      
              JasperReport jr = JasperCompileManager.compileReport(
                      ClassLoader.getSystemResourceAsStream(reportPath));
              JasperPrint jp = JasperFillManager.fillReport(jr, map, con);
              JRDocxExporter export = new JRDocxExporter();
          export.setExporterInput(new SimpleExporterInput(jp));
          export.setExporterOutput(new SimpleOutputStreamExporterOutput(new File(OUT_PUT)));
          SimpleDocxReportConfiguration config = new SimpleDocxReportConfiguration();
          export.setConfiguration(config);
          export.exportReport();
          } catch (JRException ex) {
              ex.printStackTrace();   
          }
      } }
      


    • 您可以生成通过按下应用程序中的按钮进行报告。因此,您必须在按钮内包含以下代码
      操作事件


    • You can generate your report by Pressing a button in your application.So that you have to include below code inside your button action event

      Map<String, Object> map = new HashMap<>();
                  map.put("headding", "REPORT FROM DATABASE CONNECTION");//parameter name should be like it was named inside your report.
                  new ReportGenarator().genarateReport(
                          ReportGenarator.REPORT, map, your_DB_connction_reference_here);
      


    • 这篇关于如何使用java将参数传递给JasperReport以便稍后在SQL查询中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆