使用 DynamicJasper 5.1.1 创建报告时,我从数据源获取结果集,如果生成后我的pdf为空,则不确定为什么我的pdf会为空。
我从here获得了代码参考。下面是我的代码:
import ar.com.fdvs.dj.core.DynamicJasperHelper;
import ar.com.fdvs.dj.core.layout.ClassicLayoutManager;
import ar.com.fdvs.dj.domain.DynamicReport;
import ar.com.fdvs.dj.domain.builders.FastReportBuilder;
import net.sf.jasperreports.engine.*;
import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
public class DynamicJDBCReportHandler {
static String username = "mnbfmwemf";
static String password = "fwfwfwf";
public static void main(String[] args) {
DynamicJDBCReportHandler drh = new DynamicJDBCReportHandler();
drh.getResultSet();
}
public Map<String, Object> getResultSet() {
Connection connection = null;
Statement statement = null;
JasperReport jreport;
JasperPrint jprint;
ArrayList<Object> list = new ArrayList<Object>();
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
connection = DriverManager.getConnection("jdbc:oracle:thin:@wfwfwf:1521:fwefw", username, password);
statement = connection.createStatement();
String query = "SELECT * FROM USER ORDER BY CREATE_DATE";
ResultSet rs = statement.executeQuery(query);
JRResultSetDataSource dataSource = new JRResultSetDataSource(rs); // Here associate the resultSet with JasperReport
FastReportBuilder drb = new FastReportBuilder();
DynamicReport report = drb.build();
try {
jreport = DynamicJasperHelper.generateJasperReport(report, new ClassicLayoutManager(), new HashMap());
jprint = JasperFillManager.fillReport(jreport, new HashMap<>(), dataSource);
JasperExportManager.exportReportToPdfFile(jprint, "/report.pdf");
rs.close();
} catch (JRException e) {
e.printStackTrace();
}
}catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
} finally {
try {
if (statement != null) {
statement.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
return null;
}
}
我不想创建 .jrxm 文件或 .jasper 文件,如果您在我做错事时可以指出我,我相信有人应该这样做,非常感谢您的帮助。
答案 0 :(得分:0)
您忘记借助FastReportBuilder来声明报告的结构。您使用以下代码行创建了空白报告:DynamicReport report = drb.build();
。
正确的示例:
public void buildReport() {
String query = "SELECT * FROM my_table";
try (Connection connection = DriverManager.getConnection(getConnectionString(), user, password);
Statement statement = connection.createStatement();
ResultSet rs = statement.executeQuery(query)) {
JRResultSetDataSource dataSource = new JRResultSetDataSource(rs);
DynamicReport report = new FastReportBuilder().addColumn("Column1", "column_1", String.class.getName(), 120).setTitle("Sample report").build();
JasperReport jasperReport = DynamicJasperHelper.generateJasperReport(report, new ClassicLayoutManager(), new HashMap());
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, new HashMap<>(), dataSource);
JasperExportManager.exportReportToPdfFile(jasperPrint, "./report.pdf");
} catch (SQLException | JRException | ClassNotFoundException e) {
log.error("Failed to build report", e);
}
}
我添加了 Column1 列,并使用以下代码行添加了报告标题:
DynamicReport report = new FastReportBuilder().addColumn("Column1", "column_1", String.class.getName(), 120).setTitle("Sample report").build();