gpt4 book ai didi

java - 如何使用java将参数传递给JasperReport以便稍后在SQL查询中使用

转载 作者:行者123 更新时间:2023-11-30 06:56:56 28 4
gpt4 key购买 nike

我已经创建了 6 个 Jasper Report 模板,所有静态文本字段都使用 SQL 查询来填写。 SQL 查询使用我传入的 2 个参数:FirstNameLastName。我还传递了另外 2 个参数,它们将被添加到报告中。

这是我到目前为止将带有参数的 HashMap 传递给报告的代码。但是我迷路了,因为我真的找不到任何好的文档或示例。

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);
}
}
}
}

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

最佳答案

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

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

enter image description here

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

    enter image description here如果您需要通过查询过滤数据,您可以将参数添加到 SQL 查询。只需使用新参数按钮添加参数,您就可以将显示在文本区域内的参数拖放到查询中。

在您的报告检查器中,您可以在Fields 类别下看到我们查询的所有列名,以及在Parameters 类别下定义的所有参数。

enter image description here

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

    通过将字段名称和参数拖放到您的报表设计器,您可以根据需要设计报表。 Title Band(此处为您的标题)、Column Header Band(列名称)、Detail Band(此处为迭代数据)和Summary Band (如Grand_Total、Date、Issued By和图表元素可以添加到此部分)对于基本报告来说已经足够了。

    enter image description here
  • 然后你可以声明ReportGenarator类来生成你的报告

    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();
    }
    } }
  • 您可以通过在您的应用程序中按下一个按钮来生成您的报告。因此您必须在您的按钮中包含以下代码 Action 事件

    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 - 如何使用java将参数传递给JasperReport以便稍后在SQL查询中使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33966181/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com