-2

我想将数据添加MySqlJTable. 我只是写了这个简单的代码来做到这一点,但它不起作用。

Properties props = new Properties();
    props.put("user", "root"); 
    props.put("password", "password");  
    props.put("useUnicode", "true"); 
    props.put("characterEncoding", "UTF-8");
    props.put("useServerPrepStmts", "false");
    props.put("characterEncoding", "UTF-8");
    url = "jdbc:mysql://localhost/library_System";
    Connection conn = (Connection) DriverManager.getConnection(url, props);
           Statement stmt = (Statement) conn.createStatement();    

    String SQL = "select * from library_system";

    ResultSet rs = stmt.executeQuery(SQL);

DefaultTableModel model = new DefaultTableModel();

model.addColumn("Name");
model.addColumn("Email");

JTable table = new JTable(model);
table.setSize(Int2.getWidth(), Int2.getHeight());
panel3.add(table);

while(rs.next()) 
{ 
    model.addRow(new Object[]{rs.getString("Full_Name"), rs.getString("Email")});
} 
4

2 回答 2

0

添加行后始终使用fireTableRowsInserted() 。还请参考以下链接

向 JTable 添加行

于 2012-08-23T07:09:41.380 回答
0

不太确定,但先尝试 addRow() 。稍后使用模型创建 JTable。

于 2012-08-23T06:29:05.317 回答