通过 DynamicJasper 使用没有实体的 jdbc 连接生成报告
Generating report using jdbc connection without entities via DynamicJasper
使用 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 文件,我相信有人应该这样做,如果你当我做错的时候可以指出我,非常感谢你的帮助。
怎么了?
您忘记在 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();
更多信息:
使用 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 文件,我相信有人应该这样做,如果你当我做错的时候可以指出我,非常感谢你的帮助。
怎么了?
您忘记在 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();