`
jy00509336
  • 浏览: 238220 次
  • 性别: Icon_minigender_1
  • 来自: 山西
社区版块
存档分类
最新评论

iReport+jasperReport之BEAN数据源(续)

阅读更多

转载自:http://www.blogjava.net/bulktree/archive/2008/12/12/245963.html

上篇bean数据源,如果我们自己定义自己的数据源该如何去写呢?
        jasperReport提供了很多的便利去实现自己的DataSource,简单的有三种方式:
        直接实现bean的方式      bean工厂     表格模型

还是使用前面的person.jasper文件,和Person.java VO类

    直接实现bean的方式:
    此种方式需要实现JRDataSource接口,定义一个二维对象数组用来存放数据,通过遍历数组的数据实现getFieldValue()和next()方法

    PersonDataSource.java
package org.bulktree.ireport.customdata;

import javax.activation.DataSource;

import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRField;

/**
 * 
 * 
@author bulktree Email: laoshulin@gmail.com @ Nov 7, 2008
 
*/

public class PersonDataSource implements JRDataSource {

    
public PersonDataSource() {

    }


    
private Object[][] data = {
            
"001""bulktree1""Man1""21""001111""IsoftStone1" },
            
"002""bulktree2""Man2""22""002222""IsoftStone2" },
            
"003""bulktree3""Man3""23""003333""IsoftStone3" },
            
"004""bulktree4""Man4""24""004444""IsoftStone4" },
            
"005""bulktree5""Man5""25""005555""IsoftStone5" },
            
"006""bulktree6""Man6""26""006666""IsoftStone6" },
            
"007""bulktree7""Man7""27""007777""IsoftStone7" },
            
"008""bulktree8""Man8""28""008888""IsoftStone8" },
            
"009""bulktree9""Man9""29""009999""IsoftStone9" },
            
"001""oakertree1""Man1""21""001111""IsoftStone1" },
            
"002""oakertree2""Man2""22""002222""IsoftStone2" },
            
"003""oakertree3""Man3""23""003333""IsoftStone3" },
            
"004""oakertree4""Man4""24""004444""IsoftStone4" },
            
"005""oakertree5""Man5""25""005555""IsoftStone5" },
            
"006""oakertree6""Man6""26""006666""IsoftStone6" },
            
"007""oakertree7""Man7""27""007777""IsoftStone7" },
            
"008""oakertree8""Man8""28""008888""IsoftStone8" },
            
"009""oakertree9""Man9""29""009999""IsoftStone9" },
            
"001""laoshulin1""Man1""21""001111""IsoftStone1" },
            
"002""laoshulin2""Man2""22""002222""IsoftStone2" },
            
"003""laoshulin3""Man3""23""003333""IsoftStone3" },
            
"004""laoshulin4""Man4""24""004444""IsoftStone4" },
            
"005""laoshulin5""Man5""25""005555""IsoftStone5" },
            
"006""laoshulin6""Man6""26""006666""IsoftStone6" },
            
"007""laoshulin7""Man7""27""007777""IsoftStone7" },
            
"008""laoshulin8""Man8""28""008888""IsoftStone8" },
            
"009""laoshulin9""Man9""29""009999""IsoftStone9" } }
;

    
private int index = -1;

    
public Object getFieldValue(JRField jrField) throws JRException {

        Object value 
= null;

        String fieldName 
= jrField.getName();

        
if ("pid".equals(fieldName)) {
            value 
= data[index][0];
        }

        
if ("name".equals(fieldName)) {
            value 
= data[index][1];
        }

        
if ("sex".equals(fieldName)) {
            value 
= data[index][2];
        }

        
if ("age".equals(fieldName)) {
            value 
= data[index][3];
        }

        
if ("password".equals(fieldName)) {
            value 
= data[index][4];
        }

        
if ("department".equals(fieldName)) {
            value 
= data[index][5];
        }


        
return value;
    }


    
public boolean next() throws JRException {
        index
++;

        
return (index < data.length);
    }


}


这个和遍历数据库Result的游标是一样的道理,刚开始的时候是指向第一条数据的前面,next一下才会指向第一条数据,相信这个不是很难懂吧!通过以下语句产生 JasperPrint对象
jasperPrint = JasperFillManager.fillReport(jasperReport,
                    getReportParameter(), personDataSource);
personDataSource为PersonDataSource的对象,预览效果如下:

     bean工厂:

    此种方式不需要实现JRDataSource接口,定义bean数组存放数据,只需要简单的提供两个方法即可:返回一个对象数组和返回一个bean集合
    PersonBeanFactory.java代码如下:
package org.bulktree.ireport.customdata;

import java.util.Arrays;
import java.util.Collection;

/**
 * 
 * 
@author bulktree Email: laoshulin@gmail.com @ Nov 7, 2008
 
*/

public class PersonBeanFactory {
    
private static Person[] data = {
            
new Person("001""bulktree1""Man1""21""001111""IsoftStone1"),
            
new Person("002""bulktree2""Man2""22""002222""IsoftStone2"),
            
new Person("003""bulktree3""Man3""23""003333""IsoftStone3"),
            
new Person("004""bulktree4""Man4""24""004444""IsoftStone4"),
            
new Person("005""bulktree5""Man5""25""005555""IsoftStone5"),
            
new Person("006""bulktree6""Man6""26""006666""IsoftStone6"),
            
new Person("007""bulktree7""Man7""27""007777""IsoftStone7"),
            
new Person("008""bulktree8""Man8""28""008888""IsoftStone8"),
            
new Person("009""bulktree9""Man9""29""009999""IsoftStone9"),
            
new Person("001""oakertree1""Man1""21""001111""IsoftStone1"),
            
new Person("002""oakertree2"
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics