您的位置:首页 > 编程语言 > Java开发

iReport使用javabean做数据源

2007-06-09 19:58 323 查看
iReport使用javabean做数据源需要四个步骤
一、建立一个javabean,这是一个普通的bean:




/**//*




* PersonBean.java




*




* Created on 8 luglio 2004, 1.41




*/






package it.businesslogic.ireport.examples.beans;








/** *//**




*




* @author Administrator




*/






public class PersonBean ...{








private String firstName;








private String lastName;








private HobbyBean[] hobbies;








private AddressBean address;










/** *//** Creates a new instance of PersonBean */






public PersonBean() ...{




this(null);




}










public PersonBean(String name) ...{




this.setFirstName( name );




hobbies = new HobbyBean[0];




}










/** *//**




* Getter for property firstName.




* @return Value of property firstName.




*/






public java.lang.String getFirstName() ...{




return firstName;




}










/** *//**




* Setter for property firstName.




* @param firstName New value of property firstName.




*/






public void setFirstName(java.lang.String firstName) ...{




this.firstName = firstName;




}










/** *//**




* Getter for property lastName.




* @return Value of property lastName.




*/






public java.lang.String getLastName() ...{




return lastName;




}










/** *//**




* Setter for property lastName.




* @param lastName New value of property lastName.




*/






public void setLastName(java.lang.String lastName) ...{




this.lastName = lastName;




}










/** *//**




* Getter for property hobbies.




* @return Value of property hobbies.




*/






public it.businesslogic.ireport.examples.beans.HobbyBean[] getHobbies() ...{




return this.hobbies;




}










/** *//**




* Setter for property hobbies.




* @param hobbies New value of property hobbies.




*/






public void setHobbies(it.businesslogic.ireport.examples.beans.HobbyBean[] hobbies) ...{




this.hobbies = hobbies;




}










/** *//**




* Getter for property address.




* @return Value of property address.




*/






public it.businesslogic.ireport.examples.beans.AddressBean getAddress() ...{




return address;




}










/** *//**




* Setter for property address.




* @param address New value of property address.




*/






public void setAddress(it.businesslogic.ireport.examples.beans.AddressBean address) ...{




this.address = address;




}








}





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


import net.sf.jasperreports.engine.*;




import net.sf.jasperreports.engine.data.*;




import it.businesslogic.ireport.examples.beans.*;




import java.util.*;








public class PersonBeansDataSource extends JRAbstractBeanDataSourceProvider ...{










public PersonBeansDataSource() ...{




super(PersonBean.class);




}










public JRDataSource create(JasperReport report) throws JRException ...{








ArrayList list = new ArrayList();






/**//*这里查询数据库或其他途经,把数据包装到bean中




*把bean压入list中




*




*/




return new JRBeanCollectionDataSource(list);








}










public void dispose(JRDataSource dataSource) throws JRException ...{




// nothing to do




}




}





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

四、在报表中 点击 资料来源——〉报表查询——〉选择Use DataSource Provider选项卡——〉点击Get fields from datasource 得到bean中定义的属性为 field
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: