3

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

所以我确实为我的项目使用了数据库,并在数据库和 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();
4

2 回答 2

1

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

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

  1. 在您的报告中创建一个名为PATIENT_IDtype 的新参数Integer(或者可能更合适,具体取决于数据库中患者 ID 的字段类型)。
  2. 在 中编辑您的查询,使其具有以下子句(或使用 andWHERE附加到现有子句)(假设患者 ID 在名为 的列中):WHEREANDPatient_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它已经内置了打印功能。所以用户只需要点击打印按钮。

于 2012-12-12T21:58:13.533 回答
1

您应该在作为第二个参数传递给 fillReport的 Map 中指定您的条件,例如:

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

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

于 2012-12-12T22:00:05.623 回答