JasperReport 简单使用(数据库源为数据库)
一 创建数据库数据源
二 创建Report模板
1. 文件 -> 新建
2. 编辑SQL
3. 创建元素
三 生成报表
jasperreport.datasource.ConnectionProvider.java
package jasperreport.datasource; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class ConnectionProvider { private static String driverClassName ="com.mysql.jdbc.Driver"; private static String username="root"; private static String password="root"; private static String url="jdbc:mysql://localhost/mysql"; static{ try { Class.forName(driverClassName); } catch (ClassNotFoundException e) { throw new RuntimeException(e); } } public static Connection getConnection(){ try { return DriverManager.getConnection(url, username, password); } catch (SQLException e) { throw new RuntimeException(e); } } }
jasperreport.datasource.JasperReportWithDatasource.java
package jasperreport.datasource; import java.io.InputStream; import java.sql.Connection; import java.util.HashMap; import java.util.Map; import net.sf.jasperreports.engine.JRExporterParameter; import net.sf.jasperreports.engine.JasperFillManager; import net.sf.jasperreports.engine.JasperPrint; import net.sf.jasperreports.engine.export.JRTextExporter; import net.sf.jasperreports.engine.export.JRTextExporterParameter; public class JasperReportWithDatasource { public static void export() throws Exception{ InputStream inputStream = JasperReportWithDatasource.class.getResourceAsStream("DBReport.jasper"); Map<Object,Object> parameters = new HashMap<Object,Object>(); Connection connection = ConnectionProvider.getConnection(); JasperPrint jasperPrint = JasperFillManager.fillReport(inputStream, parameters, connection); JRTextExporter exporter = new JRTextExporter(); exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, "db.txt"); exporter.setParameter(JRTextExporterParameter.PAGE_WIDTH, 200); exporter.setParameter(JRTextExporterParameter.PAGE_HEIGHT, 100); exporter.exportReport(); } public static void main(String[] args) throws Exception{ export(); } }
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>JasperReport</groupId> <artifactId>JasperReport</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <dependency> <groupId>net.sf.jasperreports</groupId> <artifactId>jasperreports</artifactId> <version>3.7.2</version> </dependency> <dependency> <groupId>org.codehaus.groovy</groupId> <artifactId>groovy-all</artifactId> <version>1.7.5</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.13</version> </dependency> </dependencies> </project>
相关推荐
lbyd0 2020-11-17
sushuanglei 2020-11-12
腾讯soso团队 2020-11-06
gaobudong 2020-11-04
yangkang 2020-11-09
85477104 2020-11-17
KANSYOUKYOU 2020-11-16
wushengyong 2020-10-28
lizhengjava 2020-11-13
星月情缘 2020-11-13
huangxiaoyun00 2020-11-13
luyong0 2020-11-08
Apsaravod 2020-11-05
PeterChangyb 2020-11-05
wwwjun 2020-11-02
gyunwh 2020-11-02
EchoYY 2020-10-31
dingyahui 2020-10-30