毕业论文范文网-论文范文
电气工程 会计论文 金融论文 国际贸易 财务管理 人力资源 学前教育 德语论文 工程管理 文化产业 工商管理 会计专业 行政管理 广告学
机械设计 汉语文学 英语论文 物流论文 电子商务 法律论文 工商管理 旅游管理 市场营销 药学论文 播音主持 人力资源 金融论文 保险学
制药工程 生物工程 包装工程 模具设计 测控专业 工业工程 教育管理 行政管理 计算机论 电子信息 市场营销 法学论文 财务管理 投资学
体育教育 小学教育 印刷工程 土木工程 书法论文 护理论文 心理学论 信息管理 公共事业 给水排水 新闻专业 摄影专业 广电编导 经济学
  • 范文首页 |
  • 毕业论文 |
  • 论文范文 |
  • 计算机论文 |
  • 外文翻译 |
  • 工作总结 |
  • 工作计划 |
  • 现成论文 |
  • 论文下载 |
  • 教学设计 |
  • 免费论文 |
  • 原创论文 |
搜索 高级搜索

原创毕业论文

当前位置:毕业论文范文网-论文范文 -> 免费论文 -> 计算机论文

B/S系统下数据访问及报表生成技术

作者: 浏览:20次
免费专业论文范文
免费专业论文
政治工作论文
计算机论文
营销专业论文
工程管理论文范文
医药医学论文范文
法律论文范文
生物专业论文
物理教学论文范文
人力资源论文范文
化学教学论文范文
电子专业论文范文
历史专业论文
电气工程论文
社会学专业论文
英语专业论文
行政管理论文范文
语文专业论文
电子商务论文范文
焊工钳工技师论文
社科文学论文
教育论文范文
数学论文范文
物流论文范文
建筑专业论文
食品专业论文
财务管理论文范文
工商管理论文范文
会计专业论文范文
专业论文格式
化工材料专业论文
英语教学专业论文
电子通信论文范文
旅游管理论文范文
环境科学专业论文
经济论文
人力资源论文范文
营销专业论文范文
财务管理论文范文
物流论文范文
财务会计论文范文
数学教育论文范文
数学与应用数学论文
电子商务论文范文
法律专业论文范文
工商管理论文范文
汉语言文学论文
计算机专业论文
教育管理论文范文
现代教育技术论文
小学教育论文范文
机械模具专业论文
报告,总结,申请书
心理学论文范文
学前教育论文范文

收费计算机专业论文范文
收费计算机专业论文
Delphi
ASP
VB
JSP
ASP.NET
VB.NET
java
VC
pb
VS
dreamweaver
c#.net
vf
VC++
计算机论文
毕业论文范文题目:B/S系统下数据访问及报表生成技术,论文范文关键词:B/S系统下数据访问及报表生成技术
B/S系统下数据访问及报表生成技术毕业论文范文介绍开始:

  目  录
摘  要 2
ABSTRACT 3
第一章  绪 论 4
第二章  相关技术背景 4
2.1  MVC设计模式和Struts框架的原理 5
2.2  JDBC数据库的原理 5
2.3  iReport的工作原理 6
        2.4  开发环境 6
第三章  数据库的操作和报表的生成 7
3.1  数据库的操作 7
3.1.1  数据库的基本操作 7
3.1.2  对结果集的处理 8
3.2  报表的生成 8
 3.2.1  报表的设计 8
    3.2.2  报表的页面显示 8
第四章 具体模块的设计实现 8
 4.1  数据库的操作 9
 4.1.1  对数据库的动态查询 14
 4.1.2  对结果集的处理 16
4.1.3  对数据库的更新操作 19
   4.2  报表展现的具体实现 20
第五章  结束语…………………………………………….. 22
第六章  参考文献…………………………………………...22
第七章  附录…………………………………………………23

摘  要
 本文是计算机系本科毕业生毕业设计论文。其项目背景是实现校内教师管理的信息化。可以方便用户进行信息录入,检索,修改,删除等功能。为了提高开发的效率,我们选择采用了MVC的开发模式进行B/S的WEB应用系统的开发。
 本文主要介绍了Struts框架的原理。在Struts框架下的Model开发。介绍了如何使用JDBC进行数据库的联接。介绍了如何使用JDBC所提供的接口实现各种的SQL语句。并介绍了如何使用可视化报表工具设计电子报表,如何能够在浏览器中显示设计好的报表文件。
 关键字:  报表           后台操作

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 ABSTRACT

 This thesis is a graduate of Department of Computer Science. This project is a Management Information System of teachers’information. It will help the administrator store , search , alter and delete the teacher’s information. We chose the Model-Contorl-View design pattern to devolep this  WEB application.
 This thesis is about the Model devolep of the Strust Framework. This thesis introduce how to connect the Relation Database by using JDBC and how to execute the SQL by the function of JDBC. At the end , I introduce how to use IDE report’s tool to design the report and how to generate the report in the browers.
 
 
 
 Key words : report       background code

 

 

 

 

 

 

 

 绪 论
 在当今的信息社会之中,Internet已经得到广泛的普及。各个单位的各个部门也都通过局域网相互联系到了一起。故我们将教师管理系统设计为B/S的结构,可以方便的在校内轻松使用。如发生故障,可以在服务器端作相应的调试即可,不需要让维护人员多次往返。
 在使用B/S结构时有两种设计模式,我们选择了了符合模式二的Struts框架。我是主要负责后台的数据库存储和相应数据读取,需要方便前台展现时读取数据。在负责报表生成时,根据用户的要求需要产生动态电子报表。用户只是在客户端发送一个请求告诉服务器端所需要的报表,这是我需要从数据库中将相应的数据读取出来,填充至我已经设计好的报表模版之中。最后需要将这个报表在客户端的浏览器上做出展现。
 本课题是基于Java语言来设计与实现的,因此先决条件是有一个Java编译器和虚拟机,用一个JDBC驱动程序访问一个关系型的数据库,一个报表的编辑开发工具以及独立的服务器。本课题是在Windows环境下使用的JBuilderX,SQL SERVER 2000, Dreamweaver MX ,iReport 0.4.1,IE浏览器和Acrobat 5.0等工具进行开发的。
 这个课题是由杨少雄老师指导开发,由杨芃、钱立娟、韩佳呈和我共同开发完成的。
 这篇文章中,将分部分来介绍Web开发模式二(MVC)的基本理论、具体的数据库交互模块和报表生成功能模块的作用和实现方法。课题的难点在于数据库数据的处理和报表的页面展现。论文组织如下:
 第二章介绍了本课题相关的一些技术背景。如MVC设计模式、Struts框架、JDBC数据库的连接和报表生成的原理。
 第三章介绍了本课题中数据库交互模块的设计思想。对数据库操作,如模糊查询如何实现和如何将查询结果封装后返回给前台页面的过程。以及报表如何在浏览器中展现和打印的实现。这是本文的重点。
 第四章,这是可以看到数据库操作中的典型代码,对其中的变量以及接口做出了介绍。并且对报表文件如何展现的代码做一个介绍。这同样是本文的重点。
第二章  相关技术背景
2.1  MVC设计模式和Struts框架的原理
 Struts框架是符合MVC设计模式的WEB应用程序开发框架。MVC即Model-View-Controller这三个部分。其中Model部分是对数据进行处理和输出数据。View部分是展现部分用以展现Model部分提供的数据。Controller部分是来控制输入和输出的。由页面部分展现给使用者,由用户产生一个请求发送给服务器端,由位于服务器端的Controller部分根据设计,返回给客户端一个页面或是交给后台的Model部分处理数据将处理过后的数据传送给页面展现。Model部分与数据库进行交互,View部分可使用Jsp标记和Struts自有标记,而Conreoller部分是负责业务逻辑控制程序流。
JDBC数据库的原理
 JDBC API以一个统一的方式访问各种数据库。与ODBC类似,JDBC将开者和私有数据库之间的问题隔离开来。由于它建立在Java上,因此JDBC可以提供平台无关的数据库访问。
 JDBC定义了4种不同的驱动,具体来说,包括有:
