浏览 2992 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2009-06-02
最后修改:2009-06-02
第一种 1.模版 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE jasperReport PUBLIC "//JasperReports//DTD Report Design//EN" "http://jasperreports.sourceforge.net/dtds/jasperreport.dtd"> <jasperReport name="DBReport"> <parameter name="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> <field name="marks" class="java.lang.Integer"></field> <field name="name" class="java.lang.String"></field> <field name="age" class="java.lang.Integer"></field> <pageHeader> <band height="30"> <staticText> <reportElement x="0" y="0" width="69" height="24" /> <text><![CDATA[marks:]]></text> </staticText> <staticText> <reportElement x="140" y="0" width="79" height="24" /> <text><![CDATA[name:]]></text> </staticText> <staticText> <reportElement x="280" y="0" width="69" height="24" /> <text><![CDATA[age:]]></text> </staticText> <staticText> <reportElement x="420" y="0" width="69" height="24" /> <text><![CDATA[you don't see this]]></text> </staticText> </band> </pageHeader> <detail> <band height="30"> <textField> <reportElement x="0" y="0" width="69" height="24" /> <textFieldExpression class="java.lang.Integer"> <![CDATA[$F{marks}]]> </textFieldExpression> </textField> <textField> <reportElement x="140" y="0" width="69" height="24" /> <textFieldExpression class="java.lang.String"> <![CDATA[$F{name}]]> </textFieldExpression> </textField> <textField> <reportElement x="280" y="0" width="69" height="24" /> <textFieldExpression class="java.lang.Integer"> <![CDATA[$F{age}]]> </textFieldExpression> </textField> <staticText> <reportElement x="420" y="0" width="69" height="24" /> <text><![CDATA[If you don't see this, it didn't work]]></text> </staticText> </band> </detail> </jasperReport> 2.把上面的JRXML文件生成JASPER文件,方便在WEB里面的调用(可以用下面的方法先生成好,放到你知道的文件夹下面) JasperCompileManager.compileReportToFile("WebRoot\\report\\JasperReportSQL.xml","WebRoot\\report\\JasperReportSQL.jasper"); 3.写Servlet调用 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; public class DynamicCreateReportWithServlet extends HttpServlet { private static final long serialVersionUID = 1L; public void 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(); } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doGet(request, response); } } 第二种方法 1.这种方法JRXML文件里面不用写SQL <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE jasperReport PUBLIC "//JasperReports//DTD Report Design//EN" "http://jasperreports.sourceforge.net/dtds/jasperreport.dtd"> <jasperReport name="DBReport"> <field name="id" class="java.lang.Integer"></field> <field name="name" class="java.lang.String"></field> <field name="marks" class="java.lang.Integer"></field> <pageHeader> <band height="30"> <staticText> <reportElement x="0" y="0" width="69" height="24" /> <text><![CDATA[id:]]></text> </staticText> <staticText> <reportElement x="140" y="0" width="79" height="24" /> <text><![CDATA[name:]]></text> </staticText> <staticText> <reportElement x="280" y="0" width="69" height="24" /> <text><![CDATA[marks:]]></text> </staticText> <staticText> <reportElement x="420" y="0" width="69" height="24" /> <text><![CDATA[you don't see this]]></text> </staticText> </band> </pageHeader> <detail> <band height="30"> <textField> <reportElement x="0" y="0" width="69" height="24" /> <textFieldExpression class="java.lang.Integer"> <![CDATA[$F{id}]]> </textFieldExpression> </textField> <textField> <reportElement x="140" y="0" width="69" height="24" /> <textFieldExpression class="java.lang.String"> <![CDATA[$F{name}]]> </textFieldExpression> </textField> <textField> <reportElement x="280" y="0" width="69" height="24" /> <textFieldExpression class="java.lang.Integer"> <![CDATA[$F{marks}]]> </textFieldExpression> </textField> <staticText> <reportElement x="420" y="0" width="69" height="24" /> <text><![CDATA[If you don't see this, it didn't work]]></text> </staticText> </band> </detail> </jasperReport> 2.同样是用下面的方法生成JASPER文件 JasperCompileManager.compileReportToFile("WebRoot\\report\\JasperReportSQLResult.xml","WebRoot\\report\\JasperReportSQLResult.jasper"); 3.写出Servlet 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; public class DynamicCreateReportWithServletDateSource extends HttpServlet { private static final long serialVersionUID = 1L; public void 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(); } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doGet(request, response); } } 注意:JRXML文件里面数据库的字段都是用$F{marks},而parameter是用$P{xxxx}表示的 ,而parameter的值都是传过去的那个HashMap里面设置的 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |