如何在JasperResports中使用结果集作为数据源?

时间:2012-06-05 07:26:23

标签: java jasper-reports resultset

以下是我开发的JasperReport的片段,它使用了报告中嵌入的静态查询。

<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="Report" pageWidth="842" pageHeight="595" orientation="Landscape" columnWidth="802" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" isFloatColumnFooter="true">
     <property name="ireport.zoom" value="1.0"/>
     <property name="ireport.x" value="129"/>
     <property name="ireport.y" value="0"/>
     <parameter name="lp" class="java.lang.Integer"/>
     <parameter name="all_price" class="java.lang.Integer"/>
     <parameter name="all_amount" class="java.lang.Integer"/>
     <parameter name="all_total_price" class="java.lang.Integer"/>
     <queryString>
          <![CDATA[SELECT sanaf_number,sanaf,sales_id,sales.sanaf_id,customer_name,  price, total_price, addad, wahda, required_amount FROM sanaf,sales  where (sales.export='0')and(sanaf.sanaf_id=sales.sanaf_id) limit $P{lp}]]>
     </queryString>
     <field name="sales_id" class="java.lang.Integer"/>
     <field name="sanaf_id" class="java.lang.Integer"/>

现在我需要使用ResultSet而不是静态查询。我用来执行报告的代码位于/

之下
void showResports(){
  try{
    // OriginalExportOriginalExport 
    String reportName ="";

    if(export.equalsIgnoreCase("1")){
      reportName = "OriginalExport.jasper";
    } else if(export.equalsIgnoreCase("0")){
      reportName = "ExportReport4.jasper";
    }

    InputStream in =getClass().getResourceAsStream(reportName);

    if(in==null|| connection==null) return ;

    HashMap parameterMap = new HashMap();
    parameterMap.put("lp", new Integer(selectedNumber));

    if(export.equalsIgnoreCase("0")){
      int all_price=DatabaseManager.countTotalPriceForImport(selectedNumber);
      int all_amount=DatabaseManager.countAmountForImport(selectedNumber);
      int all_total_price=DatabaseManager.countTotalPrice(selectedNumber);

      parameterMap.put("all_price",new Integer(all_price));
      parameterMap.put("all_amount",new Integer(all_amount));
      parameterMap.put("all_total_price",new Integer(all_total_price));
    } else if(export.equalsIgnoreCase("1")){
      int all_requiredAmount=DatabaseManager.countAllRequiredAmount(selectedNumber);
      int all_given_amount=DatabaseManager.countAllGivenAmount(selectedNumber);
      parameterMap.put("all_requiredAmount",new Integer(all_requiredAmount));
      parameterMap.put("all_given_amount",new Integer(all_given_amount));
    }

    JasperPrint jp = JasperFillManager.fillReport(in, parameterMap, connection);
    System.out.println("Object of Jasper Print created");

    JRViewer jv = new JRViewer(jp);
    //Insert viewer to a JFrame to make it showable
    JFrame jf = new JFrame();
    jf.getContentPane().add(jv);
    jf.validate();
    jf.setVisible(true);
    jf.setSize(new Dimension(800,600));
    jf.setLocation(300,100);
    jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);  
  }catch(Exception e){
    e.printStackTrace();
  }
}// showReports

1 个答案:

答案 0 :(得分:3)

如果结果集包含与您当前在报告中嵌入的查询相同的字段,则需要从报告中删除queryString,然后更改:

JasperFillManager.fillReport(in, parameterMap, connection);

JasperFillManager.fillReport(in, parameterMap, new JRResultSetDataSource(resultSet));

其中resultSet是包含数据的结果集。

唯一的复杂因素是结果集是否具有不同的字段名称或完全不同的字段。在这种情况下,您还需要调整报告中的字段名称。