设计器外挂菜单保存报表模板到数据库中

第35章 设计器外挂菜单保存报表模板到数据库中

1 . 问题概述
1.报表模板在reportConfig.xml里的配置reportFileHome是支持绝对路径和相对路径的,通常我们都会把做好的报表模板以相对的路径随同WEB应用一起部署到中间件服务器上 (weblogic,websphere,tomcat),这样就会带来一个问题,如果哪天模板要修改或者加一些字段等操作,那就需要重新发布应用(tomcat可能不需要)到中间件服务器上,步骤比较繁琐.
2.当然对于上面的问题,我们可以利用中间文件夹的共享,以绝对的路径来存放报表模板(如:
C:/reportFiles下)
3.但如果服务器为WEB集群环境可能出现读不到共享磁盘的情况或根本就没有共享区域可以用,那么我们有必要把做好的报表模板保存在数据库中.以二进制流的方式存在. jsp页面用tag标签加载报表模板时,需要从数据库读取出来.


2 . 案例
浙江公众信息

3 .流程步骤
1. 在数据库创建raq表:
if exists (select * from dbo.sysobjects where id = object_id(N’[dbo].[raq]‘) and OBJECTPROPERTY(id, N’IsUserTable’) = 1)
drop table [dbo].[raq]
GO
CREATE TABLE [dbo].[raq] (
[id] [int] IDENTITY (1, 1) NOT NULL ,
[fileName] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[content] [image] NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO

2. 在润乾报表设计器的文件菜单下(systemconfig.xml)增加一个菜单选项可命名为:保存报表到数据库中

3. 在选择该菜单可以将正在编辑的报表直接保存到数据库中的二进制字段中

4. 报表模板保存时的名称,点击保存

5.控制台打印报表模板已保存成功


6.如果出现同一张在编辑报表重复保存,出现提示信息:

7. 数据库表出现保存记录

4. 解决思路
1. 编写 Java类实现润乾报表外挂菜单功能类
2. 在 JAVA类中重调用润乾报表的接口获取当前正在设计的报表的流
3. 将报表直接保存到数据库中


5. 程序说明
1.保存模板SaveReportToDB.java代码:

package api;

import java.awt.event.ActionEvent;

import java.io.ByteArrayInputStream;

import java.io.FileInputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.ObjectInputStream;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

import javax.swing.JOptionPane;

import com.runqian.report4.ide.configmenu.CMAction;

import com.runqian.report4.ide.dialog.DialogInputFile;

public class SaveReportToDB extends CMAction {

private final static String driver = “com.newatlanta.jturbo.driver.Driver”;

private final static String url = “jdbc:JTurbo://localhost/exercise/charset=GBK”;

private final static String username = “sa”;

private final static String password = “sa”;

/**

* 获取当前编辑的报表流

*

* @return InputStream 获取当前编辑的报表流

*/

private InputStream getCurrentReportStream() {

try {

return (InputStream) handler.processMessage(“getRaqInputStream”,

null);

} catch (Exception e) {

}

return null;

}

// 获得连接

private static Connection getConnection(String driver, String url,

String username, String password) {

try {

Class.forName(driver);

} catch (ClassNotFoundException e) {

e.printStackTrace();

}

Connection con = null;

try {

con = DriverManager.getConnection(url, username, password);

} catch (SQLException e) {

e.printStackTrace();

}

return con;

}

// 关闭连接

private static void closeCon(Connection con) {

try {

con.close();

} catch (SQLException e) {

e.printStackTrace();

}

}

// 将输入流保存到数据库

private static int insertRaqStream(String fileName, InputStream is,

Connection con) {

PreparedStatement pmt = null;

int bytes = 0;

if (is != null) {

try {

bytes = is.available();

} catch (IOException e) {

e.printStackTrace();

return 0;

}

try {

pmt = con.prepareStatement(“insert into raq values(‘”

+ fileName + “‘,?)”);

pmt.setBinaryStream(1, is, bytes);

pmt.executeUpdate();

} catch (SQLException e) {

e.printStackTrace();

return 0;

}

try {

pmt.close();

} catch (SQLException e) {

e.printStackTrace();

return 0;

}

try {

is.close();

} catch (IOException e) {

e.printStackTrace();

}

}

return 1;

}

// 检测文件名是否已存在

private int checkFileName(String fileName, Connection con) {

Statement smt = null;

ResultSet rs = null;

int countNum = 0;

boolean flag = true;

try {

smt = con.createStatement();

rs = smt.executeQuery(“select fileName from raq where fileName =’”

+ fileName + “‘”);

while (rs.next()) {

countNum++;

}

} catch (SQLException e) {

flag = false;

e.printStackTrace();

}

if (smt != null) {

try {

smt.close();

} catch (SQLException e) {

flag = false;

e.printStackTrace();

}

}

if (!flag) {

countNum = -1;

}

return countNum;

}

public void actionPerformed(ActionEvent arg0) {

InputStream is = getCurrentReportStream();

String fileName = “”;

Connection con = getConnection(driver, url, username, password);

if (is != null) {

DialogInputFile dif = new DialogInputFile(请输入报表名称, “”);

dif. show ();

if (dif.getOption() != JOptionPane.OK_OPTION) {

return;

}

fileName = dif.getFile();

fileName = fileName.indexOf(“.raq”) >= 0 ? fileName : fileName

+ “.raq”;

if (checkFileName(fileName, con) != -1

&& checkFileName(fileName, con) != 0) {

JOptionPane.showMessageDialog(null, 数据库表中已存在同名文件:” + fileName,

提示, JOptionPane.WARNING_MESSAGE);

return;

}

int result = insertRaqStream(fileName, is, con);

if (result == 1) {

System.out.println(文件已成功保存!!”);

} else {

System.out.println(文件保存失败!!”);

}

closeCon(con);

}

}

}