在使用Ireport的時候,我從不建議采用這種方式制作報表:
JasperPrint jasperPrint = JasperFillManager.fillReport(String,Map,Connection); 使用hibernate的Session同樣不推薦。
原因:
1.性能考慮:
如果你將一個數(shù)據(jù)庫連接交給ireport也就意味著,你必須等待ireport將所有的操作完成才可以回收該鏈接,如果是在大批量數(shù)據(jù)顯示的情況下(往往如此),將占用很長很長的連接時間,如果實現(xiàn)了JRDataSource接口 ,先將數(shù)據(jù)保存到一個list中,那么將花費你極少的時間來處理數(shù)據(jù)庫這部分,之后的數(shù)據(jù)顯示再交給ireport
2.數(shù)據(jù)庫部分與ireport耦合太大
尤其是采用hibernate的session 那樣帶來的耦合度更大,
3.不利于代碼的重用
如果你是實現(xiàn)了JRDataSource接口,現(xiàn)將數(shù)據(jù)保存到list中,那么即使你采用ireport來顯示數(shù)據(jù),在其他的程序中一樣的可以隨時調(diào)用你的getList()方法
4.安全性考慮
越是將數(shù)據(jù)庫暴露到上層,那么應(yīng)用系統(tǒng)就越危險。
其實實現(xiàn)JRDataSource接口非常簡單:
只要實現(xiàn)兩個方法:
public class LedgerDataSource implements JRDataSource{
private List datas = null ;
private int loop = -1 ;
private static Logger logger = Logger.getLogger(LedgerDataSource.class.getName());
public Object getFieldValue(JRField field) throws JRException {
StatBean temp = (StatBean)this.datas.get(loop);
Object rs = "" ;
if("month".equals(field.getName())){
rs = temp.getMonth();
}else if("day".equals(field.getName())){
rs = temp.getDay();
}else if("voucherNo".equals(field.getName())){
rs = temp.getVoucherNo();
}else if("debitMoney".equals(field.getName())){
rs = temp.getDebitMoney();
}else if("creditMoney".equals(field.getName())){
rs = temp.getCreditMoney();
}else if("DOrc".equals(field.getName())){
rs = temp.getDOrc();
}else if("summary".equals(field.getName())){
rs = temp.getSummary();
}else if("balance".equals(field.getName())){
rs = temp.getBalance();
}
temp = null ;
return rs;
}
public boolean next() throws JRException {
loop ++ ;
if(loop >= datas.size()){
return false;
}else{
return true ;
}
}
public void finalized(){
datas = null ;
}
}
在使用該datasource的時候,只需要JasperPrint jasperPrint = JasperFillManager.fillReport(String,Map,JRDataSource);
本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請
點擊舉報。