Jasper动态报告 - 使用jasper报告生成报告错误

时间:2015-05-31 22:30:45

标签: java mysql report

我想从我的数据库生成报告。我正在研究jasper报告工具,我收到以下错误:

  Exception in thread "main" java.lang.Error: Unresolved compilation          problems:

    type cannot be resolved

    CenteredStyle cannot be resolved to a variable
    type cannot be resolved

    boldStyle cannot be resolved to a variable
    type cannot be resolved

    CenteredStyle cannot be resolved to a variable
    type cannot be resolved

    CenteredStyle cannot be resolved to a variable
    type cannot be resolved

    CenteredStyle cannot be resolved to a variable

    BigDecimal cannot be resolved to a type
    CenteredStyle cannot be resolved to a variable

    at project.Reports.main(Reports.java:36)

代码:

 package project;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import net.sf.dynamicreports.jasper.builder.JasperReportBuilder;
import net.sf.dynamicreports.report.builder.DynamicReports;
import net.sf.dynamicreports.report.builder.column.Columns;
import net.sf.dynamicreports.report.builder.column.TextColumnBuilder;
import net.sf.dynamicreports.report.builder.component.Components;
import net.sf.dynamicreports.report.builder.datatype.DataTypes;
import net.sf.dynamicreports.report.constant.HorizontalAlignment;
import net.sf.dynamicreports.report.exception.DRException;
import net.sf.dynamicreports.report.builder.style.StyleBuilder;
import net.sf.dynamicreports.report.builder.datatype.BigDecimalType;
import net.sf.dynamicreports.report.builder.group.ColumnGroupBuilder;

public class Reports {

  public static void main(String[] args) {
    Connection connection = null;
    try {
        Class.forName("com.mysql.jdbc.Driver");
        connection = DriverManager.getConnection("jdbc:mysql://hostname:3306/Project","root", "codingdevil");
    } catch (SQLException e) {
        e.printStackTrace();
        return;
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
        return;
    }

    JasperReportBuilder report = DynamicReports.report();
    Columns col;
    TextColumnBuilder<Integer> rowNumberColumn = Columns.reportRowNumberColumn("No.");
    TextColumnBuilder<java.util.Date> columndate = col.column("Date", "Date_Of_Sale", type.dateType()).setStyle(CenteredStyle);
    TextColumnBuilder<String>    columnItem = col.column("Item Name", "Item_Name", type.stringType()).setStyle(boldStyle);
    TextColumnBuilder<String>    columnCustomer = col.column("Customer Name", "Customer_name", type.stringType()).setStyle(CenteredStyle);
    TextColumnBuilder<Double>    columnunit = col.column("Unit Price", "Item_Price", type.doubleType()).setStyle(CenteredStyle);
    TextColumnBuilder<Integer>    columnqty = col.column("Qty", "Item_qty", type.integerType()).setStyle(CenteredStyle);
    TextColumnBuilder<BigDecimal>    columnsub = columnqty.multiply(columnunit).setTitle("Subtotal").setStyle(CenteredStyle);
    report
      .columns(
              rowNumberColumn,columnItem,columnCustomer,columndate,columnunit,columnqty,columnsub
              )
      .title(//title of the report
          Components.text("SimpleReportExample")
          .setHorizontalAlignment(HorizontalAlignment.CENTER))
          .pageFooter(Components.pageXofY())//show page number on the page footer
          .setDataSource("SELECT * FROM Holidays",connection);

    try {
                //show the report
        report.show();

                //export the report to a pdf file
        report.toPdf(new FileOutputStream("c:/report.pdf"));
    } catch (DRException e) {
        e.printStackTrace();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
  }
}

1 个答案:

答案 0 :(得分:1)

项目似乎缺少一些罐子,所以它无法理解这些词语的含义,试着检查一下你是否有所需的jar文件。

Try to use maven such as in this example.

将这些依赖项添加到您的pom.xml

<dependencies>
        <!-- DynamicReports -->
    <dependency>
        <groupId>net.sourceforge.dynamicreports</groupId>
        <artifactId>dynamicreports-core</artifactId>            
        <version>3.1.3</version>
    </dependency>

        <!-- MySQL database driver -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.25</version>
    </dependency>
</dependencies>