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; 
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);
}
public void userInit()
{
db.connect(); 
vCdata=db.getFieldNames("T_REGISTRATION"); 
dtm=new DefaultTableModel(); 
table1.setModel(dtm); 
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))); 
}
dtm.addRow(vRdata); 
}
db.close();
}catch(Exception ei)
{
System.out.println("error at jButton1_actionPerformed! in TestDatabase" + ei.toString());
}}
}
更新3行4列的数据:dtm.setValueAt("bigCat",3,4),也可以table1.setValueAt("bigCat",3,4).
得出3行4列数据:Object rs=dtm.getValueAt(3,4);
delete第3行数据:dtm.removeRow(3)