前言
前两篇都是介绍了如何查询数据库,但是都是通过报表模版跟数据库进行交互的,也就是说我们的查询语句和参数的定义都写死在模版里面了,所以有时候会不太方便,所以今天就来跟大家介绍下通过Servlet与数据库进行交互,模板只负责显示。
正题
跟之前的一样,我们要生成报表需要以下几个步骤:
1.引入jar包,请看《静态文本报表》 。
2.新建报表模版:
由于我们这次是通过Servlet来与数据库交互,所以我们的报表报表里面没有了查询语句:
<?xmlversion="1.0" encoding="UTF-8"?>
<jasperReportxmlns="http://jasperreports.sourceforge.net/jasperreports"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreportshttp://jasperreports.sourceforge.net/xsd/jasperreport.xsd"
name="JRResultReport">
<fieldname="ID" class="java.lang.Integer"/>
<fieldname="NAME" class="java.lang.String"/>
<fieldname="SEX" class="java.lang.String"/>
<pageHeader>
<bandheight="30">
<staticText>
<reportElementx="0" y="0" width="69" height="24"/>
<textElementverticalAlignment="Bottom"/>
<text>Id</text>
</staticText>
<staticText>
<reportElementx="140" y="0" width="79" height="24"/>
<text>Name</text>
</staticText>
<staticText>
<reportElementx="280" y="0" width="69" height="24"/>
<text>Sex</text>
</staticText>
</band>
</pageHeader>
<detail>
<bandheight="30">
<textField>
<reportElementx="0" y="0" width="69" height="24"/>
<textFieldExpressionclass="java.lang.Integer">
$F{ID}
</textFieldExpression>
</textField>
<textField>
<reportElementx="140" y="0" width="69" height="24"/>
<textFieldExpressionclass="java.lang.String">
$F{NAME}
</textFieldExpression>
</textField>
<textField>
<reportElementx="280" y="0" width="69" height="24"/>
<textFieldExpressionclass="java.lang.String">
$F{SEX}
</textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>
3.编译报表模版,请看《静态文本报表》。
4.编写servlet:
本实例是通过Servlet与数据库进行交互的(JRResultSetDataSource数据源),所以在servlet中建立与数据库的连接和查询语句:
packagecom.dan.servlet;
importjava.io.IOException;
importjava.io.InputStream;
importjava.io.PrintWriter;
importjava.io.StringWriter;
importjava.sql.Connection;
importjava.sql.DriverManager;
importjava.sql.ResultSet;
importjava.sql.Statement;
importjava.util.HashMap;
importjavax.servlet.ServletException;
importjavax.servlet.ServletOutputStream;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
importnet.sf.jasperreports.engine.JRResultSetDataSource;
importnet.sf.jasperreports.engine.JasperRunManager;
/**
* 使用JRResultSetDataSource填充
* @author zdd
*
*/
publicclass DbReportDSServlet extends HttpServlet {
@Override
protectedvoid doGet(HttpServletRequest req, HttpServletResponse resp)
throwsServletException, IOException {
Connectionconnection;
Statementstatement;
ResultSetresultSet;
resp.setContentType("application/pdf");
ServletOutputStreamservletOutputStream = resp.getOutputStream();
InputStreamreportStream =getServletConfig().getServletContext().getResourceAsStream("/WEB-INF/classes/reports/JRResultReport.jasper");
Stringquery = "SELECT T.ID,T.NAME,T.SEX FROM T_USER T";
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
connection=DriverManager.getConnection("jdbc:oracle:thin:@192.168.24.36:1521:testreport","test","test");
statement= connection.createStatement();
resultSet= statement.executeQuery(query);
JRResultSetDataSourceresultSetDataSource = new JRResultSetDataSource(resultSet);
JasperRunManager.runReportToPdfStream(reportStream,servletOutputStream,newHashMap(),resultSetDataSource );
resultSet.close();
statement.close();
connection.close();
servletOutputStream.flush();
servletOutputStream.close();
}catch(Exceptione){
StringWriterstringWriter = new StringWriter();
PrintWriterprintWriter = new PrintWriter(stringWriter);
e.printStackTrace(printWriter);
resp.setContentType("text/plain");
resp.getOutputStream().print(stringWriter.toString());
}
}
}
5.配置web.xml,就是配置servlet。
6.运行项目
看一下我的运行结果:
小结:
其实这么多的实例都很简单,每个实例间的不同点就在于使用的方式不同,其他基本都是类似的。