JasperReports学习笔记4-查询数据库生成动态的报表(WEB)
第一种方式:
sql语句中定义查询条件,报表中定义接收参数
第二种方式:
JRXML文件里面不用写SQL语句,而是将结果集查询出来经过JRResultSetDataSource转换
1
2 import java.io.IOException;
3 import java.io.InputStream;
4 import java.sql.Connection;
5 import java.sql.DriverManager;
6 import java.sql.ResultSet;
7 import java.sql.SQLException;
8 import java.sql.Statement;
9 import java.util.HashMap;
10
11 import javax.servlet.ServletException;
12 import javax.servlet.ServletOutputStream;
13 import javax.servlet.http.HttpServlet;
14 import javax.servlet.http.HttpServletRequest;
15 import javax.servlet.http.HttpServletResponse;
16
17 import net.sf.jasperreports.engine.JRException;
18 import net.sf.jasperreports.engine.JRResultSetDataSource;
19 import net.sf.jasperreports.engine.JasperRunManager;
20
21 public class DynamicCreateReportWithServletDateSource extends HttpServlet {
22
23 private static final long serialVersionUID = 1L ;
24
25 public void doGet(HttpServletRequest request, HttpServletResponse response)
26 throws ServletException, IOException {
27 Connection connection = null ;
28 Statement statument = null ;
29 ResultSet resultSet = null ;
30 String sql = " select tb.name as name,tb.age as age,tbs.marks as marks from jaspertb tb join jaspertbs tbs " ;
31 ServletOutputStream servletOutputStream = response.getOutputStream();
32 InputStream is = getServletConfig().getServletContext().getResourceAsStream( " report\\JasperReportSQLResult.jasper " );
33 try {
34 Class.forName( " com.mysql.jdbc.Driver " );
35 connection = DriverManager.getConnection( " jdbc:mysql://localhost:3306/jasperreportdb " , " root " , " root " );
36 statument = connection.createStatement();
37 resultSet = statument.executeQuery(sql);
38 // 这里把ResultSet封装到JRResultSetDataSource对象里面了
39 JasperRunManager.runReportToPdfStream(is,servletOutputStream, new HashMap(), new JRResultSetDataSource(resultSet));
40 response.setContentType( " application/pdf " );
41 servletOutputStream.flush();
42 servletOutputStream.close();
43 connection.close();
44 } catch (ClassNotFoundException e) {
45 e.printStackTrace();
46 } catch (SQLException e) {
47 e.printStackTrace();
48 } catch (JRException e) {
49 e.printStackTrace();
50 }
51 }
52
53 public void doPost(HttpServletRequest request, HttpServletResponse response)
54 throws ServletException, IOException {
55 this .doGet(request, response);
56 }
57
58 }
2 import java.io.IOException;
3 import java.io.InputStream;
4 import java.sql.Connection;
5 import java.sql.DriverManager;
6 import java.sql.ResultSet;
7 import java.sql.SQLException;
8 import java.sql.Statement;
9 import java.util.HashMap;
10
11 import javax.servlet.ServletException;
12 import javax.servlet.ServletOutputStream;
13 import javax.servlet.http.HttpServlet;
14 import javax.servlet.http.HttpServletRequest;
15 import javax.servlet.http.HttpServletResponse;
16
17 import net.sf.jasperreports.engine.JRException;
18 import net.sf.jasperreports.engine.JRResultSetDataSource;
19 import net.sf.jasperreports.engine.JasperRunManager;
20
21 public class DynamicCreateReportWithServletDateSource extends HttpServlet {
22
23 private static final long serialVersionUID = 1L ;
24
25 public void doGet(HttpServletRequest request, HttpServletResponse response)
26 throws ServletException, IOException {
27 Connection connection = null ;
28 Statement statument = null ;
29 ResultSet resultSet = null ;
30 String sql = " select tb.name as name,tb.age as age,tbs.marks as marks from jaspertb tb join jaspertbs tbs " ;
31 ServletOutputStream servletOutputStream = response.getOutputStream();
32 InputStream is = getServletConfig().getServletContext().getResourceAsStream( " report\\JasperReportSQLResult.jasper " );
33 try {
34 Class.forName( " com.mysql.jdbc.Driver " );
35 connection = DriverManager.getConnection( " jdbc:mysql://localhost:3306/jasperreportdb " , " root " , " root " );
36 statument = connection.createStatement();
37 resultSet = statument.executeQuery(sql);
38 // 这里把ResultSet封装到JRResultSetDataSource对象里面了
39 JasperRunManager.runReportToPdfStream(is,servletOutputStream, new HashMap(), new JRResultSetDataSource(resultSet));
40 response.setContentType( " application/pdf " );
41 servletOutputStream.flush();
42 servletOutputStream.close();
43 connection.close();
44 } catch (ClassNotFoundException e) {
45 e.printStackTrace();
46 } catch (SQLException e) {
47 e.printStackTrace();
48 } catch (JRException e) {
49 e.printStackTrace();
50 }
51 }
52
53 public void doPost(HttpServletRequest request, HttpServletResponse response)
54 throws ServletException, IOException {
55 this .doGet(request, response);
56 }
57
58 }
注意:JRXML文件里面数据库的字段都是用$F{marks},而parameter是用$P{xxxx}表示的
,而parameter的值都是传过去的那个HashMap里面设置的