润乾设计器外挂菜单展现数据库中保存的报表
第36章 设计器外挂菜单展现数据库中保存的报表
1 . 问题概述
同设计器外挂菜单保存报表模板到数据库中
2 . 案例
浙江公众信息
3 .流程步骤
1. 在润乾报表设计器中文件菜单(systemconfig.xml)中外挂菜单,可命名为:展示数据库中报表
2.选中从数据库选出报表菜单:
3.打开润乾报表设计器选择展示数据库中报表,弹出界面可选择报表的编号及预览效果
4. 选择报表编号点击确定可以在设计器中打开对应得报表,修改之后可以再回填数据库
4. 解决思路
1. 编写Java类实现润乾报表外挂菜单功能类
2. 在Java类中实现读取数据库中报表编号功能,同时实现通过报表编号读取数据库中报表的
功能
3. 调用润乾报表接口展示报表
4. 配置外挂菜单到润乾报表设计器中
5. 程序说明
1.定义Swing界面,连接数据库,执行报表列表和报表的浏览, GetRecordsFromTable.java代码:
package api;
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.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
import javax.swing.JPanel;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import com.runqian.report4.usermodel.CSReport;
import com.runqian.report4.usermodel.IReport;
import com.runqian.report4.util.ReportUtils;
import javax.swing.JButton;
public class GetRecordsFromTable extends JDialog {
private static final long serialVersionUID = 1L;
private JPanel jContentPane = null;
private JLabel jLabel_bblb = null;
private JScrollPane jScrollPane = null;
private JTree jTree = null;
private JLabel jLabel_bbyl = null;
private JScrollPane jScrollPane1 = null;
private JPanel jPanel = null;
private JButton jButton_gb = null;
private JButton jButton_qd = null;
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”;
private Connection con = null;
private Map smap = new HashMap();
private InputStream is;
private String fileName = “”;
public int flag = 0;
public int getFlag() {
return flag;
}
public void setFlag(int flag) {
this.flag = flag;
}
public InputStream getIs() {
return is;
}
public void setIs(InputStream is) {
this.is = is;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
/**
* This is the default constructor
*/
public GetRecordsFromTable() {
super();
initialize();
}
/**
* This method initializes this
*
* @return void
*/
private void initialize() {
con getConnection(driver, url, username, password);
this.setSize(870, 590);
this.setTitle(“选出报表“);
this.setContentPane(getJContentPane());
}
/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
jLabel_bbyl = new JLabel();
jLabel_bbyl.setBounds(new java.awt.Rectangle(218, 15, 68, 18));
jLabel_bbyl.setText(“报表预览“);
jLabel_bblb = new JLabel();
jLabel_bblb.setBounds(new java.awt.Rectangle(21, 15, 70, 18));
jLabel_bblb.setText(“报表列表“);
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.add(jLabel_bblb, null);
jContentPane.add(getJScrollPane(), null);
jContentPane.add(jLabel_bbyl, null);
jContentPane.add(getJScrollPane1(), null);
jContentPane.add(getJButton(), null);
jContentPane.add(getJButton1(), null);
}
return jContentPane;
}
/**
* This method initializes jScrollPane
*
* @return javax.swing.JScrollPane
*/
private JScrollPane getJScrollPane() {
if (jScrollPane == null) {
jScrollPane = new JScrollPane();
jScrollPane.setBounds(new java.awt.Rectangle(22, 35, 172, 440));
jScrollPane.setViewportView(getJTree());
}
return jScrollPane;
}
/**
* This method initializes jTree
*
* @return javax.swing.JTree
*/
private JTree getJTree() {
Statement smt = null;
ResultSet rs = null;
int rowNum = 0;
Hashtable h = new Hashtable();
try {
smt = con.createStatement();
rs = smt.executeQuery(“select count(*) as num from raq “);
while (rs.next()) {
rowNum = rs.getInt(“num”);
}
} catch (SQLException e) {
e.printStackTrace();
}
String[] s = new String[rowNum];
int i = 0;
try {
rs = smt.executeQuery(“select id,fileName from raq “);
while (rs.next()) {
s[i] = rs.getString(“fileName”);
smap.put(s[i], String.valueOf(rs.getInt(“id”)));
i++;
}
} catch (SQLException e) {
e.printStackTrace();
}
h.put(“raq”, s);
try {
if (smt != null) {
smt.close();
}
if (rs != null) {
rs.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
if (jTree == null) {
jTree = new JTree(h);
jTree.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent e) {
if (e.getClickCount() == 2
&& !((JTree) e.getSource())
.getLastSelectedPathComponent().toString()
.equals(“raq”)) {
fileName = ((JTree) e.getSource())
.getLastSelectedPathComponent().toString();
Statement smt = null;
ResultSet rs = null;
IReport rd = null;
int id = 0;
InputStream isForView = null;
id = Integer.parseInt((String) smap.get(((JTree) e
.getSource()).getLastSelectedPathComponent()
.toString()));
try {
smt = con.createStatement();
rs = smt
.executeQuery(“select content from raq where id= “
+ id);
while (rs.next()) {
is = (InputStream) rs
.getBinaryStream(“content”);
isForView = (InputStream) rs
.getBinaryStream(“content”);
}
if (isForView != null) {
try {
rd = ReportUtils.read(isForView);
} catch (Exception e2) {
e2.printStackTrace();
}
}
CSReport cSReport = new CSReport(rd);
jScrollPane1.remove(jPanel);
JPanel jPanel = new JPanel();
try {
jPanel.add(cSReport.getDisplayPane());
} catch (Throwable e1) {
e1.printStackTrace();
}
jScrollPane1.setViewportView(jPanel);
try {
isForView.close();
} catch (IOException e1) {
e1.printStackTrace();
}
} catch (SQLException e1) {
e1.printStackTrace();
}
}
}
});
}
return jTree;
}
/**
* This method initializes jScrollPane1
*
* @return javax.swing.JScrollPane
*/
private JScrollPane getJScrollPane1() {
if (jScrollPane1 == null) {
jScrollPane1 = new JScrollPane();
jScrollPane1.setBounds(new java.awt.Rectangle(218, 34, 623, 439));
jScrollPane1.setViewportView(getJPanel());
}
return jScrollPane1;
}
/**
* This method initializes jPanel
*
* @return javax.swing.JPanel
*/
private JPanel getJPanel() {
if (jPanel == null) {
jPanel = new JPanel();
}
return jPanel;
}
/**
* This method initializes jButton
*
* @return javax.swing.JButton
*/
private JButton getJButton() {
if (jButton_gb == null) {
jButton_gb = new JButton();
jButton_gb.setBounds(new java.awt.Rectangle(231, 512, 89, 28));
jButton_gb.setText(“关闭“);
jButton_gb.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
flag = 1;
if (con != null) {
try {
con.close();
} catch (SQLException e1) {
e1.printStackTrace();
}
}
dispose();
}
});
}
return jButton_gb;
}
private static Connection getConnection(String driver, String url,
String username, String password) {
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
}
Connection con = null;
try {
con = DriverManager.getConnection(url, username, password);
} catch (SQLException e) {
e.printStackTrace();
}
return con;
}
/**
* This method initializes jButton1
*
* @return javax.swing.JButton
*/
private JButton getJButton1() {
if (jButton_qd == null) {
jButton_qd = new JButton();
jButton_qd.setBounds(new java.awt.Rectangle(486, 512, 89, 27));
jButton_qd.setText(“确定“);
jButton_qd.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
flag = 2;
if (con != null) {
try {
con.close();
} catch (SQLException e1) {
e1.printStackTrace();
}
}
dispose();
}
});
}
return jButton_qd;
}
}
2. 选择报表编号点击确定在设计器中打开对应得报表GetReportListFromDB.java代码:
package api;
import java.awt.event.ActionEvent;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.util.HashMap;
import com.runqian.report4.ide.configmenu.CMAction;
import com.runqian.report4.usermodel.IReport;
import com.runqian.report4.util.ReportUtils;
public class GetReportListFromDB extends CMAction {
/**
* 要保存报表时从主程序获取当前编辑的报表对象
*
* @return IReport 要保存报表时从主程序获取当前编辑的报表对象
*/
public IReport getCurrentReportObj() {
try {
return (IReport) handler.processMessage(“HttpSave”, null);
} catch (Exception e) {
}
return null;
}
/**
* 打开报表
*
* @param reportStream
*报表流
* @param reportName
*报表名称
* @return boolean 成功打开报表返回true遭遇异常返回false
*/
public boolean openRemoteReport(InputStream reportStream, String reportName) {
try {
HashMap hm = new HashMap();
hm.put(“stream”, reportStream);
hm.put(“filename”, reportName);
handler.processMessage(“httpopen”, hm);
return true;
} catch (Exception e) {
}
return false;
}
/**
* 获取当前编辑的报表流
*
* @return InputStream 获取当前编辑的报表流
*/
private InputStream getCurrentReportStream() {
try {
return (InputStream) handler.processMessage(“getRaqInputStream”,
null);
} catch (Exception e) {
}
return null;
}
public void actionPerformed(ActionEvent arg0) {
InputStream is = getCurrentReportStream();
if (is != null) {
GetRecordsFromTable grdft = new GetRecordsFromTable();
grdft.setModal(true);
grdft.
show
();
if (grdft.getFlag() == 2) {
openRemoteReport(grdft.getIs(), grdft.getFileName());
try {
grdft.getIs().close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}