启动类:
import java.awt.*;
import javax.swing.*;
import java.sql.*;
import java.awt.event.*;
import javax.swing.table.*;
import java.util.*;
class Demo extends JFrame implements ActionListener
{
private ResultSet rs;
JButton jb=null;
JPanel jp1,jp2;
JScrollPane jsp=new JScrollPane();
JTable jtb;
Vector row=new Vector();
Vector col=new Vector();
DefaultTableModel dtm=new DefaultTableModel();
public Demo()
{
jtb=new JTable(dtm);
jtb.setPreferredScrollableViewportSize(new Dimension(200,200));
jsp.add(jtb);
jb=new JButton( "ok ");
jb.addActionListener(this);
jp1=new JPanel();
jp2=new JPanel();
jp2.setPreferredSize(new Dimension(300,300));
jsp.setPreferredSize(new Dimension(300,300));
jp2.setBackground(Color.red);
jp2.setLayout(new BorderLayout());
jp1.add(jb);
this.add(jp1, "North ");
this.add(jp2);
this.add(jsp);
this.setSize(400,500);
this.show();
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==jb)
{
ConDatabase base=new ConDatabase( "select * from mer ");
rs=base.getrs();
try
{
ResultSetMetaData rsmd=rs.getMetaData();
for (int i = 1; i <=rsmd.getColumnCount(); i++)
{
col.add(rsmd.getColumnName(i));
}
while(rs.next())
{
Vector v=new Vector();
for (int i = 1; i <=6; i++)
{
v.add(rs.getString(i));
}
row.add(v);
}
dtm.setDataVector(row,col);
System.out.println ( "ddddddddd ");
}
catch (Exception ex)
{
System.out.println (ex.getMessage());