JasperReport 简单使用(数据库源为数据库)

一 创建数据库数据源

JasperReport 简单使用(数据库源为数据库)_第1张图片

JasperReport 简单使用(数据库源为数据库)_第2张图片
 

JasperReport 简单使用(数据库源为数据库)_第3张图片
 

JasperReport 简单使用(数据库源为数据库)_第4张图片
 
 二 创建Report模板

1. 文件 -> 新建

2. 编辑SQL
JasperReport 简单使用(数据库源为数据库)_第5张图片


JasperReport 简单使用(数据库源为数据库)_第6张图片
 

3. 创建元素
JasperReport 简单使用(数据库源为数据库)_第7张图片
 

三 生成报表

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>

你可能感兴趣的:(template,database,IREPORT,jasperReport)