/* * DynamicJasper:用於在運行時通過指定 *列,組,樣式等動態創建報告的庫。在很多情況下,它也節省了大量的開發時間! (http://sourceforge.net/projects/dynamicjasper) * * Copyright(C)2008 FDV Solutions(http://www.fdvsolutions.com) * *此庫爲免費軟件;你可以重新發布它和/或 *根據GNU Lesser General Public條款對其進行修改 * *自由軟件基金會發布的許可證;或許可的 * *版本2.1,或(根據您的選擇)任何更新的版本。 * *這個庫分發,希望它會有用, *但沒有任何擔保;沒有暗含的保證 * *適銷性或特定用途的適用性。請參閱GNU * *更低的通用公共許可證瞭解更多詳情。 * *您應該收到一份GNU Lesser General Public *許可證以及此庫;如果沒有,請寫信給自由軟件 * *基金公司,51街富蘭克林五樓,波士頓,MA 02110-1301 USA * * */如何將屬性從數據庫發送到dJ以生成報告
package ar.com.fdvs.dj.test;
import java.sql.*;
import java.awt.Color;
import java.util.Date;
import java.util.Locale;
import net.sf.jasperreports.view.*;
import ar.com.fdvs.dj.domain.AutoText;
import ar.com.fdvs.dj.domain.DynamicReport;
import ar.com.fdvs.dj.domain.Style;
import ar.com.fdvs.dj.domain.builders.FastReportBuilder;
import ar.com.fdvs.dj.domain.builders.StyleBuilder;
import ar.com.fdvs.dj.domain.constants.Font;
import ar.com.fdvs.dj.core.DJConstants;
// import ar.com.fdvs.dj.test.*;
public class Main extends BaseDjReportTest {
公共DynamicReport buildReport()拋出異常{//連接C =新連接(); // C.Con();連接C =新CC連接(); C.Connection(); 聲明stmt; ResultSet rs = null; String SQL =「SELECT * FROM student」; ()。createStatement(); rs = stmt.executeQuery(SQL); String res =「」;
FastReportBuilder drb = new FastReportBuilder();
drb.setQuery(SQL, DJConstants.QUERY_LANGUAGE_SQL);
while (rs.next()){
res= rs.getString("Name");
**drb.addColumn("Name","Name", String.class.getName(),30);**
// drb.addc
}
//.addColumn("Branch", "branch", String.class.getName(),30)
// .addColumn("Item", "item", String.class.getName(),50)
// .addColumn("Item Code", "id", Long.class.getName(),30,true)
// .addColumn("Quantity", "quantity", Long.class.getName(),60,true)
// .addColumn("Amount", "amount", Float.class.getName(),70,true)
drb.addGroups(2);
DynamicReport sa =drb.build();
drb.setSubtitle("This report was generated at " + new Date())
.setTemplateFile("templates/TemplateReportTest.jrxml")
.setUseFullPageWidth(true);
Style atStyle = new StyleBuilder(true).setFont(Font.COMIC_SANS_SMALL).setTextColor(Color.red).build();
Style atStyle2 = new StyleBuilder(true).setFont(new Font(9, Font._FONT_TIMES_NEW_ROMAN, false, true, false)).setTextColor(Color.BLUE).build();
/***
* Adding many autotexts in the same position (header/footer and aligment) makes them to be one on top of the other
*/
//First add in the FOOTER
drb.addAutoText(AutoText.AUTOTEXT_PAGE_X, AutoText.POSITION_HEADER, AutoText.ALIGNMENT_LEFT,200,40, atStyle);
drb.addAutoText("Autotext below Page counter", AutoText.POSITION_FOOTER, AutoText.ALIGNMENT_LEFT);
//Note the styled text: <b>msimone</b>, valid tags are: <b>, <i> and <u>
drb.addAutoText("Created by <b>msimone</b>", AutoText.POSITION_FOOTER, AutoText.ALIGNMENT_RIGHT,200);
drb.addAutoText(AutoText.AUTOTEXT_PAGE_X_SLASH_Y, AutoText.POSITION_FOOTER, AutoText.ALIGNMENT_RIGHT,30,30,atStyle2);
drb.addAutoText(AutoText.AUTOTEXT_CREATED_ON, AutoText.POSITION_FOOTER, AutoText.ALIGNMENT_LEFT,AutoText.PATTERN_DATE_DATE_TIME);
//Now in HEADER
drb.addAutoText(AutoText.AUTOTEXT_PAGE_X_OF_Y, AutoText.POSITION_HEADER, AutoText.ALIGNMENT_LEFT,100,40);
drb.addAutoText("Autotext at top-left", AutoText.POSITION_HEADER, AutoText.ALIGNMENT_LEFT,200);
drb.addAutoText("Autotext at top-left (2)", AutoText.POSITION_HEADER, AutoText.ALIGNMENT_LEFT,200);
drb.addAutoText("Autotext at top-center", AutoText.POSITION_HEADER, AutoText.ALIGNMENT_CENTER,200,atStyle);
// DynamicReport dr = drb.build();
//i18N, you can set a Locale, different tha n the default in the VM
drb.setReportLocale(new Locale("es","AR"));
drb.setReportLocale(new Locale("pt","BR"));
drb.setReportLocale(new Locale("fr","FR"));
return sa;
}
public static void main(String[] args) throws Exception {
**Main test = new Main();
test.testReport();**
JasperViewer.viewReport(test.jp);
JasperDesignViewer.viewReportDesign(test.jr);
//JasperDesignViewer.viewReportDesign(DynamicJasperHelper.generateJasperReport(test.dr, test.getLayoutManager(),new HashMap()));
}
}
編寫這些代碼即時得到以下異常,我真的找不出原因
異常線程 「main」 net.sf.jasperreports .engine.JRException:從bean中檢索字段值時出錯:var net at net.sf.jasperreports.engine.data.JRAbstractBeanDataSource.getBeanProperty(JRAbstractBeanDataSource.java:123) 在net.sf.jasperreports.engine.data.JRAbstractBeanDataSource.getFieldValue(JRAbstractBeanDataSource.java:96) 在net.sf.jasperreports.engine.data.JRBeanCollectionDataSource.getFieldValue(JRBeanCollectionDataSource.java:100) 在net.sf. jasperreports.engine.fill.JRFillDataset.setOldValues(JRFillDataset.java:818) at net.sf.jasperreports.engine.fill.JRFillDataset.next(JRFillDataset.java:782) at net.sf.jasperreports.engine.fill。 JRbaseFiller.next(JRBaseFiller.java:1448) at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReport(JRVerticalFiller.java:108) at net.sf.jasperreports.engine.fill。JRBaseFiller.fill(JRBaseFiller.java:923) at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:845) at net.sf.jasperreports.engine.fill.JRFiller.fillReport(JRFiller。的java:85) 在net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:624) 在ar.com.fdvs.dj.test.BaseDjReportTest.testReport(BaseDjReportTest.java:93) 在AR。 com.fdvs.dj.test.Main.main(Main.java:121) 引起:java.lang.NoSuchMethodException:類'class ar.com.fdvs.dj.test.domain.Product上的未知屬性'varchar' ' at org.apache.commons.beanutils.PropertyUtilsBean.getSimpleProperty(PropertyUtilsBean.java:1322) at org.apache.commons.beanutils.PropertyUtilsBean.getNestedProperty(PropertyUtilsBean.java:770) at org.apache.commons.beanutils.PropertyUtilsBean.getProperty(PropertyUtilsBean.java:846) at org.apache.commons.beanutils.PropertyUtils.getProperty(PropertyUtils.java:426) at net.sf.jasperreports.engine。 data.JRAbstractBeanDataSource.getBeanProperty(JRAbstractBeanDataSource.java:111) ...... 12多個 Java結果:1