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