类型1:JDBC-ODBC桥
 开发者可以使用JDBC来访问一个ODBC数据源。它需要在客户机器上安装有一个ODBC驱动。使用这一类的驱动器,你就会失去JDBC平台无关的好处,进行数据库操作的时候运行效率也不是很高。
类型2:JDBC-native驱动桥      JDBC-native驱动桥提供了一个建筑在本地数据库驱动上的JDBC接口--没有使用ODBC。JDBC驱动将标准的JDBC调用转变为对数据库API的本地调用。使用类型2的驱动也会失去JDBC平台无关性的好处,并且需要安装客户端的本地代码。类型3:JDBC-network桥      JDBC -network桥不需要客户端的数据库驱动。它们使用网络-服务器中层来访问一个数据库。这会引出诸如负载均衡、连接池等技术,数据缓冲也是可能的。由于类型3的驱动通常可带来相对小的下载时间,它是平台无关的,并且不需要客户端的安装和管理,因此很适合用作Internet的应用。类型4:纯Java驱动      类型4使用纯Java数据库驱动来提供直接的数据库访问。可以提供高效的数据库连接。并有良好的平台无关性。
 我们使用的就是类型4的JDBC驱动,这些是位于服务器端的程序所作的操作,所以对客户端并没有实际的影响。但是他有良好的运行效率和稳定性,给我们的项目有的更好的可用性。
2.3  iReport的工作原理

 iReport是jasperreport这个报表生成包的可视化报表编辑环境。上图就是jasperreport这个报表生成工具的原理图。通过iReport将设计的报表模版在编译时保存为XML格式,在需要生成时可以将数据库中的数据装载入报表模版中,可以生成PDF、HTML等格式。
2.4   开发环境
 使用的Windows操作系统,使用Struts的开发包是jakarta-struts-1.1-b3-lib,使用的JDK版本是JDK1.4,使用的JasperReports的类库是jasperreports-0.6.4.jar。使用的可视化编辑环境是JBuilderX,SQL SERVER 2000, Dreamweaver MX ,iReport 0.4.1;服务器是TOMCAT 4.0。

第三章  数据库的操作和报表的生成
3.1 数据库的操作
   3.1.1数据库的基本操作
 我们是用的是JDBC数据库,JDBC是支持基本SQL数据库功能的一系列抽象 的接口,最重要的接口包括:Java.sql.Driver Manager(处理驱动的调入并且对产生新的数据库连接提供支持)、Java.sql.Connection(代表对特定数据库的 连接)、Java.sql.Statement(代表一个特定的容器,对一个特定的数据库执行SQL语句)、Java.sql.ResultSet(控制对一个特定语句的行数据的存取)。因为是使用Struts框架进行开发,所以数据库连接要比传统的方式显得更加简便,只需要在Struts-config中将数据库驱动的名称,数据库所在位置,用户名和密码这些基本信息配置好之后,便可以在整个程序中方便的调用。
 在进行数据库的查询、新增、修改和删除这些操作时,就需要用到Statement、PreparedStatement、CallableStatement这三个接口。其中Statement是处理静态的SQL操作,PreparedStatement是预处理SQL语句,CallableStatement是用来调用存储过程。我们根据实际应用中的情况来选择这三种方法。Statement的只能用来做静态的的操作,但是由于它的语句比较简单,所以我们在一些需要返回表中全部数据时,我们仍然大量使用这个方法。而在我们需要进行一些比较简单的由用户输入查询条件的时候,我们是使用PreparedStatement的。而当数据库操作使用单条语句或是由于表操作比较复杂的时候,需要在数据库中建立存储过程,我们就可以使用CallableStatement来进行操作。
 由于使用的数据库驱动的原因,所以MS-SQL数据库中自动提交被改成了手动提交。在对数据库中数据进行更新的时候,我们必须选择比较麻烦的批处理方式进行操作。
对结果集的处理
 当我们从数据库里将数据取出的时候,是以相当于数据库中二维表的形式给出的ResultSet。而在前台要取这些数据的时候就会产生比较大的困难。由于ResultSet的方法所限,无法很方便的找到所需要指定的对象。于是我们需要对取出的ResultSet需要做出一个处理以方便前台的调用。我们开始时,是想到了使用HashMap这个对象,因为它可以通过”Key”找到其中的”value”。于是我们将数据的字段名作为结果的”Key”。但是由于有的得结果集是以二维表的形式传出,其中会有多条记录的字段名是一样,这样仍然无法解决问题。于是我们使用了有着链表特性的ArrayList,将HashMap存入ArrayList之中。这样可以方便的将HashMap先取出,之后就可以方便的取得数据了。
 3.2  报表的生成
 3.2.1  报表的设计
 正如上边所提到的那样,我是使用了iReport这个可视化编辑工具来进行开发的。而iReport是一个Jasperreports的可视化编辑环境,它将你的报表模版保存为XML文件,在你将其中的数据源,变量等数据信息完全设计之后,编译成为.jasper文件。这个就是可以供程序调用的报表文件。根据客户的需求分析,了解到客户所需要的是比较复杂的动态报表。从用户提供了几张报表的例子分析发现了用户在一张报表里所需要的信息量非常大。如其中最为复杂的教师信息报表几乎涉及到了数据库中所有表,而且用户还希望将来可以自由定制报表。根据以上情况,再根据iReport的自身情况,如一张报表只可以使用一条SQL语句,但是可以内置子报表。这样我们就在主报表中嵌入子报表,以解决不可能以一条查询语句返回出所有的信息的问题,并且可以使部分实现动态定制报表称为可能。
报表的页面显示
 由于本系统是使用B/S结构的,那么用户仅仅只是通过浏览器来对我们的程序作展现的。那么如何能够使用户在客户端可以方便快捷的可以看到我们的报表文件,并且用户还必须能够在客户端将页面上所见的报表打印出来。这样由于是使用B/S结构就考虑到在部署应用程序时,不需要再在客户端进行比较麻烦的应用程序安装。
 出于以上考虑,我们选择了jasperreport可以输出的PDF文件。之所以选择了他,是因为根据现在的实际情况,PDF格式的文件在安全性和文件的大小程度都已经得到认可,使用的人也非常之多。而且一旦安装了Adobe Reader,它是自动内嵌入网页浏览器之中的,这样可以实现在浏览器中的浏览。而借助于Reader的强大他支持在浏览器之中直接将显示的PDF文件打印。
 由于PDF是在服务器端产生的,客户端需要将其下载之后才能显示。但是这种做法是被一部分的客户端的防火墙认为是有极大安全隐患而会被阻止下载,或是需要用户每次都去设置防火墙来允许文件通过。于是我们根据jasperreport所提供的接口将PDF文件转换作为字节流做显示,在几个主流的防火墙上测试都不会有以上的问题出现。
 在设计初期根据最后是将PDF以字节流方式显示的情况,必须是使用servlet中的方法才可以显示的。所以我就直接使用了servlet作为报表的输出页面。经过一系列独立测试时都可以顺利的显示。但是在进行于系统整合的过程中才发现,servlet在Struts中的URL路径,始终无法正确的被找到。这样打印页面就无法使用Struts提供的方法调用,为了保证程序的统一性。我就重新使用JSP作为报表的页面展现。这时就可以很好的与整个系统整合。
 
第四章 具体模块的设计实现
4.1  数据库的操作
 使用JDBC提供的方法之前需要取得数据库的联接。在使用完毕之后需要释放数据库的联接。在使用Struts时,我们在struts-config中对数据库做出配置,代码如下:
 <data-sources>
     <data-source key="dataSource" type="org.apache.commons.dbcp.BasicDataSource">
       <set-property property="driverClassName" value="net.sourceforge.jtds.jdbc.Driver" />
       <set-property property="url" value="jdbc:jtds:sqlserver://localhost:1433;DatabaseName=TeacherInfo" />
       <set-property property="maxActive" value="10" />
       <set-property property="maxWait" value="5000" />
       <set-property property="defaultAutoCommit" value="false" />
       <set-property property="defaultReadOnly" value="false" />
       <set-property property="validationQuery" value="SELECT COUNT(*) FROM DBA" />
       <set-property property="username" value="sa" />
       <set-property property="password" value="sju" />
     </data-source>
   </data-sources>
