-1

I am trying to generate some reports using JasperReports this is what I've done so far.

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         

    Connection conn = null;

    try{

        Class.forName("org.postgresql.postgresql-42.2.2");
        conn = DriverManager.getConnection("jdbc:postgresql://localhost:5432/test", "postgres", "testPassword");
        String report = "";

        JasperReport jasperReport = JasperCompileManager.compileReport(report);

        JasperPrint jp = JasperFillManager.fillReport(jasperReport, null, conn);

        JasperExportManager.exportReportToPdfStream(jp, response.getOutputStream());
        response.getOutputStream().flush();
        response.getOutputStream().close();

    }catch(Exception ex){
        System.out.println(ex);
    }
}                                        

I did all this with the help of this video

But the "response.getOutputStream()" doesnt work. it wont find the response variable. (the tutorial is made in eclipse so thought that is the cause ,or netbeans wont be able to find it..

What can be the solution for this? do you have any other way how to do it?

BTEK
  • 9
  • 3
  • You want to connect PostgreSQL database with Jasper report Studio or with iReport to generate the report using PostgreSQL database right? – Pawan Sharma Jun 29 '18 at 08:47
  • I have designed an app that will generate certain reports, its written in netbeans. and I want to connect postgresql with Jasper Reprot – BTEK Jun 29 '18 at 08:55
  • What do you want to do with the report generated? – Usagi Miyamoto Jun 29 '18 at 08:57
  • Your PDF export code seem to be from a Servlet, where `response` is the variable to hold the response to the browser... Your method seems to be a Swing action (a button press) where there is no response to a browser. You might want to save the PDF to a file... – Usagi Miyamoto Jun 29 '18 at 09:02
  • Ok, First download the ireport using this link https://sourceforge.net/projects/ireport/ , then i will ask you for database connection select "Database JDBC Connection" -->Next--> Select PostgreSQL JDBC connection " PostgreSQL (org.postgresql.Driver)" in JDBC url pass the correct hostname,database name and port number .. – Pawan Sharma Jun 29 '18 at 09:02
  • @PawanSharma The problem is with the `response` variable, that is at the PDF exporting code. – Usagi Miyamoto Jun 29 '18 at 09:04
  • but he/she didn't describe PDF exporting issue.. – Pawan Sharma Jun 29 '18 at 09:06
  • this is not supposed to be a web thigm I want to save it in a pdf, or at least just show as a report. – BTEK Jun 29 '18 at 09:14

2 Answers2

1

There are many problems with the code. Let's take them one at a time.

Load JDBC Driver

The following code will fail:

    Class.forName("org.postgresql.postgresql-42.2.2");

This is not a class, it is a reference to the Java archive that contains the driver class. The PostgreSQL documentation offers the following example for using a JDBC driver:

String url = "jdbc:postgresql://localhost/test";
Properties props = new Properties();
props.setProperty("user","fred");
props.setProperty("password","secret");
props.setProperty("ssl","true");
Connection conn = DriverManager.getConnection(url, props);

-or-

String url = "jdbc:postgresql://localhost/test?user=fred&password=secret&ssl=true";
Connection conn = DriverManager.getConnection(url);

You will have to make sure that org.postgresql.postgresql-42.2.2.jar is on the classpath somewhere so that the driver class can be found.

No Report File Specified

The following code cannot be correct:

    String report = "";

This needs to point to a path for the JRXML file to load. For example:

    String report = "reports/Template.jrxml";

AWT vs. JSF?

The following line suggests a desktop application is being written due to its reference to the AWT ActionEvent:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

However, the report is being exported to a response output stream, which suggests writing to a web browser:

    JasperExportManager.exportReportToPdfStream(jp, response.getOutputStream());

JasperReports does not play well with JSF output streams. Instead, you'll want to use a Servlet with JasperReports and use a simple h:commandLink or h:commandButton to begin generating the report from a web page.

See https://stackoverflow.com/a/35698286/59087 for details.

Response Variable

The response variable is not declared in the code snippet provided. When using a Servlet, the response variable is provided by the method signature:

protected void doGet(HttpServletRequest request,
                     HttpServletResponse response)
              throws ServletException,
                     IOException {
  // ... your code here to handle generating report ...
}

Now the response variable is available for use, but only from within a Servlet context.

DRY Principle

As a minor note, the following three lines violate the DRY principle:

    JasperExportManager.exportReportToPdfStream(jp, response.getOutputStream());
    response.getOutputStream().flush();
    response.getOutputStream().close();

Instead, you could write:

    OutputStream out = response.getOutputStream();
    JasperExportManager.exportReportToPdfStream(jp, out);
    out.close();

There's no need to call out.flush(), generally, because out.close() will ensure the data is flushed.

Recommendation

Take a look online for examples showing how to generate a report using a Servlet.

https://github.com/deadlydirk/jasperreports-example/blob/master/src/main/java/be/example/jasper/servlet/ReportServlet.java

Also, consider getting the report working with a basic application you can run from the command line before trying to do something as technically complex as integration with a Servlet for a web application.

http://www.ewebtutorials.com/generate-pdf-report-using-jasper-report-api-in-java.html

Dave Jarvis
  • 30,436
  • 41
  • 178
  • 315
0

Use JasperExportManager.exportReportToPdfFile() to save PDF to a file:

JasperExportManager.exportReportToPdfFile(jp, "path/to/file.pdf");

If you just want to show it on the screen: Use JasperViewer.viewReport() to display it in a window.

JasperViewer.viewReport(jp, false);
Usagi Miyamoto
  • 6,196
  • 1
  • 19
  • 33