数据库进入JTable

时间:2012-08-22 22:40:05

标签: java database jtable

我想将MySql的数据添加到JTable。我只是编写这个简单的代码来实现它,但它无法正常工作。

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")});
} 

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

添加行后始终使用fireTableRowsInserted()。同时裁判以下链接

Adding rows to a JTable