其中,"dataSource"是在整个程序中用来获取数据源的键,"org.apache.commons.dbcp.BasicDataSource"定义了数据源的类型。"driverClassName"配置了数据库驱动的类型,我们使用的是一个开源的四型驱动"net.sourceforge.jtds.jdbc.Driver"。"url"是定义了数据库所在的位置。之后是数据库的一些基本配置如是否是自动提交,数据库登陆名,密码等。
 在本程序中对于Struts提供的Action进行了继承:
 public class BaseAction extends Action {
 
   /**
    * get Connection from the DataSource.
    *
    * @param httpServletRequest HttpServletRequest
    * @param DataSource_Key String
    * @throws SQLException
    * @return Connection
    */
   protected Connection getConnection(HttpServletRequest httpServletRequest,String DataSource_Key) throws SQLException {
     Connection cn = null;
     DataSource ds;
     ds = getDataSource(httpServletRequest,DataSource_Key);
     cn = ds.getConnection();
 
     return cn;
   }
 … … …
 }
 其中getDataSource(...);是Struts中Action提供的数据源获取的函数。在我们的应用程序中,我们在需要使用数据库操作的Action类中我们就使用这个被重构的类。
 在Action中我们是这样取得数据库的连接的:
 … … …
 Connection cn = null;
 … … …
     try {
       cn = this.getConnection(httpServletRequest, "dataSource");
       return actionMapping.getInputForward();
     }
     catch (SQLException ex) {
       actionerrors.add(ActionErrors.GLOBAL_ERROR,
 new ActionError("edu.sju.teacherinfo.Error"));
       this.saveErrors(httpServletRequest,actionerrors);
      
 session.setAttribute(Constants.INPUT_PATH,
 actionMapping.getInputForward());
       throw new java.lang.Exception();
     }
     finally {
       if (cn != null) {
         try {
           cn.close();
         }
         catch (Exception ex1) {
           actionerrors.add(ActionErrors.GLOBAL_ERROR,new ActionError("edu.sju.teacherinfo.Error"));
           session.setAttribute(Constants.INPUT_PATH,
 actionMapping.getInputForward());
           this.saveErrors(httpServletRequest,actionerrors);
           throw new java.lang.Exception();
         }
       }
     }
 在声明一个Connection对象之后,我们使用getConnection(…)这个方法来取得数据连接。我们一般会将Connection对象作为参数传到后台的Model部分,在处理完之后。我们需要将Connection对象关闭。这是一个完整的数据库连接过程。
 如上文所提到JDBC提供三种接口来实现对数据的新增,删除和查询。根据MVC框架的理论,我们了解需要使用Action来对后台代码进行调用。本系统主要要实现对校内教师的信息管理,登录系统的管理,报表的输出和考虑到院系、课程的变化,加入了对课程的管理。这四个部分都是需要数据库的操作。于是我们在这四个部分之外,我们建立了一个更加底层的进行直接与数据库交互的模块我们成其为sqlManager。于是整个数据库操作过程是View->(ActionForm)->Action->User(\Teacher\Subject\Print)Model
 ->sqlManager。
 
public class sqlManager {
   /**
    *the Statement sql string
    */
   private String _stmtSql = null;
 
   /**
    * the preparedStatement sql string
    */
   private String _pstmtSql = null;
 
   /**
    * the
    */
   private String _cstmtSql = null;
 
   /**
    * the all prepareStatement sql's parametres
    */
   private ArrayList _dataRowSet = null;
 
   /**
    * the connection which connect to database
    */
   private Connection _cn = null;
 … … …
 }
 这些是其中的成员变量,_stmtSql、_pstmtSql和_cstmtSql是用来传入查询语句的变量。_dataRowSet是用来传入作查询时的参数。_cn是用来保存数据库连接的变量。

4.1.1  对数据库的基本查询
 在本系统中大量需要使用的是动态查询,所以我们需要使用JDBC提供的三种接口来实现。Statement方法虽然是只能做一个静态的SQL的操作,但是它的方法实现比较方便。于是我们在做数据库操作,传入的参数比较简单时,我们使用以下方法来实现动态查询:
 public class UserModel {
 … … … 
 /**
    *get legality user's popedom
    *
    *@return String userPopedom
    */
   public String getUserDroit(Connection cn , String userID) throws SQLException {
     sqlManager sqlMr = new sqlManager();
     sqlMr.setCn(cn);
     sqlMr.setStmtSql("select droit from DBA where userID ='" + userID+"'");
     return sqlMr.getStatement_String();
   }
 … … …
 }
 我们在进行传入插叙语句的时候加入了一个userID的变量,这样就可以实现一个简单的动态查询。首先我们在UserModel类中声明了一个sqlManager类的类对象:sqlManager sqlMr = new sqlManager();
 之后我们就需要调用sqlManager所提供的方法。先要传入数据库的连接对象,通过这个方法sqlMr.setCn(cn);。由于是使用得是静态方法是先所以需要传入一个完整的SQL语句,sqlMr.setStmtSql("select droit from DBA where userID ='" + userID+"'");。setStmtSql(String sqlQuery)这是用来将sql语句传入后台的接口。
 但是当传入的参数数量比较多的时候,我们就不能在使用这种拼接查询语句来实现动态的查询。那我们就需要使用比较复杂的preparedStatement来实现。
public class UserModel {
 public UserModel() {
   }
 
   /**
    * Return islegal user
    *
    * @return Boolean islegal user
    */
   public boolean islegaluser(Connection cn,ArrayList userArrayList) throws
 SQLException {
     Hashtable ht = new Hashtable();
     boolean isUser = false;
     sqlManager sqlmanager = new sqlManager();
     String SQLQuery = "select * from DBA where userID=? and psw=?";
 
     sqlmanager.setArrayListData(userArrayList);
     sqlmanager.setPstmtSql(SQLQuery);
     sqlmanager.setCn(cn);
     userArrayList = sqlmanager.getPreparedStatement_ArrayList();
     if (userArrayList.size() > 0)
       isUser = true;
     return isUser;
   }
 … … … }
 与调用Statement方法相同的,我们需要先声明一个sqlManager类的类对象,传入数据库连接对象,查询语句和参数。
 String SQLQuery = "select * from DBA where userID=? and psw=?";
 这是一个preparedStatement可以处理的查询语句。"?"就是用来传入参数的位置。
 sqlmanager.setArrayListData(userArrayList);
 这是将参数userArrayList传入后台的语句。这样就可以实现一个完整的查询。但是由于需要将参数传到"?",这是需要指定类型的传入。但是这样无疑是非常麻烦得,于是在可以完成这个功能的基础之上,我将参数传入作了一个接口。
 protected void setPreparedStatement(ArrayList al,PreparedStatement ps) throws
       SQLException {
     for(int i = 0; i<al.size() ; i++)
       ps.setObject( i+1, al.get(i));
   }
这样可以根据传入的参数的个数自动的将参数传入相应得位置上去。一般的使用这个方法就可以实现动态查询。

4.1.2  对结果集的处理
 在我们实现数据库的查询之后,JDBC是返回了一个ResultSet这个类对象。其中存储的对象的结构与关系型数据库中数据存储的方式是一样的,同样是一个二维表。这样在数据返回到前台之后对于前台的取得指定数据不是很方便。于是我们需要对数据做出一定的处理。我们是将数据放在ArrayList和Hashmap这两个类对象之中。我们先来看个使用ArrayList的方法:
   /**
    * get CallableStatement sql result
    *
    * @throws SQLException
    * @return ArrayList
    */
   public ArrayList getCallableStatement_ArrayList() throws SQLException {
 
     ArrayList al = new ArrayList();
     ResultSetMetaData rsmd = null;
     Connection cn = this._cn;
     CallableStatement cstmt = cn.prepareCall(this._cstmtSql);
 
     //set CallableStatement Sql
     this.setCallableStatement(this._dataRowSet,cstmt);
     this._dataRowSet.clear();
     ResultSet rs = cstmt.executeQuery();
     rsmd =rs.getMetaData();
 
     while(rs.next()){
       Hashtable ht = new Hashtable();
       for(int i = 1; i <= rsmd.getColumnCount(); i++ )
         if (rs.getObject(i) == null) {
           ht.put(rsmd.getColumnName(i),"");
         }
         else {
           ht.put(rsmd.getColumnName(i),rs.getObject(i));
         }
       al.add(ht);
     }
     cstmt.close();
     return al;
  }
