如何将数据库从数据库发送到dJ进行报告

时间:2010-02-27 14:02:13

标签: java jasper-reports dynamic-jasper

/ *     * DynamicJasper:通过指定动态创建报告的库     *运行时的列,组,样式等。它还节省了大量的开发     *在很多情况下的时间! (http://sourceforge.net/projects/dynamicjasper)     *     *版权所有(C)2008 FDV解决方案(http://www.fdvsolutions.com)     *     *这个库是免费软件;你可以重新分配它和/或     *根据GNU Lesser General Public的条款进行修改    *    *自由软件基金会发布的许可;或    *    *许可证的2.1版,或(根据您的选择)任何更高版本。    *    *这个库的发布是希望它有用,    *但没有任何担保;甚至没有暗示的保证    *    *适销性或特定用途的适用性。见GNU    *    *更少的通用公共许可证了解更多详情。    *    *您应该已收到GNU Lesser General Public的副本    *与此库一起许可;如果没有,请写信给自由软件    *    * 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 {                //连接C =新连接();                // C.Con();             CConnection C = new CConnection();             C.Connection();             声明stmt;             ResultSet rs = null;             String SQL =“SELECT * FROM student”;             stmt = C.Con()。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检索字段值时出错:net.sf.jasperreports.engine.data.JRAbstractBeanDataSource.getBeanProperty中的varchar(JRAbstractBeanDataSource.java:123)         at net.sf.jasperreports.engine.data.JRAbstractBeanDataSource.getFieldValue(JRAbstractBeanDataSource.java:96)         at net.sf.jasperreports.engine.data.JRBeanCollectionDataSource.getFieldValue(JRBeanCollectionDataSource.java:100)         at 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)         at 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'         在org.apache.commons.beanutils.PropertyUtilsBean.getSimpleProperty(PropertyUtilsBean.java:1322)         在org.apache.commons.beanutils.PropertyUtilsBean.getNestedProperty(PropertyUtilsBean.java:770)         在org.apache.commons.beanutils.PropertyUtilsBean.getProperty(PropertyUtilsBean.java:846)         在org.apache.commons.beanutils.PropertyUtils.getProperty(PropertyUtils.java:426)         at net.sf.jasperreports.engine.data.JRAbstractBeanDataSource.getBeanProperty(JRAbstractBeanDataSource.java:111)         ......还有12个 Java结果:1

1 个答案:

答案 0 :(得分:3)

您可能已经意识到这一点,但似乎在某处,代码试图访问Product对象上名为“varchar”的属性:

引起:java.lang.NoSuchMethodException:类'ar.com.fdvs.dj.test.domain.Product'类中的未知属性'varchar'.......

我不知道你的例子中发生了什么。可能有一个地方,名字是预期的,而你是传递类型。或者在您的配置中的某个地方,您已经切换了名称和类型,因此它正在寻找名为“varchar”的字段。这有意义吗?

通常,我发现错误日志的“由...引起”部分是最有用的。