分两种方法
第一种
1.模版
<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPE jasperReport PUBLIC "//JasperReports//DTD Report Design//EN"
"http://jasperreports.sourceforge.net/dtds/jasperreport.dtd">
<jasperReportname="DBReport">
<parametername="hp"class="java.lang.Integer"></parameter>
<queryString>
<![CDATA[
select tb.name as name,tb.age as age,tbs.marks as marks from jaspertb tb join jaspertbs tbs on(tb.id = tbs.tbid) where tb.id > $P{hp}
]]>
</queryString>
<fieldname="marks"class="java.lang.Integer"></field>
<fieldname="name"class="java.lang.String"></field>
<fieldname="age"class="java.lang.Integer"></field>
<pageHeader>
<bandheight="30">
<staticText>
<reportElementx="0"y="0"width="69"height="24"/>
<text><![CDATA[marks:]]></text>
</staticText>
<staticText>
<reportElementx="140"y="0"width="79"height="24"/>
<text><![CDATA[name:]]></text>
</staticText>
<staticText>
<reportElementx="280"y="0"width="69"height="24"/>
<text><![CDATA[age:]]></text>
</staticText>
<staticText>
<reportElementx="420"y="0"width="69"height="24"/>
<text><![CDATA[you don't see this]]></text>
</staticText>
</band>
</pageHeader>
<detail>
<bandheight="30">
<textField>
<reportElementx="0"y="0"width="69"height="24"/>
<textFieldExpressionclass="java.lang.Integer">
<![CDATA[$F{marks}]]>
</textFieldExpression>
</textField>
<textField>
<reportElementx="140"y="0"width="69"height="24"/>
<textFieldExpressionclass="java.lang.String">
<![CDATA[$F{name}]]>
</textFieldExpression>
</textField>
<textField>
<reportElementx="280"y="0"width="69"height="24"/>
<textFieldExpressionclass="java.lang.Integer">
<![CDATA[$F{age}]]>
</textFieldExpression>
</textField>
<staticText>
<reportElementx="420"y="0"width="69"height="24"/>
<text><![CDATA[If you don't see this, it didn't work]]></text>
</staticText>
</band>
</detail>
</jasperReport>
JasperCompileManager.compileReportToFile("WebRoot\\report\\JasperReportSQL.xml","WebRoot\\report\\JasperReportSQL.jasper");
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.HashMap;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperRunManager;
publicclass DynamicCreateReportWithServlet extends HttpServlet {
privatestaticfinallong serialVersionUID = 1L;
publicvoid doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Connection connection = null;
//获得输出流
ServletOutputStream servletOutputStream = response.getOutputStream();
//获得JASPER文件的输入流,一定要知道你的文件的地方
InputStream is = getServletConfig().getServletContext().getResourceAsStream("report\\JasperReportSQL.jasper");
//生成Map对象传数据
HashMap hm = new HashMap();
hm.put("hp", new Integer(3));
try {
//连结JDBC
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/jasperreportdb","root", "root");
//生成相于的PDF输入流,这个JasperRunManager对象还有很多种方法的,这只是一种
JasperRunManager.runReportToPdfStream(is,servletOutputStream,hm, connection);
//设置格式
response.setContentType("application/pdf");
servletOutputStream.flush();
servletOutputStream.close();
connection.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} catch (JRException e) {
e.printStackTrace();
}
}
publicvoid doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);
}
}
<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPE jasperReport PUBLIC "//JasperReports//DTD Report Design//EN"
"http://jasperreports.sourceforge.net/dtds/jasperreport.dtd">
<jasperReportname="DBReport">
<fieldname="id"class="java.lang.Integer"></field>
<fieldname="name"class="java.lang.String"></field>
<fieldname="marks"class="java.lang.Integer"></field>
<pageHeader>
<bandheight="30">
<staticText>
<reportElementx="0"y="0"width="69"height="24"/>
<text><![CDATA[id:]]></text>
</staticText>
<staticText>
<reportElementx="140"y="0"width="79"height="24"/>
<text><![CDATA[name:]]></text>
</staticText>
<staticText>
<reportElementx="280"y="0"width="69"height="24"/>
<text><![CDATA[marks:]]></text>
</staticText>
<staticText>
<reportElementx="420"y="0"width="69"height="24"/>
<text><![CDATA[you don't see this]]></text>
</staticText>
</band>
</pageHeader>
<detail>
<bandheight="30">
<textField>
<reportElementx="0"y="0"width="69"height="24"/>
<textFieldExpressionclass="java.lang.Integer">
<![CDATA[$F{id}]]>
</textFieldExpression>
</textField>
<textField>
<reportElementx="140"y="0"width="69"height="24"/>
<textFieldExpressionclass="java.lang.String">
<![CDATA[$F{name}]]>
</textFieldExpression>
</textField>
<textField>
<reportElementx="280"y="0"width="69"height="24"/>
<textFieldExpressionclass="java.lang.Integer">
<![CDATA[$F{marks}]]>
</textFieldExpression>
</textField>
<staticText>
<reportElementx="420"y="0"width="69"height="24"/>
<text><![CDATA[If you don't see this, it didn't work]]></text>
</staticText>
</band>
</detail>
</jasperReport>
JasperCompileManager.compileReportToFile("WebRoot\\report\\JasperReportSQLResult.xml","WebRoot\\report\\JasperReportSQLResult.jasper");
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRResultSetDataSource;
import net.sf.jasperreports.engine.JasperRunManager;
publicclass DynamicCreateReportWithServletDateSource extends HttpServlet {
privatestaticfinallong serialVersionUID = 1L;
publicvoid doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Connection connection = null;
Statement statument = null;
ResultSet resultSet = null;
String sql = "select tb.name as name,tb.age as age,tbs.marks as marks from jaspertb tb join jaspertbs tbs";
ServletOutputStream servletOutputStream = response.getOutputStream();
InputStream is = getServletConfig().getServletContext().getResourceAsStream("report\\JasperReportSQLResult.jasper");
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/jasperreportdb","root", "root");
statument = connection.createStatement();
resultSet = statument.executeQuery(sql);
//这里把ResultSet封装到JRResultSetDataSource对象里面了
JasperRunManager.runReportToPdfStream(is,servletOutputStream,new HashMap(), new JRResultSetDataSource(resultSet));
response.setContentType("application/pdf");
servletOutputStream.flush();
servletOutputStream.close();
connection.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} catch (JRException e) {
e.printStackTrace();
}
}
publicvoid doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);
}
}
,而parameter的值都是传过去的那个HashMap里面设置的