我们使用了ArrayList来存放HashTable这个类的实例化对象。
ResultSet rs = cstmt.executeQuery();将数据库查询中的结果放在这个ResultSet的类对象之中。
ResultSetMetaData rsmd =rs.getMetaData();这个对象之中存放的是数据库二维表的一些信息。
ht.put(rsmd.getColumnName(i),rs.getObject(i));在这个操作里,我们向一个HashTable对象传入数据。它所需的KEY是通过ResultSetMetaData的对象取得了二维表的列名。它的VAULE是从rs.getObject(i)取得的一个java类型对象。通过FOR-LOOP,我们就将一条记录存入了一个HashTable之中。最后将这一条记录存放到ArrayList中。经过While-LOOP之后,就可以将一整张二维表存入一个ArrayList对象之中。
/**
   *get PreparedStatement Results
   *
   * @throws SQLException
   * @return HashMap
   */
  public HashMap getPreparedStatement_HashMap() throws SQLException {
    PreparedStatement pstmt = _cn.prepareStatement(this._pstmtSql);
    ResultSetMetaData rsmd = null;
    HashMap hm = new HashMap();

    //set setPreparedStatement
    this.setPreparedStatement(this._dataRowSet , pstmt);

    this._dataRowSet.clear();
    ResultSet rs = pstmt.executeQuery();
    rsmd =rs.getMetaData();

    int j = 1;
    while(rs.next()){
      ArrayList al = new ArrayList();
      for(int i = 1; i <= rsmd.getColumnCount(); i++ ) {
        if (rs.getObject(i) == null) {
          al.add("");
        }
        else {
          al.add(rs.getObject(i));
        }
      }
      hm.put(String.valueOf(j),al);
      j++;
    }
    pstmt.close();
    return hm;
  }


对数据库的更新操作
 对数据库更新操作包括新增,修改和删除。由于在进行这些操作时有可能因为一些不可预知的错误而导致脏数据等的产生。所以数据库会使用事务回滚,使数据库回到发生错误之前的数据,来保证数据的正确性。但是我们使用的MS-SQL是使用自动提交的。所以为了防止发生不能回滚,我们是将数据库改成了手动提交。
 <set-property property="defaultAutoCommit" value="false" />
 我们在struts-config中这样定义事务提交,不是使用自动提交。
 所以我们在进行更新操作时需要进行事务提交:
 
 /**
    * execute statement update,insert,delete
    *
    * @param _stmtSql String
    * @param _cn Connection
    *
    * @throws SQLException
    */
   public void executeBatchStatement() throws SQLException {
 
       Statement stmt = _cn.createStatement();
 
       stmt.addBatch(this._stmtSql);
       stmt.executeBatch();
 
       _cn.commit();
   }
 这是使用静态的方法,在声明了一个Statement的类对象之后。使用
 stmt.addBatch(this._stmtSql);这个方法将需要进行增删改的语句this._stmtSql加到这个批里。在所有的语句都加到这个批里之后,用stmt.executeBatch()这个方法进行执行。之后使用_cn.commit()进行提交。这样如果执行时发生错误的话,那么数据库会回滚从而保证数据库中的数据的正确性。
 
4.2  报表展现的具体实现
 我们使用的是jasperreport来生成一个报表的模版,这个模版是以XML的格式存放的。我们可以使用jasperreport这个包中提供的报表对象来生成这个报表的对象,使用代码来实现这个模版;或者可以使用XML来直接来设计这个报表模版;而我们是使用ireport这个集成开发环境来设计报表模版。在模版设计完成之后,由于是动态生成的报表,那么是需要从数据库中取得数据的。但是由于我们需要生成的报表是一个非常复杂的对象,它需要从多张数据库表中取得数据,而一个jasperreport对象只能支持一条查询语句。即使使用了存储过程,但是无疑会是一个非常复杂的过程。我们使用的子报表来解决。将来自同一张数据库表的数据放在一张表中,这样就可以使用比较简单的语句来是实现一个复杂的显示。由于是需要向表中传入数据,所以会在报表中放入许多的变量。这样将这些变量和子报表放入之后,我们就可以将一个报表进行编译。这样可以产生一个 *.jasperreport对象,这个对象是最后可以在程序中使用的。我们通过一个例子来看如何在页面中使用这个对象。
 String teacherID = pageContext.getSession().getAttribute("teacherID").toString();
 String master = application.getRealPath("masterreport.jasper");
 File one = new File(application.getRealPath("sub_baseInfo.jasper"));
 File two = new File(application.getRealPath("sub_letter.jasper"));
 File three = new File(application.getRealPath("sub_clan.jasper"));
 File four = new File(application.getRealPath("sub_degree.jasper"));
 File five = new File(application.getRealPath("sub_phone.jasper"));
 File six = new File(application.getRealPath("sub_studyHistroy.jasper"));
 File seven = new File(application.getRealPath("sub_uphistroy.jasper"));
  File masterFile = new File(master);
 Map parameters = new HashMap();
 try {
 JasperReport oneR = (JasperReport) JRLoader.loadObject(one);
 JasperReport twoR = (JasperReport) JRLoader.loadObject(two);
 JasperReport threeR = (JasperReport) JRLoader.loadObject(three);
  JasperReport fourR = (JasperReport) JRLoader.loadObject(four);
 JasperReport fiveR = (JasperReport) JRLoader.loadObject(five);
 JasperReport sixR = (JasperReport) JRLoader.loadObject(six);
JasperReport sevenR = (JasperReport) JRLoader.loadObject(seven); parameters.put("sub_one", oneR); parameters.put("sub_two", twoR);
 parameters.put("sub_three", threeR);
 parameters.put("sub_four", fourR);
 parameters.put("sub_five", fiveR);
  parameters.put("sub_six", sixR);
 parameters.put("sub_seven",sevenR);
 parameters.put("teacherID",teacherID);
 } catch (JRException ex3) { String ioo = "sorry!"; }
  Connection conn=null;
  String driverName = "net.sourceforge.jtds.jdbc.Driver";
 try { Class.forName(driverName); }
 catch (ClassNotFoundException ex1) { }
 try
 { conn = DriverManager.getConnection("jdbc:jtds:sqlserver://192.168.16.242:1433;DatabaseName=TeacherInfo","sa","sju"); }
 catch (SQLException ex2) { String so ="?"; }
  byte[] bytes = null;
  try { bytes = JasperRunManager.runReportToPdf(
 masterFile.getPath(),
  parameters,
 conn ); }
 catch (JRException ex) { String err = ""; }
  response.setContentType("application/pdf");
 response.setContentLength(bytes.length);
 ServletOutputStream ouputStream = response.getOutputStream();
 ouputStream.write(bytes, 0, bytes.length);
 ouputStream.flush();
 ouputStream.close();
 
 这是对教师的所有信息作显示的代码,这是本程序中最为复杂的一张报表。由用户在页面上选择需要作显示的教师,之后我们通过String teacherID = pageContext.getSession().getAttribute("teacherID").toString();
 我们是将教师的工号这个字段存放在session之中的,我们在之后进行数据库查询时也是以这个字段作为查询条件的。
 由于是在我们的应用程序的位置的不确定性,所以我们需要在使用这些对象之前需要在程序中对这些文件进行动态定位。
 String master = application.getRealPath("masterreport.jasper");
 这个方法是将,masterreport这个文件的位置放在一个字符串之中。这是根据jasperreport的方法而确定的。它需要主报表的位置。而它需要子报表的是以文件的形式。
 File one = new File(application.getRealPath("sub_baseInfo.jasper"));
 我们直接将一个子报表转化成一个文件对象。
 之后我们通过JasperReport oneR = (JasperReport) JRLoader.loadObject(one);
 这个方法来将文件对象转换成JasperReport对象,这是由于在定义子报表的时候是将其定义子报表是JasperReport对象。
 Map parameters = new HashMap();
 parameters.put("sub_one", oneR)
 我们将子报表作为变量放入主报表之中。我们是将变量保存在一个Map对象之中的。我们通过Key来取得其所标示的值。这个Key如上例中的”sub_one”是在主报表中定义好的变量名,所以需要按照定义好的位置传入正确的数据。
 在生成报表之前需要取得数据库连接对象。
 但这些准备好之后,我们就使用jasperreport提供的方法将产生一个PDF的对象字节流。这样我们就可以在页面上通过PDF阅读器来看一个报表对象。
 response.setContentType("application/pdf");
 response.setContentLength(bytes.length);
 ServletOutputStream ouputStream = response.getOutputStream();
 ouputStream.write(bytes, 0, bytes.length);
 ouputStream.flush();
 ouputStream.close();
 我们是使用servlet提供的方法来显示这些PDF文件。

 结束语
   经过三个月的工作,我们的整个师资管理系统已经可以在校内的局域网内试运行。我们的系统已经基本实现了师资办的教师提出的基本要求。但是毕竟因为是第一次使用Struts框架进行WEB应用程序的开发工作,在使用电子报表工具时,也是边学习边使用。所以在功能实现上也有一些问题。相信如果有充裕的时间的话,我们可以使程序更好的运行。
   在这段时间的开发过程中十分感谢组员的辛勤工作和无私的帮助,使我可以完成我的工作。还要感谢我的指导老师杨少雄,很感谢帮助我解决了一些技术性的问题。
   经过这次的开发,使我对于使用MVC设计框架进行WEB应用程序的开发有了一定的认识。并可以使用IRport这个报表工具方便的设计和生成动态电子报表。相信这个经历会对我的经后的工作带来很大的帮助。
 
 
 
