【问题标题】:When I update database in a JTable, I get multiple Items当我在 JTable 中更新数据库时,我得到了多个项目
【发布时间】:2017-06-20 19:47:48
【问题描述】:

好的,我做了一个新的更新 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

【问题讨论】:

标签: java sql swing jtable


【解决方案1】:

我想我找到了解决问题的方法。 当我进入我的表格面板时,我删除了所有表格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 );
   }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-30
    • 1970-01-01
    • 1970-01-01
    • 2017-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多