jasreport来交互数据-pdf

在上篇文章中我们的数据筛选是由RXML文件决定的,即如果我们要改变查询条件的话,要先修改JRXML文件中的SQL语句,再改JAVA文件。这篇文章中我们的数据筛选完全决定于JAVA文件,JRXML文件只负责定义数据格式:

例子同上:有个数据库名字叫TEST,里面有张表存放的是某作物PLANT的编号:PLANT_UUID,和作物的代码号:PLANT_CODE,作为的英文名字:ENGLISH_NAME

一.用ireport生成的JRXML文件:

<?xml version="1.0" encoding="UTF-8" ?>

<!-- Created with iReport - A designer for JasperReports -->

<!DOCTYPE jasperReport PUBLIC "//JasperReports//DTD Report Design//EN" "http://jasperreports.sourceforge.net/dtds/jasperreport.dtd">

<jasperReport

                  name="JRParameterDemo"

                  columnCount="1">

      <property name="ireport.scriptlethandling" value="2" />

      <property name="ireport.encoding" value="UTF-8" />

      <import value="java.util.*" />

      <import value="net.sf.jasperreports.engine.*" />

      <import value="net.sf.jasperreports.engine.data.*" />

      <parameter name="uuid" isForPrompting="true" class="java.lang.String">

                  <defaultValueExpression ><![CDATA[new java.lang.String("uuid")]]></defaultValueExpression>

      </parameter>

     

      <field name="PLANT_UUID" class="java.lang.String"/>

      <field name="PLANT_CODE" class="java.lang.String"/>

      <field name="ENGLISH_NAME" class="java.lang.String"/>

。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。

                  <detail>

                              <band height="36" isSplitAllowed="true" >

                              <textField><reportElement key="textField"/>

                                          <textFieldExpression   class="java.lang.String"><![CDATA[$F{PLANT_UUID}]]></textFieldExpression>

                                          </textField>

                                          <textField><reportElement key="textField"/>

                                          <textFieldExpression   class="java.lang.String"><![CDATA[$F{PLANT_CODE}]]></textFieldExpression>

                                          </textField>

                                          <textField><reportElement key="textField"/>

                                          <textFieldExpression   class="java.lang.String"><![CDATA[$F{ENGLISH_NAME}]]></textFieldExpression>

                                          </textField>

                              </band>

                  </detail>

                 

      。。。。。。。。。。。。。。。。。。。。。。。。

</jasperReport>

为了节省篇幅,我把比如COLUMNHEADER,COLUMNFOOT等部分的内容去掉了,(文中打。。。的部分),读者可以自己用IREPORT生成JRXML文件时自己定义那些部分。

跟上篇文章唯一的区别在于少了<queryString><![CDATA[select * from plant where plant_uuid like $P{uuid}]]></queryString>,因为我们的SQL语句定义在了JAVA文件中。

二. JAVA文件

public class Demo {

            public static void main(String[] args) {

                        Connection conn = null;

                        ResultSet resultSet = null;

                        Statement statement = null;

                        String url = "jdbc:oracle:thin:@IP:PORT:SCHEMA";

                        String userName = "USERNAME ";

                        String password = "PASSWORD";

                        try {

                                    Class.forName("oracle.jdbc.OracleDriver");

                        } catch (Exception e) {

                                                            e.printStackTrace();

                        }


                        try {

                                    conn = DriverManager.getConnection(url, userName, password);

                        } catch (Exception e) {

                                    e.printStackTrace();

                        }

                        String sql = "select * from plant where plant_uuid = 'PLANT_UUID_0001'";

                        try {

                                    statement = conn.createStatement();

                                    resultSet = statement.executeQuery(sql);

                        } catch (Exception e) {

                                    e.printStackTrace();

                        }


                        try {

                                    JasperReport jasperReport = JasperCompileManager

                                                            .compileReport("report\\plant.jrxml");

                                    JasperPrint jasperPrint = JasperFillManager.fillReport(

                                                            jasperReport, new HashMap(), new JRResultSetDataSource(

                                                                                    resultSet));

                                    JasperExportManager.exportReportToPdfFile(jasperPrint,

                                                            "report\\plant.pdf");


                        } catch (Exception e) {

                                    e.printStackTrace();

                        }

            }

}

这样我们就可以在JAVA文件中根据情况定义SQL语句,JRXML只负责定义数据格式。同样在这种情况下每次都要编译JRXML文件,怎样改进读者可以参考上一篇文章。 

你可能感兴趣的:(java,oracle,sql,jdbc,F#)