第六章   参考文献
 1.《精通Struts:基于MVC的Java Web 设计与开发》
 2.《J2EE企业级应用开发》
 3.《Servlet/JSP程序设计技术与实例》
 4.《Struts KICK START》
 5.《JasperReport使用指南》
 
 
 
 
 
 
 
 

第七章   附录
1 Introduction
The JasperReports library is a very powerful and flexible report-generating tool that has the ability to
deliver rich content onto the screen, to the printer or into PDF, HTML or XML files. Hopefully, in the
future, other output formats such as CSV, XLS, RTF and other will be supported.
The library is entirely written in Java and can be used in a variety of Java enabled applications,
including J2EE or Web applications, to generate dynamic content. Its main purpose is to help creating
page oriented, ready to print documents in a simple and flexible manner.
JasperReports organizes data according to the report design defined in an XML file. This data may
come from various data sources including relational databases, collections or arrays of Java objects.
Users can plug the reporting library to custom made data sources, by implementing a simple interface,
as you will see later in this book.
In order to fill a report with data, the XML report design must be compiled first. Through compilation,
a report design object is generated and then serialized in order to store it on disk or send it over the
network. This serialized object is then used when the application wants to fill the specified report
design with data. In fact, the compilation of a report design implies the compilation of all Java
expressions defined in the XML file representing the report design. Various verifications are made at
compilation time, to check the report design consistency. The result is a ready to fill report design that
will be then used to generate documents on different sets of data.
To fill a report design, the engine needs to receive the report data. This may come in various forms.
Some of this data can be passed in as report parameters. But most of the data will be found in the report
data source. The reporting engine can directly receive special data source objects from which to get the
information to put on the report, or can deal itself with a supplied JDBC connection object, if that data
is located in a relational database.
The result of the report filling operation is a new object that represents the ready to print document.
This one is also serialized for storage on disk or network transfer. It can be viewed directly using the
JasperReports built-in viewer or can be exported to other, more popular formats like PDF, HTML or
XML.
The JasperReports Ultimate Guide
Page 5
2 API Overview
Most of the time, when using the JasperReports library, people will get to work only with a few classes
and won't have to get to know the entire API, in order to benefit from all its features and functionality.
In this section we shall get a close look at the classes and interfaces which are significant when using
the library and see how to make use of them in applications that need the reporting functionality that
JasperReports offer.
- figure 1 -
Class dori.jasper.engine.design.JasperDesign
We begin with this class because instances of it represent the raw material, which the JasperReports
library uses for report generating purposes. Such instances are obtained after the XML report design files are parsed by the library internal XML parsing procedures, for example, but can be build programmatically,
 by the application that uses JasperReports, if working with XML files is not an option. Among the supplied samples that come with the project source files, there is one called
