我不知道为什么但桌子没有显示。值将被添加到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){
.
.
.
答案 0 :(得分:0)
您将JTable
添加到JScrollPane
,但看起来您不会将JScrollPane
添加到任何内容中。 (除非有代码我们没有看到。)