JRBeanCollectionDataSource in jasper how to use - java

try {
//Connecting to the MySQL database
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/hospital", "root" ,"root");
String report="C:\\Users\\DELL\\report10.jrxml";
JasperReport jasperreport=JasperCompileManager.compileReport(report);
patientDB DataBeanList = new patientDB();
ArrayList<patientBean> dataList = DataBeanList.getDataBeanList();
JRBeanCollectionDataSource beanColDataSource =
new JRBeanCollectionDataSource(dataList);
Map<String,Object> parameters = new HashMap<String,Object>();
JasperPrint jp=JasperFillManager.fillReport(jasperreport, parameters,beanColDataSource);
JasperViewer.viewReport(jp);
}
catch(Exception e)
{
out.println(e);
}
i am new in jasper report trying to get value from bean class but nothing happen i spend my last 7 days for this

Please clarify,
What is the error you are getting?
does the DataBeanList.getDataBeanList() return objects with values?
Is Db connection configuration is correct?
the path to jrxml is correct?(I think you don't need double slash in path)

Related

I can not choose where to save the generated report in Jasper

I have a problem when I click to generate a report ... I would like the moment I clicked the button to generate the report to be shown a window asking where I want to save the document, the way it is now I'm in java code specifying the location and file name, and always the file is saved in the specified place in the code, I do not want it, I need to leave it open for the person to choose where to save ... down goes a piece of code I am using.
.
try {
URL arquivo = getClass().getResource(/reports/term.jasper);
JasperReport jasperReport = (JasperReport) JRLoader.loadObject(arquivo);
//It generates the dto that will be sent to IReport
ArrayList<MinutoTRDto> dataList = getDataBeanList(licitacao);
JRBeanCollectionDataSource beanColDataSource = new JRBeanCollectionDataSource(dataList);
Map<String, Object> parameters = getParametros();
JasperPrint print = JasperFillManager.fillReport(jasperReport, parameters, beanColDataSource);
JROdtExporter exporterOdt = new JROdtExporter();
exporterOdt.setExporterInput(new SimpleExporterInput(print));
// HERE IS THE PROBLEM!
exporterOdt.setExporterOutput(new SimpleOutputStreamExporterOutput("C://teste//sample_report.odt"));
exporterOdt.exportReport();
} catch (JRException jre) {
jre.printStackTrace();
}
You will have to write the following in your code
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition",
"attachment;filename=<your file name>");
With this, the 'file download' dialog will appear. You can refer here for the complete code.
Following the AMDG replied, I changed my file to the following code, only it was launched the following exception:
org.apache.jasper.JasperException: java.lang.IllegalStateException: getOutputStream() has already been called for this response
I using Wicket from framework
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
try {
URL arquivo = getClass().getResource(REPORT_PATH);
JasperReport jasperReport = (JasperReport) JRLoader.loadObject(arquivo);
ArrayList<MinutoTRDto> dataList = getDataBeanList(licitacao);
JRBeanCollectionDataSource beanColDataSource = new JRBeanCollectionDataSource(dataList);
Map<String, Object> parameters = getParametros();
print = JasperFillManager.fillReport(jasperReport, parameters, beanColDataSource);
JROdtExporter exporterOdt = new JROdtExporter();
exporterOdt.setExporterInput(new SimpleExporterInput(print));
exporterOdt.setExporterOutput(new SimpleOutputStreamExporterOutput(outputStream));
exporterOdt.exportReport();
byte[] bytes = outputStream.toByteArray();
if (bytes != null && bytes.length > 0) {
HttpServletResponse response =(HttpServletResponse)((WebResponse)comp.getResponse()).getContainerResponse();
response.setContentType("application/odt");
response.setHeader("Content-disposition", "inline; filename=\"file_generated.odt\"");
response.setContentLength(bytes.length);
ServletOutputStream outputStream2 = response.getOutputStream();
outputStream2.write(bytes, 0, bytes.length);
outputStream2.flush();
outputStream2.close();
}
} catch (Exception e) {
e.printStackTrace();
}

How to print Jasper reports from servlets?

I am trying to print a Jasper report from my web application's servlet. Below is the code which calls the printing function.
//Print the report
HashMap<String, Object> params = new HashMap<String, Object>();
params.put("companyName",userBean.getCompanyName().trim());
params.put("companyPhone",userBean.getPhone().trim());
params.put("patientName",demographicsBean.get(0).getfName()+" "+demographicsBean.get(0).getmName()+" "+demographicsBean.get(0).getlName());
params.put("patientAge",String.valueOf(demographicsBean.get(0).getDob()));
params.put("address",demographicsBean.get(0).getAdrLine1()+" "+demographicsBean.get(0).getAdrLine2());
java.util.Date date = new java.util.Date();
params.put("date",String.valueOf(new Date(Calendar.getInstance().getTimeInMillis())));
params.put("doctorName",subUserBean.getFirstName()+" "+subUserBean.getLastName());
params.put("drugName",drug);
params.put("ptInstructions",ptInstructionsTxt);
params.put("quantity",request.getParameter("quantityTxt"));
params.put("refills",refilTxt);
Connection connection = DBMaster.getInstance().getConnection();
ReportConnector r = new ReportImpl();
r.printReport(getClass().getResourceAsStream("/ReportResources/Prescription2.jasper"), params,connection);
Below is the code printing the report.
public void printReport(InputStream path, Map<String, Object> params, Connection con) throws JRException
{
JasperPrint jasperPrint = JasperFillManager.fillReport(path, params, con);
JasperPrintManager.printReport(jasperPrint, false);
}
The issue is, eventhough this worked in localhost, nothing is getting printed (I do not have a printer now, but MS One Note should be open as the printer. That is happening in localhost).
what is wrong here?

