table.setVisible(true)不显示我的表

时间:2013-11-26 19:26:00

标签: swing user-interface netbeans jdbc

我不知道为什么但桌子没有显示。值将被添加到mysql的基表中,但它不会显示在jtable中。

Connection con = null;
Statement stmt = null;
try{
    Class.forName("com.mysql.jdbc.Driver");
    con = DriverManager.getConnection(DB_URL, USER, PASS);
    stmt = con.createStatement();
    String sql = "insert into customers "+
        "values("+id+", \""+name+"\", "+"\""+add+"\", "+pno+");";
    stmt.executeUpdate(sql);
    ResultSet rs = stmt.executeQuery("Select * from customers;");
    DefaultTableModel model = new DefaultTableModel(); 
    JTable table = new JTable(model); 
    JScrollPane scrollPane = new JScrollPane(table);
    model.addColumn("CustID"); 
    model.addColumn("CustName"); 
    model.addColumn("CustAdd"); 
    model.addColumn("PhoneNo"); 
    while(rs.next()){ 
        int tid = rs.getInt("custid");
        String tname = rs.getString("custname");
        String tadd = rs.getString("custadd");
        long tpno = rs.getLong("phoneno");
        model.addRow(new Object[]{tid, tname, tadd, tpno});
    }
    table.setVisible(true);
    }catch(SQLException se){
    jOptionPane1.showMessageDialog(this, se);
}catch(Exception e){
.
.
.

1 个答案:

答案 0 :(得分:0)

您将JTable添加到JScrollPane,但看起来您不会将JScrollPane添加到任何内容中。 (除非有代码我们没有看到。)