mhmad
mhmad

Reputation: 43

How to view only one record from a table using JasperReports?

I am now working on a clinic project and I need to print special reports for the project, such as private information for specific patient.

So I did work the database for my project and I made a connection between the database and JDeveloper. I have designed my report to be printed by the program JasperReport 5 and I have made a connection between the program Jasperreport 5 and JDeveloper. Now I want to print the report for a specific patient by Patient_Id. Finally i need print report for one record from table in my database, not all records in the table from database.

This is the code to connect between JasperReport and 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();

Upvotes: 3

Views: 2406

Answers (2)

hd1
hd1

Reputation: 34667

You should specify your conditions in a Map that you pass as the second argument to fillReport, eg:

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

Hope that helps, let me know how you get on.

Upvotes: 1

Jacob Schoen
Jacob Schoen

Reputation: 14212

From the looks of the code supplied, you have embedded the query to get the patient information in the report itself. Which is fine. Now you need to fine tune that query to get a single result. So basically you need to pass in a parameter to the report to give it the patient id, and then filter the results by that. Since you are already using sql, it makes to since to do that filtering directly in the sql (that is what databases are good at, so let it do it).

Here are the steps you need to follow:

  1. In your report create a new parameter named PATIENT_ID of type Integer (or possibly something more appropriate depending on the field type in your database for the patient id).
  2. Edit your query in the to have a WHERE clause (or append to existing WHERE clause using and AND) the following (assuming patient id is in a column named Patient_id):
    Patient_id = $P!{PATIENT_ID}
  3. Now you need to need to pass in the patient id in in your java code. So create a Map and add it to it like so:

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

    Then you need to pass it in to the your call to the fillReport method, so change it to look like:

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

As far as printing goes, the JRViewer has printing capabilities built into it already. So the users just need to click the print button.

Upvotes: 1

Related Questions