noxmldesign that you can check to see how to dynamically create a
dori.jasper.engine.design.JasperDesign object without editing an XML report design file.
All instances of the dori.jasper.engine.design.JasperDesign class are subject to compilation
before being used for filling and report generation. This is why they are considered the raw material for
the library.
Class dori.jasper.engine.JasperReport
Instances of this class represent compiled report design objects. These can be obtained only as a result
of the JasperReports report compilation process and are ready to use for filling with data and report
generation.
Through compilation, along with various consistency checks and rearrangements of the report elements
for more rapid later utilization, the library creates a temporary class file containing all the report
expressions such as report variables expressions, text field and image expressions, group expressions,
etc.
This temporary Java source file is compiled on the fly using either the Java compiler classes from the
JDK used to run the application. If the tools.jar file is not found in the classpath in order to do that,
the compilation will go ahead anyway by launching at runtime the javac.exe compiler, behind the
scenes. The bytecodes of the resulting class file are stored in the resulting
dori.jasper.engine.JasperReport for using when filling the report with data, to eva luate the
various report expressions at runtime.
The JasperReports Ultimate Guide
Page 6
Class dori.jasper.engine.JasperCompileManager
This is the class that exposes all the library functionality concerning the report compilation.
It has various methods that allow the users to compile XML report designs found in files on disk or that
come from input streams. It also lets you compile in-memory report designs by directly passing a
dori.jasper.engine.design.JasperDesign object and receiving the corresponding
dori.jasper.engine.JasperReport object.
Other utility methods include report design verification and XML report design generation for inmemory
constructed dori.jasper.engine.design.JasperDesign class instances. Those are very
useful especially in GUI tools that simplify the report design work.
Class dori.jasper.engine.JasperPrint
After a compiled report design is filled with data, the resulting document comes in the form of a
dori.jasper.engine.JasperPrint instance. Such an object can be viewed directly using the
JasperReports build-in report viewer, or can be serialized for disk storage and later use, or for sending
it over the network.
The instances of this class represent the output of the report filling process of the JasperReports library
and can be considered as a custom format for storing full featured, page oriented documents. They can
be transformed into other more popular formats like PDF, HTML, XML or other by using the library's
export functionality.
Interface dori.jasper.engine.JRDataSource
JasperReports is a very flexible reporting tool as far as the source of the report data is concerned. It lets
people use any kind of data source they might want to, as long as they can provide an appropriate
implementation of this interface, so that the reporting engine can interpret and retrieve the data from
that data source when filling the reports.
Normally, every time a report is being filled, an instance of this interface is always supplied or created
behind the scenes by the reporting engine.
Class dori.jasper.engine.JRResultSetDataSource
This is a default implementation of the dori.jasper.engine.JRDataSource interface.
Since most of the reports are generated using data that comes from a relational database, JasperReports
includes by default this implementation that wraps a java.sql.ResultSet object.
This class can be instantiated on purpose, to wrap already loaded result sets, before passing it to the
report filling routines, but it is also used by the reporting engine to wrap the data retrieved from the
database after having executed through JDBC the report query, if present.
Class dori.jasper.engine.data.JRTableModelDataSource
This class represents another default implementation of the dori.jasper.engine.JRDataSource
interface that is shipped with the library. It wraps a javax.swing.table.TableModel object and
can be used in Java Swing applications to generate reports using data that has already been loaded into
on-screen tables.
The JasperReports Ultimate Guide
Page 7
Class dori.jasper.engine.JREmptyDataSource
Being the most simple implementation of the dori.jasper.engine.JRDataSource interface, this
class can be used in reports that do not display data from the supplied data source, but rather from
parameters, and when only the number of virtual rows in the data source is important.
Many of the provided samples such as fonts, images, shapes and unicode use an instance of this class
when filling the reports, to simulate a data source with one record in it, but with all the fields null.
Class dori.jasper.engine.JasperFillManager
This class is the facade to the report filling functionality of the JasperReports library.
It exposes a variety of methods that receive a report design in the form of an object, file or input stream
and produce a document also in various output forms: object, file or output stream.
But along with the report design, the report filling engine has to receive also the data source from
which to retrieve data and the values for the report parameters, in order to generate the documents.
Parameter values are always supplied in a java.util.Map object in which the keys are the report
parameter names.
The data source can be supplied in two different forms, depending on the situation:
Normally, it has to be is supplied as a dori.jasper.engine.JRDataSource object, like already
mentioned above.
But with the majority of reports in the reporting world being filled with data from relational databases,
JasperReports has a built-in default behavior that lets people specify an SQL query in the report design
itself. This SQL query is executed in order to retrieve the data to use when filling the report at runtime.
In such cases, the only thing JasperReports needs is a java.sqlConnection object, instead of the
usual data source object. It needs this connection object to connect to the relational database
management system through JDBC and execute the report query.
It will automatically creates a dori.jasper.engine.JRResultSetDataSource behind the scenes
to wrap the java.sql.ResultSet object returned after the execution of the query and passes it to the
normal report filling process.
Class dori.jasper.engine.JRAbstractScriptlet
Scriptlets are a very powerful feature of the JasperReports library. They allow users to write custom
code that will be executed by the reporting engine during the report filling process. This user code can
deal with report data manipulation and gets executed at well-defined moments such as page, column or
group breaks, opening a whole new range of possibilities in customizing the content of the generated
documents.
Class dori.jasper.engine.JRDefaultScriptlet
This is a convenience subclass of the dori.jasper.engine.JRAbstractScriptlet class. Most of
the time users will chose to subclass this when working with scriptlets, so they won't have to implement
all the abstract methods declared in the abstract class.
Class dori.jasper.engine.JasperPrintManager
We are talking here about a Java reporting tool and what reporting tools are meant for is printing.
After having filed a report, we have the option of viewing it, exporting it into a different format and last
but not least printing it.
The JasperReports Ultimate Guide
Page 8
In JasperReports, we can print reports using this particular manager class, which is a façade to the
printing functionality exposed by the library.
We can find here various methods that send to the printer entire documents or only portions of it, either
by displaying the print dialog or not.
The content of a page from a JasperReports document can be displayed by generating a
java.awt.Image object for it using this manager class.
Class dori.jasper.engine.JasperExportManager
As already mentioned, JasperReports allows transforming generated documents from its proprietary
format into more popular documents formats such as PDF, HTML or XML. With time, this part of the
JasperReports functionality will be extended to support other formats like CSV, XSL and other.
This manager class has various methods that ca process data that comes from different sources and
goes to different destinations: files, input and output streams, etc.
Class dori.jasper.engine.JasperRunManager
Sometimes is useful to produce documents only in a popular format such as PDF or HTML, without
having to store on disk the serialized, intermediate dori.jasper.egine.JasperPrint object,
produced by the report filling process.
This can be achieved using this manager class which immediately exports the document produced by
the report filling process into the desired output format.
The use of this manager class is shown and can be tested in the supplied webapp sample, where PDF
and HTML content is produced on the fly.
Class dori.jasper.view.JRViewer
This class is different from the rest of the classes listed above in the way that it is more like pluggable
visual component than an utility class.
It can be used in Swing based applications to view the reports generated by the JasperReports library.
- figure 2 -
This visual component is not meant to satisfy everybody. It was included in the main library more like
a demo component, to show how the core printing functionality can be used to display the reports in
Swing based applications, by generating java.awt.Image objects for the document pages, using the
dori.jasper.engine.JasperPrintManager class.
The JasperReports Ultimate Guide
Page 9
The preferred way to adapt this component to a particular application needs is by subclassing it.
Class dori.jasper.view.JasperViewer
This is also more like a didactical purpose class that uses the dori.jasper.view.JRViewer
component to display reports. It represents a simple Java Swing application that can load and display
reports. It is used in almost all of the supplied samples to display the generated documents.
Class dori.jasper.view.JasperDesignViewer
Usually, an application that uses the JasperReports library for reporting purposes will never get to work
with this class. This can be used at design time to preview the report templates before going into
production and help with the design work. It was included in the main library as a development tool in
order to make up for the missing visual designer.
- figure 3 -
This is also used in all the samples to preview the report designs, either in the raw XML form or the
compiled form.
Class dori.jasper.engine.util.JRLoader
All JasperReports main processes, like report compilation, report filling and exporting, often work with
serialized objects. Sometimes it is useful to manually load those serialized objects before submitting
them to the desired JasperReport process.
This is why we have the dori.jasper.engine.util.JRLoader class, which is a utility class that
helps loading serialized objects found in various locations such as files, URLs or input streams.
The most interesting method exposed by this class is the loadObjectFromLocation(String
location) method.
When calling this method to load an object from the supplied location, the program will first try to
interpret the location as a valid URL. If this fails, then the program will consider that the supplied
location is the name of a file on disk and will try to read from it. If no file is found at that location, it
will try to locate a resource through classpath that would correspond to the location. Only after this
third try fails, an exception will be thrown.
The JasperReports Ultimate Guide
Page 10
3 Main Tasks and Processes
In this chapter we shall see what you have to know in order to parse your XML report designs, compile
them, fill them with data, view them, print them, or export them to other formats.
3.1 XML Parsing
JasperReports uses the SAX 2.0 API to parse the XML files. However, it is not tided to a particular
SAX 2.0 implementation, like Xerces for examples, but instead you are able to decide at runtime what
XML parser you are using.
To instantiate the parser class, JasperReports uses the createXMLReader() method of the
org.xml.sax.helpers.XMLReaderFactory class.
In this case, it will be necessary at runtime to set the org.xml.sax.driver Java system property to
the full class name of the SAX driver, as specified in the SAX 2.0 documentation.
You can achieve this in two ways. We shall explain both using the Xerces XML parser, just like we do
it in the provided samples. If you use a different SAX 2.0 XML parser, you have to modify the name of
the parser class accordingly.
The first way you can set a system property is by using the -D switch in the command line when you
launch the Java Virtual Machine:
java -Dorg.xml.sax.driver=org.apache.xerces.parsers.SAXParser MySAXApp
sample.xml
In all the provided samples we use the ANT build tool to perform different tasks. We supply this
system property to the JVM using the <sysproperty> element of the <java> built-in task:
<sysproperty key="org.xml.sax.driver"
value="org.apache.xerces.parsers.SAXParser"/>
The second way to set a system property is by using the
java.lang.System.setProperty(String key, String value) method like this:
System.setProperty("org.xml.sax.driver",
"org.apache.xerces.parsers.SAXParser");
Check the jsp/compile.jsp and WEB-INF/classes/servlets/CompileServlet.java files in
the webapp sample provided, to see this in action.
3.2 Compiling Report Designs
In order to generate a report, one has to create the report's design first, either by editing an XML file or
by programmatically building a dori.jasper.engine.design.JasperDesign object. In this
book, we shall mainly deal with the XML approach, because it is the preferred way to use the
JasperReports library at least for the moment, and we'll have the chance to better understand its
behavior.
It is very likely that the present and future GUI tools that are and will be developed to help and simplify
the report design work will directly use the JasperReports API to create the report design objects,
without the need to pass through the XML form.
But this book does not try to document the use of such GUI tools and concentrates only on the
JasperReports core functionality. This is why the entire book is oriented towards explaining the content
The JasperReports Ultimate Guide
Page 11
and syntax of the XML report designs. Once this approach understood, the other, programmatic
approach should be more then intuitive.
As already mentioned, the XML report designs are the raw material that the library uses to generate
reports. This is because this XML content has to be parsed and loaded into a
dori.jasper.engine.design.JasperDesign object that has to suffer the report compilation
process before being ready to be filed with data by the reporting engine.
Note that most of the time, the report compilation should be considered more like a
development time job. You should compile your application report designs just like you
compile your Java source files, and you should ship them already compiled, with your
application, to be installed on the deployment platform. That's because in the majority of
cases, the report designs are static and few applications need to offer their users the
possibility to dynamically generate report designs, that would need to be compiled at
runtime.
The main purpose of the report compilation process is to produce and load the bytecodes of a class
containing all the report expressions. This dynamically created class will be used when filling the
report to eva luate all those report expressions.
But before proceeding with this class generation, the engine verifies the report design for consistency
and will not continue if at least one validation check fails. We shall see what are the conditions for a
report design to be considered valid in the following chapters of this book.
For now, we only need to know how the report compilation works, so that we can make sure it can be
performed successfully.
There are at least three important aspects concerning the way the bytecodes for this class containing all
the report expressions are obtained:
_ temporary working directory;
_ Java compiler used;
_ classpath;
In order to be able to compile a Java source file, this file must be created and saved on disk. The output
of the Java compilation process is also a file with the .class extension. This is why JasperReports
needs access to a temporary, working directory for it to create the class containing the report
expressions and to compile it. After the report compilation task is finished, those temporary class files
will be automatically deleted and the resulting bytecodes are stored in the resulting
dori.jasper.engine.JasperReport, which can be serialized itself and stored on disk, if desired.
By default, the temporary working directory is the current directory when launching the JVM, and it is
obtained interrogating the user.dir system property. It can easily be changed, by supplying a value to
a system property called jasper.reports.compile.temp. This is useful especially in Web
environment when you don't want to end up using the same directory that contains the batch files that
launch the Web server, as a temporary working location for the report compilation process.
The second aspect mentioned concerns the Java compiler used to compile the report expressions class.
First, the reporting engine tries to compile the Java source file using the sun.tools.javac.Main
class. This approach may succeed only if the tools.jar file that contains this class, usually found in
the /lib directory of the JDK installation directory, is available through classpath.
If loading the compiler class sun.tools.javac.Main fails, the program tries to dynamically launch
the Java compilation process, just like we would normally do it from the command line, using the
javac.exe program found in the /bin directory of the JDK installation directory.
This is why in the project tree available for download, copying the tools.jar file from the JDK
location into the /lib directory of the JasperReports project is an optional operation. If the
tools.jar file is not found in classpath, JasperReports displays a warning and continues as
mentioned.

 

 

 

 

 

 

 

 

 

 

 

