我想从我的数据库中生成一份报告。我正在使用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();
}
}
}发布于 2015-12-03 09:36:47
该项目似乎缺少了一些jar,因此它无法理解这些单词的含义,请尝试检查是否有您需要的所有jar文件。
尝试使用maven,如本例所示。
将这些依赖项添加到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>https://stackoverflow.com/questions/30563380
复制相似问题