2017-06-20 56 views
1

好吧,我做了一个新的更新Jtable代码,但它不工作,你能帮我理解为什么吗?当我更新JTable中的数据库时,我得到了多个项目

下面的代码:

private void update(){ 
     DefaultTableModel modelo = new DefaultTableModel(); 
     try{ 
      Connection lig; 
      lig = DriverManager.getConnection("jdbc:mysql://localhost/bdteste","root",""); 
      PreparedStatement inst; 
      inst = (PreparedStatement) lig.createStatement(); 
      ResultSet res; 
      res = inst.executeQuery("SELECT * FROM pessoa"); 
      while(res.next()){ 
       int id = res.getInt("ID"); 
       String descriçao = res.getString("Descriçao"); 
       double montante = res.getDouble("Montante"); 
       String categoria = res.getString("Categoria_Extrato"); 
       model.addRow(new Object[]{id, descriçao, montante, categoria}); 
      } 
        res.close(); 
     inst.close(); 
     lig.close(); 
     } 

     catch(SQLException ex){ 
       JOptionPane.showMessageDialog(null, "Erro na base de dados!"); 
     } 
     recdadostbl.setModel(modelo); 
    } 

而这里发生了什么: Me inserting the dataError1

+2

1)为了更好地帮助更快,发布[MCVE]或[短,自成一格,正确的例子](http://www.sscce.org/)。 2)使用合乎逻辑的一致形式缩进代码行和块。缩进旨在使代码的流程更易于遵循! 3)IDE与问题无关。不要在标题中提及它,或者添加标签。 –

回答

0

我想我找到了我的问题的解决方案。 当我进入我的表板,我删除了所有的表model.setRowCount(0);,然后我用这个方法重新插入所有新的和旧的数据,

public void updatetbl() 
{ 

    try { 
Connection lig; 
lig = DriverManager.getConnection("jdbc:mysql://localhost/financas","root",""); 
Statement inst; 
inst = lig.createStatement(); 
ResultSet res = inst.executeQuery("SELECT * FROM extrato"); 
    while(res.next()){ 
    int id = res.getInt("ID"); 
    String descriçao = res.getString("Descriçao"); 
    double montante = res.getDouble("Montante"); 
    String categoria = res.getString("Categoria_Extrato"); 
    model.addRow(new Object[]{id, descriçao, montante, categoria}); 

    } 

    } 
    catch(SQLException ex){ 
     JOptionPane.showMessageDialog(null,"Base de Dados Indisponivel"+ex); 
    } 
} 
相关问题