---
title: "[BIRT]-使用Java对象自定义数据源和数据集"
author: "flyingbug"
date: "2005-08-22T23:20:00Z"
tags: []
slug: "birt-java"
draft: false
lastmod: "2005-08-22T23:20:00Z"
id: 40287
url: "https://flyingbug.iteye.com/blog/40287"
---

l         使用DataSet的‘open’方法来执行初始化任务：右击DataSet选择‘编辑代码’，然后在其中选择‘open’方法，这里初始化一个java对象来完成数据访问功能

l         Open方法

a)         在script中引用的包含java对象的class文件的jar文件，必须包含在&lt;ECLIPSE\_INSTALL&gt;\\plugins\\org.eclipse.birt.report.viewer\\birt\\WEB-INF\\lib.目录下

b)        引入一个package：importPackage(Packages.com.yourCompany.yourApplication);[]() 引入通常在open方法的第一行。&lt;o:p&gt;&lt;/o:p&gt;

c)        创建一个java对象的实例：var myList = MyListFactory.getList();&lt;o:p&gt;&lt;/o:p&gt;

d)        从java对象中获取数据的典型方式是从该对象中获得一个包含数据的Iterator：  
var iterator = myList.getIterator();[]() &lt;o:p&gt;&lt;/o:p&gt;

l         Fatch方法（fatch方法从Open中创建的java实例中获得数据，并赋值给行）

a)         Fatch方法的第一步是判断Iterator中是否有数据：  
if(iterator.hasNext() == false ){[]()  
  return null;[]()  
}[]() &lt;o:p&gt;&lt;/o:p&gt;

b)        然后是从iterator中得到一个行对象node以便得到该行中每列的数据：  
var node = iterator.next( );[]()  
row\[1] = node.getFirstCol( );[]()  
row\[2] = node.getSecondCol( );[]()  
row\[3] = node.getThirdCol( );[]() &lt;o:p&gt;&lt;/o:p&gt;

c)        最后，是返回true：return true;[]() &lt;o:p&gt;&lt;/o:p&gt;

l         Close方法：

a)         最后，在Close方法中清理所有创建的对象：  
myList = null;[]()  
iterator = null;[]()  
node = null;[]()