/* * DynamicJasper:通过在运行时指定列、组、样式等来动态创建报告的库。在很多情况下也节省了大量的开发*时间!( http://sourceforge.net/projects/dynamicjasper ) * * 版权所有 (C) 2008 FDV Solutions ( http://www.fdvsolutions.com) * * 这个库是免费软件;您可以重新分发它和/或 * 根据 GNU Lesser General Public 的条款对其进行修改 * * 由自由软件基金会发布的许可证;* * 许可证的 2.1 版,或(由您选择)任何更高版本。* * 分发这个库是希望它有用,* 但没有任何保证;甚至没有 * * 适销性或适合特定用途的默示保证。有关详细信息,请参阅 GNU * * Lesser General Public License。* * 您应该已经收到了一份 GNU Lesser General Public * 许可证的副本以及这个库;如果没有,请写信给 Free Software * * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * * */
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 {
public DynamicReport buildReport() throws Exception { // Connection C = new Connection(); // C.Con(); CConnection C= 新 CConnection(); C.连接();语句 stmt; 结果集 rs = null; String SQL = "SELECT * FROM student"; stmt = C.Con().createStatement(); rs = stmt.executeQuery(SQL); 字符串 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()));
}
}
写这段代码我得到以下异常,我真的不知道原因
线程“主”net.sf.jasperreports.engine.JRException 中的异常:从 bean 检索字段值时出错:net.sf.jasperreports.engine.data.JRAbstractBeanDataSource.getBeanProperty 的 varchar(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) 在 net.sf.jasperreports.engine.fill.JRFillDataset.next(JRFillDataset.java:782) 在 net.sf.jasperreports.engine.fill.JRBaseFiller.next(JRBaseFiller.java:1448)在 net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReport(JRVerticalFiller.java:108) 在网上。sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:923) 在 net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:845) 在 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 上的未知属性 'varchar' .Product' 在 org.apache.commons.beanutils.PropertyUtilsBean.getNestedProperty(PropertyUtilsBean.java:770) 在 org.apache.commons.PropertyUtilsBean.getSimpleProperty(PropertyUtilsBean.java:1322) 在 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