Eclipse 操作 Crystal Report 笔记

关于Eclipse 操作 Crystal Reporte笔记

1.安装

本人的环境是eclipse-jee-kepler-SR2-win32-x86_64 位版本。先从SAP官方下载 Eclipse支持Crystal report插件如下地址 http://download.csdn.net/detail/u011910756/7505827

 下载解压后把对应的文件拷贝到你Eclipse对应的文件夹下面. 

 2.建立项

	新建项目,在其他中选择 Crystal Reports Web Project。


输入项目名称-->Target runtime (我用的是tomcat7.0)-->Configuration(Crystal report web Project)



以上完成后,Eclipse会自带出如下项目结构
 

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 DBURL = "jdbc:mysql://IP:端口/数据库名称?useUnicode=true&characterEncoding=utf8";
	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;
	}

}

4.建立报表



建立一张空白报表。在报表视图里把下如蓝色标记的地方拖到 Data 的空白部分


设计报表的部分省略  
设计了一张空表。把POJO的框架引入过来了。接下来做数据和JSP展示。


选择如下


完成后修改自动生成的jsp文件,如下jsp代码
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon" />
<%@ page import="com.businessobjects.samples.CRJavaHelper,
com.businessobjects.samples.*,
com.crystaldecisions.report.web.viewer.CrystalReportViewer,
com.crystaldecisions.sdk.occa.report.application.OpenReportOptions,
com.crystaldecisions.sdk.occa.report.application.ReportClientDocument,
com.crystaldecisions.sdk.occa.report.lib.ReportSDKExceptionBase,
java.util.ArrayList,
java.util.List" %><%
	// This sample code calls methods from the CRJavaHelper class, which 
	// contains examples of how to use the BusinessObjects APIs. You are free to 
	// modify and distribute the source code contained in the CRJavaHelper class. 

	try {

		String reportName = "Person.rpt";
		ReportClientDocument clientDoc = (ReportClientDocument) session.getAttribute(reportName);

		if (clientDoc == null) {
			// Report can be opened from the relative location specified in the CRConfig.xml, or the report location
			// tag can be removed to open the reports as Java resources or using an absolute path
			// (absolute path not recommended for Web applications).

			clientDoc = new ReportClientDocument();
			clientDoc.setReportAppServer(ReportClientDocument.inprocConnectionString);
			
			// Open report
			clientDoc.open(reportName, OpenReportOptions._openAsReadOnly);

		
			// ****** BEGIN SET RUNTIME DATABASE CREDENTIALS ****************  
			{
				// This option is not applicable for the report you have chosen
			}
			// ****** END SET RUNTIME DATABASE CREDENTIALS **************** 		
		
		
			// ****** 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 ****************  
			{
				// **** POPULATE MAIN REPORT ****
				{

					 // Populate POJO data source
					 String className = "com.businessobjects.samples.Person";
					 
					 // Look up existing table in the report to set the datasource for and obtain its alias.  This table must
					 // have the same schema as the Resultset that is being pushed in at runtime.  The table could be created
					 // from a Field Definition File, a Command Object, or regular database table.  As long the Resultset
					 // schema has the same field names and types, then the Resultset can be used as the datasource for the table.
					 String tableAlias = "Person";

					 //Create a dataset based on the class com.businessobjects.samples.Person
					 //If the class does not have a basic constructor with no parameters, make sure to adjust that manually
				//********以下为修改部分*********
	 					List dataSet = new ArrayList();
							 IPersonDao dao = new PersonDao();
							 dataSet = dao.queryAll();
//					 dataSet.add(new com.businessobjects.samples.Person());
//					 dataSet.add(new com.businessobjects.samples.Person());
//					 dataSet.add(new com.businessobjects.samples.Person());
//					 dataSet.add(new com.businessobjects.samples.Person());
//					 dataSet.add(new com.businessobjects.samples.Person());
                               //*****以上为修改部分********
					 //Push the resultset into the report (the POJO resultset will then be the runtime datasource of the report)
					 CRJavaHelper.passPOJO(clientDoc, dataSet, className, tableAlias, "");
				}


			}
			// ****** END POPULATE WITH POJO SNIPPET ****************		
			
			
			// Store the report document in session
			session.setAttribute(reportName, clientDoc);

		}

				
		// ****** BEGIN CONNECT CRYSTALREPORTPAGEVIEWER SNIPPET ****************  
		{
			// Create the CrystalReportViewer object
			CrystalReportViewer crystalReportPageViewer = new CrystalReportViewer();

			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.processHttpRequest(request, response, application, null); 

		}
		// ****** END CONNECT CRYSTALREPORTPAGEVIEWER SNIPPET ****************		
	

	} catch (ReportSDKExceptionBase e) {
	    out.println(e);
	}
	
%>


主要是用DAO实现去取数,得到的List。给LIST指定给CRJavaHelper.passPOJO就可以了。 点击运行就该JSP在开始部署的TOMCAT 就能看到结果了。









评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值