iReport使用javabean做数据源

iReport使用javabean做数据源需要四个步骤

一、建立一个javabean,这是一个普通的bean

java 代码
  1. /*  
  2.  
  3.  * PersonBean.java  
  4.  
  5.  *  
  6.  
  7.  * Created on 8 luglio 2004, 1.41  
  8.  
  9.  */  
  10.   
  11.   
  12. package it.businesslogic.ireport.examples.beans;   
  13.   
  14.   
  15. /** *//**  
  16.  
  17.  *  
  18.  
  19.  * @author  Administrator  
  20.  
  21.  */  
  22.   
  23. public class PersonBean ...{   
  24.   
  25.        
  26.   
  27.     private String firstName;   
  28.   
  29.        
  30.   
  31.     private String lastName;   
  32.   
  33.        
  34.   
  35.     private HobbyBean[] hobbies;   
  36.   
  37.        
  38.   
  39.     private AddressBean address;   
  40.   
  41.        
  42.   
  43.     /** *//** Creates a new instance of PersonBean */  
  44.   
  45.     public PersonBean() ...{   
  46.   
  47.        this(null);   
  48.   
  49.     }   
  50.   
  51.        
  52.   
  53.     public PersonBean(String name) ...{   
  54.   
  55.            this.setFirstName( name );   
  56.   
  57.         hobbies = new HobbyBean[0];   
  58.   
  59.     }   
  60.   
  61.        
  62.   
  63.     /** *//**  
  64.  
  65.      * Getter for property firstName.  
  66.  
  67.      * @return Value of property firstName.  
  68.  
  69.      */  
  70.   
  71.     public java.lang.String getFirstName() ...{   
  72.   
  73.         return firstName;   
  74.   
  75.     }   
  76.   
  77.        
  78.   
  79.     /** *//**  
  80.  
  81.      * Setter for property firstName.  
  82.  
  83.      * @param firstName New value of property firstName.  
  84.  
  85.      */  
  86.   
  87.     public void setFirstName(java.lang.String firstName) ...{   
  88.   
  89.         this.firstName = firstName;   
  90.   
  91.     }   
  92.   
  93.        
  94.   
  95.     /** *//**  
  96.  
  97.      * Getter for property lastName.  
  98.  
  99.      * @return Value of property lastName.  
  100.  
  101.      */  
  102.   
  103.     public java.lang.String getLastName() ...{   
  104.   
  105.         return lastName;   
  106.   
  107.     }   
  108.   
  109.        
  110.   
  111.     /** *//**  
  112.  
  113.      * Setter for property lastName.  
  114.  
  115.      * @param lastName New value of property lastName.  
  116.  
  117.      */  
  118.   
  119.     public void setLastName(java.lang.String lastName) ...{   
  120.   
  121.         this.lastName = lastName;   
  122.   
  123.     }   
  124.   
  125.        
  126.   
  127.     /** *//**  
  128.  
  129.      * Getter for property hobbies.  
  130.  
  131.      * @return Value of property hobbies.  
  132.  
  133.      */  
  134.   
  135.     public it.businesslogic.ireport.examples.beans.HobbyBean[] getHobbies() ...{   
  136.   
  137.         return this.hobbies;   
  138.   
  139.     }   
  140.   
  141.        
  142.   
  143.     /** *//**  
  144.  
  145.      * Setter for property hobbies.  
  146.  
  147.      * @param hobbies New value of property hobbies.  
  148.  
  149.      */  
  150.   
  151.     public void setHobbies(it.businesslogic.ireport.examples.beans.HobbyBean[] hobbies) ...{   
  152.   
  153.         this.hobbies = hobbies;   
  154.   
  155.     }   
  156.   
  157.        
  158.   
  159.     /** *//**  
  160.  
  161.      * Getter for property address.  
  162.  
  163.      * @return Value of property address.  
  164.  
  165.      */  
  166.   
  167.     public it.businesslogic.ireport.examples.beans.AddressBean getAddress() ...{   
  168.   
  169.         return address;   
  170.   
  171.     }   
  172.   
  173.        
  174.   
  175.     /** *//**  
  176.  
  177.      * Setter for property address.  
  178.  
  179.      * @param address New value of property address.  
  180.  
  181.      */  
  182.   
  183.     public void setAddress(it.businesslogic.ireport.examples.beans.AddressBean address) ...{   
  184.   
  185.         this.address = address;   
  186.   
  187.     }   
  188.   
  189.        
  190.   
  191. }   

 

二 实现JRDataSourceProvider接口

       实现JRDataSourceProvider接口最好的办法是继承JRAbstractBeanDataSourceProvider类:

java 代码
  1. import net.sf.jasperreports.engine.*;   
  2.   
  3. import net.sf.jasperreports.engine.data.*;   
  4.   
  5. import it.businesslogic.ireport.examples.beans.*;   
  6.   
  7. import java.util.*;   
  8.   
  9.   
  10. public class PersonBeansDataSource extends JRAbstractBeanDataSourceProvider ...{   
  11.   
  12.      
  13.   
  14.       public PersonBeansDataSource() ...{   
  15.   
  16.              super(PersonBean.class);   
  17.   
  18.       }   
  19.   
  20.      
  21.   
  22.       public JRDataSource create(JasperReport report) throws JRException ...{   
  23.   
  24.                 
  25.   
  26.              ArrayList list = new ArrayList();   
  27.   
  28. /**//*这里查询数据库或其他途经,把数据包装到bean中  
  29.  
  30. *把bean压入list中  
  31.  
  32.               *  
  33.  
  34. */  
  35.   
  36.              return new JRBeanCollectionDataSource(list);   
  37.   
  38.          
  39.   
  40.       }   
  41.   
  42.          
  43.   
  44.       public void dispose(JRDataSource dataSource) throws JRException ...{   
  45.   
  46.              // nothing to do   
  47.   
  48.       }   
  49.   
  50.   }   

 

三、打开ireport 5.0 ,选择资料来源——〉连接/资料来源——〉new——〉type of connection/datasource选项选择JRDataSourceProvider,——〉填入名称和class——〉save——〉完成

 

四、在报表中 点击   资料来源——〉报表查询——〉选择Use DataSource Provider选项卡——〉点击Get fields from datasource 得到bean中定义的属性为 field

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值