分两种方法 
第一种 
1.模版 
Xml代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE jasperReport PUBLIC "//JasperReports//DTD Report Design//EN"  
  3. "http://jasperreports.sourceforge.net/dtds/jasperreport.dtd">  
  4. <jasperReport name="DBReport">  
  5.     <parameter name="hp" class="java.lang.Integer"></parameter>   
  6.     <queryString>  
  7.         <![CDATA[ 
  8.             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} 
  9.         ]]>  
  10.     </queryString>  
  11.     <field name="marks" class="java.lang.Integer"></field>  
  12.     <field name="name" class="java.lang.String"></field>  
  13.     <field name="age" class="java.lang.Integer"></field>  
  14.     <pageHeader>  
  15.         <band height="30">  
  16.             <staticText>  
  17.                 <reportElement x="0" y="0" width="69" height="24" />  
  18.                 <text><![CDATA[marks:]]></text>  
  19.             </staticText>  
  20.               
  21.             <staticText>  
  22.                 <reportElement x="140" y="0" width="79" height="24" />  
  23.                 <text><![CDATA[name:]]></text>  
  24.             </staticText>  
  25.               
  26.             <staticText>  
  27.                 <reportElement x="280" y="0" width="69" height="24" />  
  28.                 <text><![CDATA[age:]]></text>  
  29.             </staticText>   
  30.               
  31.             <staticText>  
  32.                 <reportElement x="420" y="0" width="69" height="24" />  
  33.                 <text><![CDATA[you don't see this]]></text>  
  34.             </staticText>       
  35.         </band>  
  36.     </pageHeader>  
  37.       
  38.     <detail>  
  39.         <band height="30">  
  40.             <textField>  
  41.                 <reportElement x="0" y="0" width="69" height="24" />  
  42.                 <textFieldExpression class="java.lang.Integer">  
  43.                     <![CDATA[$F{marks}]]>  
  44.                 </textFieldExpression>  
  45.             </textField>  
  46.             <textField>  
  47.                 <reportElement x="140" y="0" width="69" height="24" />  
  48.                 <textFieldExpression class="java.lang.String">  
  49.                     <![CDATA[$F{name}]]>  
  50.                 </textFieldExpression>  
  51.             </textField>  
  52.             <textField>  
  53.                 <reportElement x="280" y="0" width="69" height="24" />  
  54.                 <textFieldExpression class="java.lang.Integer">  
  55.                     <![CDATA[$F{age}]]>  
  56.                 </textFieldExpression>  
  57.             </textField>    
  58.             <staticText>  
  59.                 <reportElement x="420" y="0" width="69" height="24" />  
  60.                 <text><![CDATA[If you don't see this, it didn't work]]></text>  
  61.             </staticText>  
  62.         </band>  
  63.     </detail>  
  64. </jasperReport>  

2.把上面的JRXML文件生成JASPER文件,方便在WEB里面的调用(可以用下面的方法先生成好,放到你知道的文件夹下面) 
Java代码  收藏代码
  1. JasperCompileManager.compileReportToFile("WebRoot\\report\\JasperReportSQL.xml","WebRoot\\report\\JasperReportSQL.jasper");  

3.写Servlet调用 
Java代码  收藏代码
  1. import java.io.IOException;  
  2. import java.io.InputStream;  
  3. import java.sql.Connection;  
  4. import java.sql.DriverManager;  
  5. import java.sql.SQLException;  
  6. import java.util.HashMap;  
  7.   
  8. import javax.servlet.ServletException;  
  9. import javax.servlet.ServletOutputStream;  
  10. import javax.servlet.http.HttpServlet;  
  11. import javax.servlet.http.HttpServletRequest;  
  12. import javax.servlet.http.HttpServletResponse;  
  13.   
  14. import net.sf.jasperreports.engine.JRException;  
  15. import net.sf.jasperreports.engine.JasperRunManager;  
  16.   
  17. public class DynamicCreateReportWithServlet extends HttpServlet {  
  18.   
  19.     private static final long serialVersionUID = 1L;  
  20.   
  21.     public void doGet(HttpServletRequest request, HttpServletResponse response)  
  22.             throws ServletException, IOException {  
  23.         Connection connection = null;  
  24.         //获得输出流  
  25.         ServletOutputStream servletOutputStream = response.getOutputStream();  
  26.         //获得JASPER文件的输入流,一定要知道你的文件的地方  
  27.         InputStream is = getServletConfig().getServletContext().getResourceAsStream("report\\JasperReportSQL.jasper");  
  28.         //生成Map对象传数据  
  29.         HashMap hm =  new HashMap();  
  30.         hm.put("hp"new Integer(3));  
  31.         try {  
  32.             //连结JDBC  
  33.             Class.forName("com.mysql.jdbc.Driver");  
  34.             connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/jasperreportdb","root""root");  
  35.             //生成相于的PDF输入流,这个JasperRunManager对象还有很多种方法的,这只是一种  
  36.             JasperRunManager.runReportToPdfStream(is,servletOutputStream,hm, connection);  
  37.             //设置格式  
  38.             response.setContentType("application/pdf");  
  39.             servletOutputStream.flush();  
  40.             servletOutputStream.close();  
  41.             connection.close();  
  42.         } catch (ClassNotFoundException e) {  
  43.             e.printStackTrace();  
  44.         } catch (SQLException e) {  
  45.             e.printStackTrace();  
  46.         } catch (JRException e) {  
  47.             e.printStackTrace();  
  48.         }         
  49.     }  
  50.   
  51.     public void doPost(HttpServletRequest request, HttpServletResponse response)  
  52.             throws ServletException, IOException {  
  53.         this.doGet(request, response);  
  54.     }  
  55.   
  56. }  


第二种方法 
1.这种方法JRXML文件里面不用写SQL 
Xml代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE jasperReport PUBLIC "//JasperReports//DTD Report Design//EN"  
  3. "http://jasperreports.sourceforge.net/dtds/jasperreport.dtd">  
  4. <jasperReport name="DBReport">  
  5.     <field name="id" class="java.lang.Integer"></field>  
  6.     <field name="name" class="java.lang.String"></field>  
  7.     <field name="marks" class="java.lang.Integer"></field>  
  8.       
  9.     <pageHeader>  
  10.         <band height="30">  
  11.             <staticText>  
  12.                 <reportElement x="0" y="0" width="69" height="24" />  
  13.                 <text><![CDATA[id:]]></text>  
  14.             </staticText>  
  15.               
  16.             <staticText>  
  17.                 <reportElement x="140" y="0" width="79" height="24" />  
  18.                 <text><![CDATA[name:]]></text>  
  19.             </staticText>  
  20.               
  21.             <staticText>  
  22.                 <reportElement x="280" y="0" width="69" height="24" />  
  23.                 <text><![CDATA[marks:]]></text>  
  24.             </staticText>   
  25.               
  26.             <staticText>  
  27.                 <reportElement x="420" y="0" width="69" height="24" />  
  28.                 <text><![CDATA[you don't see this]]></text>  
  29.             </staticText>       
  30.         </band>  
  31.     </pageHeader>  
  32.       
  33.     <detail>  
  34.         <band height="30">  
  35.             <textField>  
  36.                 <reportElement x="0" y="0" width="69" height="24" />  
  37.                 <textFieldExpression class="java.lang.Integer">  
  38.                     <![CDATA[$F{id}]]>  
  39.                 </textFieldExpression>  
  40.             </textField>  
  41.             <textField>  
  42.                 <reportElement x="140" y="0" width="69" height="24" />  
  43.                 <textFieldExpression class="java.lang.String">  
  44.                     <![CDATA[$F{name}]]>  
  45.                 </textFieldExpression>  
  46.             </textField>  
  47.             <textField>  
  48.                 <reportElement x="280" y="0" width="69" height="24" />  
  49.                 <textFieldExpression class="java.lang.Integer">  
  50.                     <![CDATA[$F{marks}]]>  
  51.                 </textFieldExpression>  
  52.             </textField>    
  53.             <staticText>  
  54.                 <reportElement x="420" y="0" width="69" height="24" />  
  55.                 <text><![CDATA[If you don't see this, it didn't work]]></text>  
  56.             </staticText>  
  57.         </band>  
  58.     </detail>  
  59. </jasperReport>  

2.同样是用下面的方法生成JASPER文件 
Java代码  收藏代码
  1. JasperCompileManager.compileReportToFile("WebRoot\\report\\JasperReportSQLResult.xml","WebRoot\\report\\JasperReportSQLResult.jasper");  

3.写出Servlet 
Java代码  收藏代码
  1. import java.io.IOException;  
  2. import java.io.InputStream;  
  3. import java.sql.Connection;  
  4. import java.sql.DriverManager;  
  5. import java.sql.ResultSet;  
  6. import java.sql.SQLException;  
  7. import java.sql.Statement;  
  8. import java.util.HashMap;  
  9.   
  10. import javax.servlet.ServletException;  
  11. import javax.servlet.ServletOutputStream;  
  12. import javax.servlet.http.HttpServlet;  
  13. import javax.servlet.http.HttpServletRequest;  
  14. import javax.servlet.http.HttpServletResponse;  
  15.   
  16. import net.sf.jasperreports.engine.JRException;  
  17. import net.sf.jasperreports.engine.JRResultSetDataSource;  
  18. import net.sf.jasperreports.engine.JasperRunManager;  
  19.   
  20. public class DynamicCreateReportWithServletDateSource extends HttpServlet {  
  21.   
  22.     private static final long serialVersionUID = 1L;  
  23.   
  24.     public void doGet(HttpServletRequest request, HttpServletResponse response)  
  25.             throws ServletException, IOException {  
  26.         Connection connection = null;  
  27.         Statement statument = null;  
  28.         ResultSet resultSet = null;  
  29.         String sql = "select tb.name as name,tb.age as age,tbs.marks as marks from jaspertb tb join jaspertbs tbs";  
  30.         ServletOutputStream servletOutputStream = response.getOutputStream();  
  31.         InputStream is = getServletConfig().getServletContext().getResourceAsStream("report\\JasperReportSQLResult.jasper");  
  32.         try {  
  33.             Class.forName("com.mysql.jdbc.Driver");  
  34.             connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/jasperreportdb","root""root");  
  35.             statument = connection.createStatement();  
  36.             resultSet = statument.executeQuery(sql);  
  37.             //这里把ResultSet封装到JRResultSetDataSource对象里面了  
  38.             JasperRunManager.runReportToPdfStream(is,servletOutputStream,new HashMap(), new JRResultSetDataSource(resultSet));  
  39.             response.setContentType("application/pdf");  
  40.             servletOutputStream.flush();  
  41.             servletOutputStream.close();  
  42.             connection.close();  
  43.         } catch (ClassNotFoundException e) {  
  44.             e.printStackTrace();  
  45.         } catch (SQLException e) {  
  46.             e.printStackTrace();  
  47.         } catch (JRException e) {  
  48.             e.printStackTrace();  
  49.         }  
  50.     }  
  51.   
  52.     public void doPost(HttpServletRequest request, HttpServletResponse response)  
  53.             throws ServletException, IOException {  
  54.         this.doGet(request, response);  
  55.     }  
  56.   
  57. }  


注意:JRXML文件里面数据库的字段都是用$F{marks},而parameter是用$P{xxxx}表示的 

,而parameter的值都是传过去的那个HashMap里面设置的


转载自:http://langhua9527.iteye.com/blog/400435

posted on 2012-07-17 16:28  星^_^風  阅读(1213)  评论(0编辑  收藏  举报