为什么下面程序编译后,用appletviewer  test.htm 显示结果没有数据呢(我表中有数据)
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;
import java.sql.*;
import java.awt.*;
public class test extends JApplet
{public void init()
    {
JPanel panel=new JPanel();
getContentPane().add(panel);
AbstractTableModel tm;
Connection con;
Statement stat;
ResultSet rs;
//声明一个类AbstractTableModel对象
JTable jg_table;//声明一个类JTable对象
final Vector vect;//声明一个向量对象
JScrollPane jsp;//声明一个滚动杠对象
final String title[]={"学号","姓名","年龄"};//二维表列名 vect=new Vector();//实例化向量
tm=new AbstractTableModel()
{
  public int getColumnCount()
    {
    return title.length;
     }//取得表格列数
  public int getRowCount()
    {
     return vect.size();
     }//取得表格行数
  public Object getValueAt(int row,int column){
if(!vect.isEmpty())
return
 ((Vector)vect.elementAt(row)).elementAt(column);
else
return null;}//取得单元格中的属性值
public String getColumnName(int column){
return title[column];}//设置表格列名
public void setValueAt
(Object value,int row,int column){}
//数据模型不可编辑,该方法设置为空
public Class getColumnClass(int c){
return getValueAt(0,c).getClass();
}//取得列所属对象类
public boolean isCellEditable(int row,int column){
return false;}//设置单元格不可编辑,为缺省实现
};     jg_table=new JTable(tm);//生成自己的数据模型
 jg_table.setToolTipText("显示全部查询结果");
           //设置帮助提示
 jg_table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
           //设置表格调整尺寸模式
 jg_table.setCellSelectionEnabled(false);
           //设置单元格选择方式
 jg_table.setShowVerticalLines(true);//
          // 设置是否显示单元格间的分割线
 jg_table.setShowHorizontalLines(true);
 jsp=new JScrollPane(jg_table);//给表格加上滚动杠
 panel.add(jsp);
       try
           {
               Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");           con=DriverManager.getConnection("jdbc:odbc:stud","sa","");
           stat=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
            rs=stat.executeQuery("select * from stud");
            vect.removeAllElements();//初始化向量对象
tm.fireTableStructureChanged();//更新表格内容
while(rs.next())
{
    Vector rec_vector=new Vector();
    //从结果集中取数据放入向量rec_vector中
    rec_vector.addElement(rs.getString(1));
    rec_vector.addElement(rs.getString(2));
    rec_vector.addElement(Integer.toString(rs.getInt(3)));    
    vect.addElement(rec_vector);
    //向量rec_vector加入向量vect中
  }
    tm.fireTableStructureChanged();
    //更新表格,显示向量vect的内容                   rs.close();
   stat.close();
   con.close();
    } catch(Exception e)
           {}   }
}

解决方案 »

  1.   

    太长了,看不完,给你个例子研究去吧:JTableExample.java import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.util.*;
       
    public class JTableExample extends JFrame implements ActionListener
    {
        public static void main(String[] argv)
        {
            JTableExample mainApp = new JTableExample();
        }
        
        public JTableExample()
        {
            super("JTable Example");
            setBounds(0, 0, 450, 350);
            getContentPane().setLayout(null);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
                   
            table = new JTable(10, 3);
            table.setBounds(10, 10, 420, 200);   
            
            tableHandler = new TableHandler();
            table.setModel(tableHandler);
                   
            // Create three JTextField...
            textfield1 = new JTextField(15);
            textfield1.setLocation(10, 220);
            textfield1.setSize(textfield1.getPreferredSize());
            
            textfield2 = new JTextField(15);
            textfield2.setLocation(10, 250);
            textfield2.setSize(textfield2.getPreferredSize());
            
            textfield3 = new JTextField(15);
            textfield3.setLocation(10, 280);
            textfield3.setSize(textfield3.getPreferredSize());
            
            // Create the button...
            addButton = new JButton("Add Data to Table");
            addButton.setLocation(200, 220);
            addButton.setSize(addButton.getPreferredSize());
            
            // Add the action listeners...
            addButton.addActionListener(this);
                                       
            // Add the objects to the content pane...
            getContentPane().add(table);
            getContentPane().add(textfield1);
            getContentPane().add(textfield2);
            getContentPane().add(textfield3);
            getContentPane().add(addButton);
            
            setVisible(true);
        }
        
        public void actionPerformed(ActionEvent e)
        {
            if(e.getSource() == addButton)
            {
                // Check there is text in the 'textfield'
                if(textfield1.getText().compareTo("") != 0 &&
                   textfield2.getText().compareTo("") != 0 &&
                   textfield3.getText().compareTo("") != 0)
                {
                    // Then add the three fields to the JTable
                    tableHandler.addRowToTable(textfield1.getText(),
                                               textfield2.getText(),
                                               textfield3.getText());
                    
                    // Clear the textfields...
                    textfield1.setText("");
                    textfield2.setText("");
                    textfield3.setText("");
                }
            }
        }
        
        JTable table;
        TableHandler tableHandler;
        JTextField textfield1;
        JTextField textfield2;
        JTextField textfield3;
        JButton addButton;
    }
    TableHandler.java import javax.swing.table.*;
    import java.util.*;
       
    class TableHandler extends AbstractTableModel
    {
        public TableHandler()
        {
            dataRows = new Vector(); // set up the vector 
        }
        
        public void addRowToTable(String a, String b, String c)
        {
            String[] rowData = new String[3];
                               
            rowData[0] = a;
            rowData[1] = b;
            rowData[2] = c;
            
            dataRows.addElement(rowData);   // Add the data to a vector
                
            fireTableChanged(null); // Tell the table there is new data
        }
        
        public int getColumnCount()
        {
            return 3;
        }
        
        public int getRowCount()
        {
            if(dataRows != null)
            {
                return dataRows.size();
            }
            else
            {
                return -1;
            }
        }
       
        public Object getValueAt(int row, int column)
        {
            if(dataRows != null)
            {
                return ((String[])(dataRows.elementAt(row)))[column];
            }
            else
            {
                return null;
            }
        }
           
        private Vector dataRows;   // Vector to contain the rows of data
    }