水晶报表for java_水晶报表(crystal reports)--java
两天项目需要用到水晶报表(crystal reports),上网查阅了一些资料, 网上的相关资料有限.
1.安装
在使用时,请访问官网(https://www.sap.com/china/product/analytics/crystal-reports-eclipse.html)下载SAP Crystal Reports(Eclipse 版)插件并将其集成到Eclipse环境中;完成下载后需进行解压操作以方便后续配置与使用。

将文件放到eclipse相应的文件夹下。重启eclipse!
2.建项目
eclipse右键新建项目,选择Crystal Reports Web Project

输入项目名

finaish,项目结构展示如下:

3.建立java取数和数据库连接
有两种方式
第一种:
这个实例由 CrystalReport1.rpt 生成,在 Eclipse 中可以方便地进行数据访问和操作。
数据取出来不做任何修改的展示。
我在当前环境中采用POJO对象的方式来生成报表。为了满足日常中Java数据处理完成后展示报表的需求而采用该方法。
首先建立和数据库表一样名字的POJO Person.java
package com.businessobjects.samples;
import java.util.Date;
public class Person {
private int id;
private String t_name;
private String password;
private Date birthday;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getT_name() {
return t_name;
}
public void setT_name(String t_name) {
this.t_name = t_name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public Person() {
}
public Person(int id, String t_name, String password, Date birthday) {
super();
this.id = id;
this.t_name = t_name;
this.password = password;
this.birthday = birthday;
}
@Override
public String toString() {
return "Person [id=" + id + ", t_name=" + t_name + ", password="
+ password + ", birthday=" + birthday + "]";
}
}
建立与数据库连接的类
package com.businessobjects.samples;
import java.sql.Connection;
import java.sql.DriverManager;
public class DataBaseConnection {
private final String DBDRIBER ="com.mysql.jdbc.Driver";
private final String DatabaseUrl = "jdbc:mysql://IP:端口/数据库名称?(使用Unicode字符集; 字符编码= UTF-8)".
private final String DBUSER = "用户";
private final String DBPASSWORD = "密码";
private Connection conn= null;
public DataBaseConnection(){
try{
Class.forName(DBDRIBER);
this.conn = DriverManager.getConnection(DBURL, DBUSER, DBPASSWORD);
}catch(Exception e){
e.printStackTrace();
}
}
public Connection getConnection(){
return this.conn;
}
public void close(){
try{
this.conn.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
建立取数的DAO接口
package com.businessobjects.samples;
import java.util.List;
/**
-
取数据DAO接口
-
@author Scaler_Zhang
*/
public interface IPersonDao {
/**
-
添加操作
-
@param person 用户信息
-
@throws Exception
*/
public void insert(Person person)throws Exception;
/**
-
更新操作
-
@param person
-
@throws Exception
*/
public void update(Person person)throws Exception;
/**
-
删除操作
-
@param person
-
@throws Exception
*/
public void delete(Person person)throws Exception;
/**
-
根据ID查询操作
-
@param id
-
@return
-
@throws Exception
*/
public Person queryById(int id)throws Exception;
/**
-
查询全部
-
@return persons
-
@throws Exception
*/
public List queryAll() throws Exception;
/**
-
模糊查询
-
@param cond
-
@return persons
-
@throws Exception
*/
public List queryByLike(String cond)throws Exception;
}
对接口写实现。该处只做QUERYALL的实现。作为示例
package com.businessobjects.samples;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
public class PersonDao implements IPersonDao {
public void insert(Person person) throws Exception {
// TODO Auto-generated method stub
}
public void update(Person person) throws Exception {
// TODO Auto-generated method stub
}
public void delete(Person person) throws Exception {
// TODO Auto-generated method stub
}
public Person queryById(int id) throws Exception {
// TODO Auto-generated method stub
return null;
}
public List queryAll() throws Exception {
List all = new ArrayList();
String sql = "select * from person";
PreparedStatement stat = null;
DataBaseConnection dbc = null;
try{
//连接数据库
dbc = new DataBaseConnection();
stat = dbc.getConnection().prepareStatement(sql);
ResultSet rst = stat.executeQuery();
while(rst.next()){
Person person = new Person();
person.setId(rst.getInt(1));
person.setT_name(rst.getString(2));
person.setPassword(rst.getString(3));
person.setBirthday(rst.getDate(4));
all.add(person);
}
}catch(Exception e){
e.printStackTrace();
throw new Exception("查询出现异常");
}finally{
dbc.close();
}
return all;
}
public List queryByLike(String cond) throws Exception {
// TODO Auto-generated method stub
return null;
}
}
这样整体就写完了。
2)建立报表
右击项目选择

创建一个新的空白数据表。将蓝色标记的位置拖动至Data区域。

制作一张简单的报表,在右侧栏中选择要展示的字段,并将其拖放至指定的位置。


选择如下

OK,生成jsp代码如下
com.crystaldecisions.sdk.occa.report.application.OpenReportOptions,
CrystalDecisions.Sdk.OcCA.ReportApplication.ReportClient_Document
com.crystaldecisions.sdk.occa.report.lib.ReportSDKExceptionBase" %>
// This sample code calls methods from the CRJavaHelper class, which
// contains examples of how to use the BusinessObjects APIs. You are free to
redesign, restructure, or reorganize the source code within the CRJavaHelper class for distribution.
try {
String reportName = "Person.rpt";
通过 session 获取 reportName 的属性值并将其转换为 ReportClientDocument 对象
if (clientDoc == null) {
Report can be accessed from the relative path indicated by CRConfig.xml, as well as its local file path.
tag can be removed for opening the reports as Java resources or alternatively using an absolute path
// (absolute path not recommended for Web applications).
clientDoc = new ReportClientDocument();
报告客户端文档中的内部进程配置字符串被设置为报告应用服务器配置
// Open report
clientDoc.open(reportName, OpenReportOptions._openAsReadOnly);
// ****** BEGIN POPULATE WITH RESULTSET SNIPPET ****************
{
// This option is not applicable for the report you have chosen
}
// ****** END POPULATE WITH RESULTSET SNIPPET ****************
// ****** BEGIN POPULATE WITH POJO SNIPPET ****************
{
// This option is not applicable for the report you have chosen
}
// ****** END POPULATE WITH POJO SNIPPET ****************
// Store the report document in session
session.setAttribute(reportName, clientDoc);
}
// ****** BEGIN CONNECT CRYSTALREPORTPAGEVIEWER SNIPPET ****************
{
// Create the CrystalReportViewer object
晶报观众器创建一个新的晶报页面观众器。
String reportSourceSessionKey = reportName+"ReportSource";
Object reportSource = session.getAttribute(reportSourceSessionKey);
if (reportSource == null)
{
reportSource = clientDoc.getReportSource();
session.setAttribute(reportSourceSessionKey, reportSource);
}
// set the reportsource property of the viewer
crystalReportPageViewer.setReportSource(reportSource);
// Apply the viewer preference attributes
// Process the report
crystalReportPageViewer.handleHTTP请求(request, response, application, null);
}
// ****** END CONNECT CRYSTALREPORTPAGEVIEWER SNIPPET ****************
} catch (ReportSDKExceptionBase e) {
out.println(e);
}
%>
运行项目访问页面就可以看到展示的报表了
第二种:
