点击查询这后,想把数据库中的内容显示到面板p1中,应该怎样做。package jiemian;
import java.awt.*;
import java.awt.event.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;import javax.swing.*;
public class StaffMainFrame extends JFrame{
protected static String 
                 driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";//连接数据库的驱动名
protected static String              dbName= "jdbc:sqlserver://localhost:1433;DatabaseName=staff_management_system";//数据库映射路径
protected static String dbUser = "jianxiang";   //数据库的登录名
protected static String dbPwd = "198937";    //数据库的登录密码
private static Connection conn = null ; 
 Panel p1;
 //  private static final long serialVersionUID = 1696099952059929396L;
   private static final JDesktopPane DESKTOP_PANE = new JDesktopPane();
  
   public StaffMainFrame() {
    super();
    
    Panel p1=new Panel();
    p1.setBounds(10, 50,450, 320);
    p1.setBackground(Color.white);
    add(p1);
    
    setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);    //设置窗口的关闭模式
    setLocationByPlatform(true);                                
    setSize(600,400);
    setTitle("员工查询系统");
    setResizable(false);
    
    Button b1=new Button("查询");
    b1.setBounds(500,320, 60, 20);
   b1.addActionListener(new BActionListener());
    add(b1);
         
    DESKTOP_PANE.setBackground(Color.DARK_GRAY);          //设置DESKTOP_PANE的背景色
    getContentPane().add(DESKTOP_PANE );                //将DESKTOP_PANE加入窗口
    setVisible(true);
      }
  class BActionListener implements ActionListener {  
   public void actionPerformed(final ActionEvent e) {
  
         try{ String[] colHeads={ "工号 ", "姓名 ", "性别","部门"}; 
Class.forName(driverName).newInstance(); 
Connection  con=DriverManager.getConnection(dbName,dbUser,dbPwd);
 Statement stmt=con.createStatement(); 
 ResultSet rs; 
rs=stmt.executeQuery( "SELECT count(*) as staff from Staff "); 
 rs.next();                  int iCount=rs.getInt( "staff"); 
                      Object[][] data=new Object[iCount][]; 
     int   i=0; 
rs=stmt.executeQuery( "SELECT staff_id,staff_name,staff_sex,staff_departid from Staff ");   while(rs.next()){ 
  data[i]=new Object[4]; 
  data[i][0]=rs.getString( "staff_id"); 
  data[i][1]=rs.getString( "staff_name"); 
  data[i][2]=rs.getString( "staff_sex"); 
  data[i][3]=rs.getString( "staff_departid");
  i++; 
  }    JTable table=new JTable(data,colHeads); 
  JScrollPane jsp=new JScrollPane(table); 
  p1.add(jsp);   
                     setDefaultCloseOperation(EXIT_ON_CLOSE);
  }catch(Exception e1){      
       } 
   
    
  } 
   }
}   

解决方案 »

  1.   


    import java.awt.BorderLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.util.Vector;import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.table.DefaultTableModel;
    public class Demo extends JFrame implements ActionListener {
    private JButton button;
    private Vector<Vector> vector;
    private Vector  columnNames;
    private JTable table;
    private JScrollPane jsp;
    private DefaultTableModel tableModel;

    public Demo() {
    this.setTitle("数据库查询示例");
    this.setSize(800,600);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setLayout(new BorderLayout());
    button = new JButton("查询");
    this.add(button, BorderLayout.NORTH);
    button.addActionListener(this);
    //调用方法初始化表格
    init();
    jsp = new JScrollPane(table);
    this.add(jsp);
    this.setVisible(true);
    }

    /**
     * 初始化表格
     */
    private void init() {
    //表头
    columnNames = new Vector();
    String[] columns = {"学号","姓名","性别","年龄","政治面貌","籍贯","专业"};
    for(int i=0; i<columns.length; i++) {
    columnNames.add(columns[i]);
    }
    //数据
    vector = new Vector<Vector>();
    tableModel = new DefaultTableModel(vector,columnNames);
    table = new JTable(tableModel);
    }



    public static void main(String[] args) {
    new Demo();
    } /**
     * 处理单击查询事件,用DefaultTableModel或继承AbstractTableModel自己实现模型
     * 更新数据的时候,更新模型的数据,然后重新设置表格的模型,这样表格就可以显示出更新后的数据
     */
    public void actionPerformed(ActionEvent e) {
    try {
    vector = Demo.query();
    tableModel = (DefaultTableModel)table.getModel();
    tableModel.setDataVector(vector, columnNames);
    table.setModel(tableModel);
    //下面几行可有可无,通常情况下写上就可以了
    table.validate();
    table.repaint();
    } catch (ClassNotFoundException e1) {
    JOptionPane.showMessageDialog(this, "数据库驱动加载错误!");
    } catch (SQLException e1) {
    JOptionPane.showMessageDialog(this, "数据库操作错误!");
    }
    }

    /**
     * 查询数据
     * @return 包含数据的Vector
     * @throws ClassNotFoundException 
     * @throws SQLException
     * @throws ClassNotFoundException
     * @throws SQLException 
     */
    public static Vector<Vector> query() throws ClassNotFoundException, SQLException {
    Vector<Vector> v = new Vector<Vector>();
    String sql = "select * from student";
    Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
    Connection conn = DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;dataBaseName=student","sa","sa");
    Statement stmt = conn.createStatement();
    ResultSet rs = stmt.executeQuery(sql);
    while(rs.next()) {
    Vector temp = new Vector();
    temp.add(rs.getString("code"));
    temp.add(rs.getString("name"));
    temp.add(rs.getInt("sex"));
    temp.add(rs.getInt("age"));
    temp.add(rs.getString("political"));
    temp.add(rs.getString("origin"));
    temp.add(rs.getString("professional"));
    v.add(temp);
    }
    rs.close();
    stmt.close();
    conn.close();
    return v;
    }
    }写的不好,欢迎各位指正!