2012-08-22 124 views
-2

我想將MySql中的數據添加到JTable中。我只是寫這個簡單的代碼來做到這一點,但它不工作。將數據庫導入到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")}); 
} 
+1

確定您的查詢返回結果嗎?快速的方法來驗證這將是添加一個println到你的循環。 – Bill

+0

你好,查詢返回結果 – user1529682

+0

你可以添加ResultSet對象的代碼和查詢... – Chan

回答

0

不太確定,但先嚐試addRow()。 稍後使用模型創建JTable。