介绍
JasperReports是一个灵活强大的报表生成工具。其可以产生多种文件格式如PDF、HTML或XML。并且未来将会支持诸如CSV、XLS、RTF等格式的输出。它是用纯JAVA编写的,它可以应用于J2EE或WEB应用程序。它的主要目的是帮助一个简单或丰富的输出文件。JasperReport是按报表内的数据生成相应的XML文件。这些数据来自多种数据源包括关系型数据库,集合或Java类对象的数组。用户可以通过一些简单的接口生成用户自定义数据源,这些将在下文中介绍。为了让这些数据加入文件,你必须将设计好的XML报表进行编译。通过编译程序可以产生完整的报表文件。这样可以将其保存在本地硬盘或通过网络传输。这个经过编译的文件可以在程序需要使用报表时传入参数。事实上经编译的报表是将定义成XML文件的报表转换成JasperReport格式。在编译时会对XML定义的文件作多种检查保证经编译文件的正确性。经编译的结果是包含了报表中的变量等信息,可以转换成各种文件形式。为了生成一个报表,驱动引擎需要接收数据。这个有多种方式。一些数据可以通过报表变量传递。但是大部分的数据是通过数据源传入的。报表生成引擎可以接收指定的数据源对象装入报表中,或通过处理连接到关联数据库上的JDBC数据对象。这是就产生了可以被显示的含参数的结果。
这个结果可以通过JasperReport内建的显示器或转换成一些流行的格式如PDF、HTML或XML。

2.API介绍
大多人使用JasperReport类库时,只是使用了一部分类,并没有了解整个API库,可能觉得这样没有什么必要。那么我们就着眼于一些重要的类和接口,并研究一下如何在程序里使用,使得JasperReport文件功能更加强大。
Class dori.jasper.engine.design.JasperDesign
我们从这个类开始,因为这是报表类库用来产生报表最基本的原料。这个对象是在XML报表文件建立之后通过XML解析程序生成的。例如一个应用程序使用JasperReports,如果不使用XML格式编辑,在一些例子是一些工程源文件,这些称做noxmldesign,你可以动态建立一个dori.jasper.engine.design.JasperDesign对象而不需要编辑XML。一个实例化的dori.jasper.engine.design.JasperDesign对象将在填充数据和生成报表之前编译。这就是我们成其为原料的原因。
Class dori.jasper.engine.JasperReport
这个类的实例化对象是一个经过编译的框架报表对象。其是通过对JasperReport报表对象编译取得的并可以作为填充数据和展现报表的对象。在编译时要经过错误检查和对报表元素的重组,以提高使用时的效率。类库建立一个临时文件保存包括报表变量表达式、文字栏、图片、分组表达式等报表变量。这些临时的Java源文件可以使用Java编译类或通过JDK进行。如果无法通过classpath找到tools.jar文件进行编译,编译器会通过启动javac.exe进行编译。经编译的字节码文件是作为dori.jasper.engine.JasperReport的编译结果。
Class dori.jasper.engine.JasperCompileManager
这个类是关于进行编译报表的功能。他有多种方法允许用户编译XML设计的报表或其他输入流。他甚至可以将保存在内存中的dori.jasper.engine.design.JasperDesign对象或接收
droi.jasper.engine.JasperReport对象。
Class dori.jasper.engine.JasperPrint
在编译过的设计报表是填充了数据的,这个结果将由类
dori.jasper.engine.JasperPrint实例化。这样的一个实例化对象可以被内建的JasperReports浏览器预览,或作为最终的文件保存。这个类的实例化对象时填充了数据对象并保存有用户设计的样式。你可以将这个文件转换成诸如PDF、HTML、XML或其他支持的格式。
Interface dori.jasper.engine.JRDataSource
JasperReports是一个灵活的报表生成工具。用户可以使用各种各样他想使用的数据源,他只要使用适当的接口,这样报表生成引擎就可以将来自指定数据源的数据传入报表。一般的,每次向报表做数据填充式都回实例化一个本接口对象来支持报表建立。
Class dori.jasper.engine.JRResultSetDataSoruce
这是从接口dori.jasper.engine.JRDataSoruce继承的类。因为大部分的报表生成时所用的数据都来自关系型数据库,JasperReport就将java.sql.ResultSet封装在这个类中。这个类的目的是在装载数据到设计报表之前,取得数据结果集,它是通过JDBC取得对数据库操作的结果。
Class dori.jasper.engine.data.JRTableModelDataSource
这个是继承另一个接口dori.jasper.engine.JRSataSource。他封装了
Javax.swing.table.TableModel对象,那么他可以被本地的Java应用程序展现报表时使用。
Class dori.jasper.engine.JREmptyDataSource
这是一个非常简单的类,同样继承接口dori.jasper.engine.JRDataSource。这个类在不需要数据而只是展现一个报表的框架时使用。其中的字体、图片、形状和编码都将被实例化,需要传入数据的部分都将以空填充。
Class dori.jasper.engine.JasperFillManager
这个类是作为向设计报表装载数据的。他有许多的方法用来接收处理对象、文件或输入流,它的输出同样有多种:对象、文件或输出流。为了向设计报表装载数据它需要从数据源找到与报表变量相对应的数据一边生成报表。变量的值是放在java.util.Map类对象下的,通过“Key”找到对应的值。支持两种数据源,按照情形分:一般的,它支持上边提到过的dori.jasper.engine.JRDataSource对象。但是大多数的报表时使用关系型数据库来获取数据的。JasperReports用内建的方法允许用户指定一个SQL语句。这个SQL语句是为了接收数据来填充设计报表。这种情形下JasperReports只需要使用java.sqlConnection对象而不是一般的数据源对象。它需要通过JDBC来连接关系型数据库系统来执行查询语句。它将自动的建立一个dori.jasper.engine.JRResultSetDataSource对象,这个类是封装了java.sql.ResultSet对象,用来返回查询结果的。
Class dori.jasper.engine.JRAbstractScriptlet
脚本标记是JasperReport中一个很有用的特性。它允许编制用户代码,这些代码将在报表引擎装载数据时运行。这些用户代码可以处理报表中的数据处理工作并且可以执行定义页、列或者组的分割。这样完全可以展现用户所需要的报表显示。
Class dori.jasper.engine.JRDefaultScriptlet
这是dori.jasper.engine.JRAbstractScriptlet类的子类。大部分的用户会选择使用这个子类,这样他们不必实现所有的在父类中定义的抽象方法。

