【问题标题】:How to view only one record from a table using JasperReports?如何使用 JasperReports 从表中仅查看一条记录?
【发布时间】:2012-12-13 06:00:05
【问题描述】:

我现在正在从事一个临床项目,我需要为该项目打印特殊报告,例如特定患者的私人信息。

所以我确实为我的项目使用了数据库,并在数据库和 JDeveloper 之间建立了连接。我设计了由 JasperReport 5 程序打印的报告,并在 Jasperreport 5 程序和 JDeveloper 之间建立了连接。现在我想通过Patient_Id 打印特定患者的报告。最后,我需要为我的数据库中的表中的一条记录打印报告,而不是数据库中表中的所有记录。

这是连接 JasperReport 和 JDeveloper 的代码:

Connection con;

InputStream input=null;

Class.forName("oracle.jdbc.driver.OracleDriver");
String url = "jdbc:oracle:thin:@localhost:1521:orcl";
con = DriverManager.getConnection(url, "hr", "hr");
input=new FileInputStream(new File("report.jrxml")); 

JasperDesign jasperDesign;
jasperDesign=JRXmlLoader.load(input);

JasperReport jasperReport;
jasperReport=JasperCompileManager.compileReport(jasperDesign);
JasperPrint jasperPrint;
jasperPrint=JasperFillManager.fillReport(jasperReport,null,con);
JRViewer v=new JRViewer(jasperPrint);
v.setVisible(true);
JFrame fr2=new JFrame();
fr2.setSize(200, 200);
fr2.add(v);
fr2.setVisible(true);

input.close();
con.close();

【问题讨论】:

    标签: java jasper-reports


    【解决方案1】:

    从所提供代码的外观来看,您已嵌入查询以在报告本身中获取患者信息。这很好。现在您需要微调该查询以获得单个结果。所以基本上你需要在报告中传递一个参数来给它病人ID,然后过滤结果。由于您已经在使用 sql,因此可以直接在 sql 中进行过滤(这是数据库擅长的,所以让它去做吧)。

    以下是您需要遵循的步骤:

    1. 在您的报告中创建一个名为 PATIENT_ID 类型的新参数 Integer(或者可能更合适,具体取决于 患者 ID 的数据库中的字段类型)。
    2. 在 中编辑您的查询以添加 WHERE 子句(或附加到 现有的 WHERE 子句使用和 AND) 以下(假设 患者 ID 在名为 Patient_id 的列中):
      Patient_id = $P!{PATIENT_ID}
    3. 现在您需要在您的 java 代码中传入患者 ID。所以创建一个Map 并像这样添加它:

      Map<String, Object> parameters = new HashMap<String, Object>();
      parameters.put("PATIENT_ID", <the patient id value here>);
      

      然后您需要将其传递给您对fillReport 方法的调用,因此将其更改为如下所示:

      jasperPrint=JasperFillManager.fillReport(jasperReport,parameters,con);
      

    就打印而言,JRViewer 已经内置了打印功能。所以用户只需点击打印按钮即可。

    【讨论】:

      【解决方案2】:

      您应该在作为 second argument to fillReport 传递的 Map 中指定您的条件,例如:

      Map<String, Serializable> conditions = new HashMap<String, Serializable>();
      conditions.put("PATIENT_ID", 1);
      JasperPrint jasperPrint = JasperfillManager.fillReport(jasperReport, conditions, con);
      

      希望对您有所帮助,让我知道您的进展情况。

      【讨论】:

      • 很抱歉问题没有解决
      • @mhmad 输出是什么?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-04-06
      • 1970-01-01
      • 1970-01-01
      • 2021-12-21
      • 2017-06-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多