当前位置: 首页 > 图文教程 > 网络编程 > JSP > 用Swing实现数据表格功能

JSP
我认为JSP有问题(上)
我认为JSP有问题(下)
jsp“抓”网页代码的程序
关于在bean里面打印html的利弊看法
bean里面如何打印到html页面
jdbc3中的RowSet 接口规范
Apusic Application Server1.0中jsp源代码泄漏漏洞
Unify的eWave ServletExec拒绝服务漏洞
通过提交超长的GET请求导致IBM HTTP Server远程溢出
在HTTP请求中添加特殊字符导致暴露JSP源代码文件
Resin 1.2 重要源代码暴露漏洞
多中WEB服务器的通用JSp源代码暴露漏洞
Tomcat 暴露JSP文件内容
IBM WebSphere Application Server 暴露JSP文件内容
JRun 2.3.x 范例文件暴露站点安全信息
BEA WebLogic 暴露源代码漏洞
IBM WebSphere Application Server 3.0.2 存在暴露源代码漏洞
Tomcat 3.1 存在暴露网站路径问题
Sun Java Web Server 能让攻击者远程执行任意命令
Netscape 修复 JAVA 安全漏洞

JSP 中的 用Swing实现数据表格功能


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-11-02   浏览: 44 ::
收藏到网摘: n/a

最近有个项目是delphi来做的,之中有很多地方要用到数据表格。这种功能在delphi和vb中能很方便的做出来,java没

有提供这项功能,但是可以用Jtable 来实现,不过就是麻烦了点:)。

下面是我用applet实现的一个简单数据表格程序代码。
package com.applet.cat10;

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
import com.util.*;
import java.util.*;
import javax.swing.table.*;
import java.sql.*;

/**
* Title: Cat工程
* Description: BCat
* Copyright: Copyright (c) 2001
* Company: smartcomm
* @author daniel
* @version 1.0
*/

public class TestDatabase extends JApplet {
boolean isStandalone = false;
JButton jButton1 = new JButton(); //触发时间查询按扭
Database db=new Database();
JTable table1 = new JTable();
JScrollPane scroll = new JScrollPane();
JTextField text1 = new JTextField();

DefaultTableModel dtm; //定义表格的数据模型
Vector vCdata=null; //定义表的列名(以vector存储)
ResultSet rsRow=null; //查询表的数据集合

/**Construct the applet*/
public TestDatabase() {
}
/**Initialize the applet*/
public void init() {
try {
jbInit();
userInit(); //自定义的操作都在次方法中
}
catch(Exception e) {
e.printStackTrace();
}
}
/**Component initialization*/
private void jbInit() throws Exception {
jButton1.setText("jButton1");
jButton1.setBounds(new Rectangle(26, 225, 79, 29));
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton1_actionPerformed(e);
}
});
this.setSize(new Dimension(664, 300));
this.getContentPane().setLayout(null);
scroll.setBounds(new Rectangle(12, 24, 644, 189));
text1.setBounds(new Rectangle(16, 271, 365, 22));
this.getContentPane().add(scroll, null);
this.getContentPane().add(jButton1, null);
// this.getContentPane().add(text1, null);
scroll.getViewport().add(table1, null);
}

/*userInit() 数据表格的初始化*/
public void userInit()
{
db.connect(); //数据库连接
vCdata=db.getFieldNames("T_REGISTRATION"); //得出列名(vector存储)
dtm=new DefaultTableModel(); //定义模式
table1.setModel(dtm); //设定表模式

/**for 列出标题烂**/
for(int i=0;i<vCdata.size();i++)
dtm.addColumn((String)vCdata.elementAt(i));

rsRow=db.executeQueryTable("T_REGISTRATION"); //得出数据集合

}

/**Start the applet*/
public void start() {
}
/**Stop the applet*/
public void stop() {
}

/**Destroy the applet*/
public void destroy() {
}
//static initializer for setting look & feel
static {
try {
//UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
//UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
}
catch(Exception e) {
}
}

//触发查询事件
void jButton1_actionPerformed(ActionEvent e)
{

try
{

while(rsRow.next())
{
Vector vRdata=new Vector();
for(int i=0;i<vCdata.size();i++)
{
vRdata.addElement(rsRow.getString((String)vCdata.elementAt(i))); //列举列数据(vector存

储)
}
dtm.addRow(vRdata); //向表中添家数据
}


db.close();
}catch(Exception ei)
{
System.out.println("error at jButton1_actionPerformed! in TestDatabase" + ei.toString());
}

}
}

这个程序简单的实现了数据表格功能,初始化状态是数据表格只有标题,当click 按扭显示数据。

1。在userInit()方法中做了数据表格的初始化,关键的方法:
建立表格模型: DefaultTableModel dtm=new DefaultTableModel(),
设定表模型:table1.setModel(dtm)
设定表格标题:(一切操作可以在模型上做) dtm.addColumn(Vector arg),arg是表的标题,也就是field名字数组
得出数据的结果集合:rsRow=db.executeQueryTable("T_REGISTRATION"),这就是一般的数据查询结果哦

2。在 void jButton1_actionPerformed(ActionEvent e)中显示数据,关键方法:
以行的方式加入数据:dtm.addRow(Vector arg)。

3。Jtable有很多的实例化方式,这里采用的是DefaultTableModel模型,对表的操作都可以建立在模型上。

4。以后如