My Jasper file wont work

I'm trying to make this program but i cannot take a report out of it... even if it worked in java netbeans when i tried to run it using the executable JAR file i get this error !
my coding part is
JRTableModelDataSource datasource = new JRTableModelDataSource(dt.getModel());
String reportSource = "file/door_reports.JRXML";
Map<String, Object> params = new HashMap<>();
params.put("c1", subtot.getText());
JasperReport jasperReport = JasperCompileManager.compileReport(reportSource);
JasperPrint jasperPrint1 = JasperFillManager.fillReport(jasperReport, params, datasource);
JasperViewer.viewReport(jasperPrint1, false);
error is : error compiling java source file D:/test/dist/Blank_A4_1426091490655_11644
above file do not even exist! anybody know what would be the reason?
Use this method, it should be useful for you:
public static void GenerateReport(String filename, HashMap parameter) {
try {
JasperDesign jspDesign = JRXmlLoader.load(filename);
JasperReport jspReport = JasperCompileManager.compileReport(jspDesign);
JasperPrint jspPrint = JasperFillManager.fillReport(jspReport, parameter, yourconnection);
JasperViewer.viewReport(jspPrint, false);
} catch (Exception e) {
JOptionPane.showConfirmDialog(null, e);
}
}
and be sure of the path of the file, good luck.
Did you rename your jxml after you edited it in Jasper?
The report sets the report file name you chose while creating it in Jasper as name, so if you did simple rename your file and not change the name directly in the jxml too... i think there could be your problem.

How to show the content of a List in JasperReports

I have a Collection of type List that I want to pass it to a report using JasperReports and iReport on NetBeans, but I can't manage to show the content of the list on the Report.
I don't know how to show the data in the Details section of the jrxml. What parameter should I use?
The list has Objects of type Proveedor (Provider in Spanish) and has variables like name, code, etc.
Here is the code of the method who call to JasperReports:
public void generateReport(List<Proveedor> aList){
String reportName = System.getProperty("user.dir") + "\\src\\reports\\lista-proveedores.jrxml";
JRBeanCollectionDataSource dataSource;
JasperReport jasperReport;
JasperPrint jasperPrint;
try{
//1-I fill the dataSource with the Collection
dataSource = new JRBeanCollectionDataSource(aList);
//2-Compile the XML
jasperReport = JasperCompileManager.compileReport(reportName);
//3-Fill the report with the datasource
jasperPrint = JasperFillManager.fillReport(jasperReport, null, dataSource);
//4-Export to PDF and save to disk
JasperExportManager.exportReportToPdfFile(jasperPrint, "Proveedores.pdf");
System.out.println("Finished!");
}catch (Exception e){
System.out.println(e);
//e.printStackTrace();
}
}

How I can join compiled jasper file and my project in java?

I have design my project in iReport and I have compiled successfully. Now I want to add the compiled jasper file to add in my java project. May any one help me?
Just add them in some package in your source tree. Your build process or IDE should copy them in your classes directory or in the generated jar file, along with your classes. The reports may then be loaded with Class.getResource() or Class.getResourceAsStream().
Here is the code you need in order to get two values from two combo-boxes, add them to a HashMap and pass the map to iReport. These paramaters, for this example "storeName" and "actionCode", are used to specify values for the query which is stored inside the iReport. The report jasper file is "../ireps/AccessCounter.jrxml". The viewer is a JDialog. If you have no parameters to pass to your report just skip the map.put. con is the connection to the database.
Make sure that you include the necessary jar file to your path.
try {
String shopName = jComboBox1.getSelectedItem().toString();
String actionCode = jComboBox2.getSelectedItem().toString();
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("storeName", shopName);
map.put("actionCode", actionCode);
URL reportFileURL = getClass().getResource("../ireps/AccessCounter.jrxml");
File reportFile = new File(reportFileURL.toURI());
JasperDesign jasperDesign = JRXmlLoader.load(reportFile);
JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, map, con);
JasperViewer jv = new JasperViewer(jasperPrint);
JDialog viewer = new JDialog(this, "Batch Report", true);
viewer.setBounds(jv.getBounds());
viewer.getContentPane().add(jv.getContentPane());
viewer.setResizable(true);
viewer.setIconImage(jv.getIconImage());
viewer.setVisible(true);
} catch (JRException exc) {
System.out.println(exc.getMessage());
} catch (URISyntaxException exs) {
System.out.println(exs.getMessage());
}

Categories