Class dori.jasper.engine.JasperPrintManager
我们在这里讨论的一个Java报表生成工具和报表打印工具。当我们有了一个完整的报表我们可以选择浏览,或是把它转换到其他的格式或只是作打印。在JasperReport中我们使用这个具有强大打印功能的类来实现。我们可以从这个类中找到多种方法,可以打印全部报表或是部分报表。JasperReport文件中目录页可以通过java.awt.Image对象来进行展现。

Class dori.jasper.engine.JasperExportManager
就像上边提到的JasperReports工具允许将报表转换成PDF、HTML或XML。一些时间之后JasperReports将会支持CSV、XSL或其他一些类型。这个类有多种方法可以处理来自多种数据源如文件、输入(输出)流等。

Class dori.jasper.engine.JasperRunManager
有时候只想将报表保存为PDF或HTML格式,但不想保存为dori.jasper.engine.JasperPrint所支持的序列文件。这个类能够方便的产生您所想要的格式。如何使用这个类,可以在原文件webapp中找到生成的PDF和HTML文件。

Class dori.jasper.view.JRViewer
这是一个与以上提到的不同类型的类,它更像是一个可视化容器。他可以应用于本地Java应用程序中显示JasperReport文件。但这个可视化容器不一定令每个人都很满意。这是一个更像是一个demo容器,在本地应用程序中通过生成java.awt.Image对象显示dori.jasper.engine.JasperPrintManager装载完成的对象。当你需要通过继承这个类你可以做一些修改来满足您的要求。

Class dori.jasper.view.JasperViewer
这同样是一个更像是教学目的的类,来指导如何使用dori.jasper.view.JRView来显示报表。他是一个简单地的可以装载显示报表的Swing应用程序。他几乎应用于每一个实例程序中来显示报表。

Class dori.jasper.view.JasperDesignViewer
通常人们在程序中使用了JasperReports库时是不会使用到这个类。这是在你设计报表模版是帮助你设计的。有了这个开发辅助工具是为没有可视化开发工具的一个补偿。这个同样在实例程序中可以预览XML格式或经编译的模版。

Class dori.jasper.engine.util.JRLoader
所有JasperReports的主要产生过程如报表编译、填充数据和报表输出,通常是在处理序列化的对象。在你的得到你想要的JasperReport之前你需要先装载着先序列对象。这是我们为什么使用dori.jasper.engine.util.JRLoader,它可以帮助你从本地硬盘、网络上或是一个输入流中装载序列对象。这个类中最为有趣的方法是loadObjectFromLocation(String location)。在我们调用这个方法从指定位置装载对象时,这个方法会先将这个位置解析成一个合法的URL。如果失败了程序会认为这个路径是一个本地文件路径将试图从本地硬盘上读取。如果没有文件存在于指定的路径下,他会从系统中默认的JasperReports的工作目录中查找。只有经过这三次失败,才会抛出异常。

主要的任务和过程
在这节中我们将讨论如何分析XML设计的报表模板、如何填充数据、如何预览、如何打印或如何以其他格式输出。

XML的解析
JasperReports使用SAX2.0 API来解析XML文件。然而在执行时细节SAX2.0并不是很好,而例如Xerces可以在运行时决定如何解析XML文件。在实例化解析对象时JasperReports使用org.xml.sax.helpers.XMLReaderFactory中的createXMLReader()方法。在这种情况下在运行的时候需要指定org.xml.sax.driver的SAX驱动名称来指定SAX 2.0解析文件。你可以用两种方法实现。我们使用Xerces XML解析程序作解释。如果你使用是不同的SAX 2.0 XML解析程序,你必须修改解析器的名称。首先你需要设置系统变量,在当你命令窗口中启动了Java虚拟机是输入:
Java–D org.xml.sax.driver=org.apache.xerces.parsers.SAXParser MySAXApp
Sample.xml
在所有的示例中我们使用了ANT编译工具来实现不同的方法。我们支持在Java虚拟机中使用sysproperty这个变量来修改。
<sysproperty key = "org.xml.sax.driver"
 Value = "org.apache.xerces.parsers.SAXParser"/>
第二种方法设置环境变量是使用
java.lang.System.setProperty(String key, String value)方法:
System.setProperty("org.xml.sax.driver",
 "org.apache.xerces.parsers.SAXParser");
请确定jsp/compile.jsp和WEB-INF/classes/servlets/CompileServlet.java文件在webapp中。

编译报表
为了生成报表,你可以编辑XML文件或是使用程序建立dori.jasper.engine.design.JasperDesign对象。在这本书中我们着重介绍使用XML方法,这至少在目前是较好的方法使用JasperReports,我们将有机会更多地了解这个方法。当然现在也有GUI工具可以帮助你更加容易的设计报表。而这些工具就是直接通过JasperReports API来建立报表对象而不是通过解析XML文件的。但是这个材料我们着重介绍JasperReports核心程序而不去介绍GUI工具。一旦可以理解如何使用XML编写报表,那么其他的如用程序的方法就会变得容易。正如上文中提到的XML文件是JasperReports生成报表的基础。这是因为XML文件将会在填充数据生成报表前被编译成dori.jasper.engine.design.JasperDesign对象。大部分时候报表的编译过程与你编写的Java原程序很相似。报表的模版设计通常是静态的,而输出报表是动态的所以需要实时的进行编译。他所做的主要工作是将报表中表达式编译成二进制代码,之后再将其装载。这个动态生成的过程中将装入数据。


以上为本篇毕业论文范文B/S系统下数据访问及报表生成技术的介绍部分。
本论文在计算机论文栏目,由论文网(www.zjwd.net)整理,更多论文,请点论文范文查找

毕业论文降重 相关论文

收费专业论文范文
收费专业论文
汉语言文学论文
物理学论文
自动化专业论文
测控技术专业论文
历史学专业论文
机械模具专业论文
金融专业论文
电子通信专业论文
材料科学专业论文
英语专业论文
会计专业论文
行政管理专业论文
财务管理专业论文
电子商务国贸专业
法律专业论文
教育技术学专业论文
物流专业论文
人力资源专业论文
生物工程专业论文
市场营销专业论文
土木工程专业论文
化学工程专业论文
文化产业管理论文
工商管理专业论文
护理专业论文
数学教育专业论文
数学与应用数学专业
心理学专业论文
信息管理专业论文
工程管理专业论文
工业工程专业论文
制药工程专业论文
电子机电信息论文
现代教育技术专业
新闻专业论文
艺术设计专业论文
采矿专业论文
环境工程专业论文
西班牙语专业论文
热能与动力设计论文
工程力学专业论文
酒店管理专业论文
安全管理专业论文
交通工程专业论文
体育教育专业论文
教育管理专业论文
日语专业论文
德语专业论文
理工科专业论文
轻化工程专业论文
社会工作专业论文
乡镇企业管理
给水排水专业
服装设计专业论文
电视制片管理专业
旅游管理专业论文
物业管理专业论文
信息管理专业论文
包装工程专业论文
印刷工程专业论文
动画专业论文
环境艺术专业论文
信息计算科学专业
物流专业论文范文
人力资源论文范文
营销专业论文范文
工商管理论文范文
汉语言文学论文范文
法律专业论文范文
教育管理论文范文
小学教育论文范文
学前教育论文范文
财务会计论文范文

电子商务论文范文

上一篇:Browser/Server模式下管理系统的.. 下一篇:MVC结构模式下WEB表达层的设计和..

最新论文

精品推荐

毕业论文排版

热门论文


本站简介 | 联系方式 | 论文改重 | 免费获取 | 论文交换

本站部分论文来自网络,如发现侵犯了您的权益,请联系指出,本站及时确认删除 E-mail:229120615@qq.com

毕业论文范文-论文范文-论文同学网(www.zjwd.net)提供计算机论文毕业论文,毕业论文范文,毕业设计,论文范文,毕业设计格式范文,论文格式范文

Copyright@ 2010-2024 zjwd.net 毕业论文范文-论文范文-论文同学网 版权所有