Problem with JButtton delete

1

Good I have this code for delete that is part of a class that handles the events of a frame :

if(e.getSource()== estAdmin.getSubmenu_Eliminar()){
        int Option= JOptionPane.showConfirmDialog(estAdmin, "Esta seguro"
                + "de que desea borrar este contacto", "Borrar", JOptionPane.YES_NO_OPTION);

        if(Option== 0){    
            int fila= estAdmin.getTablaEstudiante().getSelectedRow();

            String id= estAdmin.getTablaEstudiante().getValueAt(fila, 0).toString();

            estDAO.borrar(Integer.parseInt(id));
            this.ListarTabla(estAdmin.getTablaEstudiante());
        }
    }

The following is the method to which you send the parameter you get:

public void borrar(int Id) {
    Connection con = null;
    PreparedStatement pstm = null;

        try {
            con = dbcon.conectar();

        String sql= "DELETE FROM notas WHERE id=?";
            pstm= con.prepareStatement(sql);

            pstm.setInt(1, Id);

            pstm.execute();

    } catch (SQLException ex) {
        JOptionPane.showMessageDialog(null, "Error al conectar a la BD");
    }
    finally {
        dbcon.desconectar(con);
    }
}

However, when running the program and trying to delete an element of JTable I get the following stackTrace :

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "David"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:580)
at java.lang.Integer.parseInt(Integer.java:615)
at comm.estudiante.dao.controlador.ControladorEstudiante.actionPerformed(ControladorEstudiante.java:192)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.AbstractButton.doClick(AbstractButton.java:376)
at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:833)
at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(BasicMenuItemUI.java:877)
at java.awt.Component.processMouseEvent(Component.java:6535)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6300)
at java.awt.Container.processEvent(Container.java:2236)
at java.awt.Component.dispatchEventImpl(Component.java:4891)
at java.awt.Container.dispatchEventImpl(Container.java:2294)
at java.awt.Component.dispatchEvent(Component.java:4713)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
at java.awt.Container.dispatchEventImpl(Container.java:2280)
at java.awt.Window.dispatchEventImpl(Window.java:2750)
at java.awt.Component.dispatchEvent(Component.java:4713)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

Line 192 that mentions the stackTrace is the following:

estDAO.borrar(Integer.parseInt(id));

You can tell me why this error occurs to me, when I see the stackTrace what it says to me it's as if the method is not receiving the number of the column.

This is the DefaultTableModel of the JTable:

public void ListarTabla(JTable tabla){
    DefaultTableModel modeloTabla= new DefaultTableModel();
    tabla.setModel(modeloTabla);

    modeloTabla.addColumn("NOMBRE");
    modeloTabla.addColumn("MATRICULA");
    modeloTabla.addColumn("NOTA");

    Object[] columna= new Object[3];

    //instancia del List
    int objGuardados= estDAO.extraerTodos().size();

    for (int i = 0; i < objGuardados; i++) {
        columna[0]= estDAO.extraerTodos().get(i).getNombre();
        columna[1]= estDAO.extraerTodos().get(i).getMatricula();
        columna[2]= estDAO.extraerTodos().get(i).getNota();

        modeloTabla.addRow(columna);
    }

}

The following code corresponds to the List from which the table is filled:

public List<Estudiante> extraerTodos() {
    Connection con = null;
    Statement stmnt = null;
    ResultSet rs = null;
    String sql = "SELECT * FROM notas";
    List<Estudiante> lista = new ArrayList<>();    

    try {
        con = dbcon.conectar();
        stmnt = con.createStatement();
        rs = stmnt.executeQuery(sql);

        while (rs.next()) {
        lista.add(new Estudiante(
                rs.getInt("id"),
                rs.getString("nombre"),
                rs.getString("matricula"),
                rs.getInt("nota")
        ));
    }
    
asked by David Calderon 10.09.2016 в 03:35
source

2 answers

1

You could do the following and support yourself from the vector you occupy:

     if(e.getSource()== estAdmin.getSubmenu_Eliminar()){
                int Option= JOptionPane.showConfirmDialog(estAdmin, "Esta seguro"
                        + "de que desea borrar este contacto", "Borrar", JOptionPane.YES_NO_OPTION);

                if(Option== 0){    
                    //obtenemos la posicion de la arreglo del cual extraeremos el id a ocupar
                    int fila= estAdmin.getTablaEstudiante().getSelectedRow();

           List<Estudiante> estudiantes = estDAO.extraerTodos();
           Estudiante e = estudiantes.get(fila); 
           int id = e.getID();



     estDAO.borrar(id);
                this.ListarTabla(estAdmin.getTablaEstudiante());
            }
        }
    
answered by 10.09.2016 / 05:37
source
1

If you check your error message:

  

Exception in thread "AWT-EventQueue-0"   java.lang.NumberFormatException: For input string: "David" at   java.lang.NumberFormatException.forInputString (NumberFormatException.java:65)

You can see that the problem is caused when trying to parse a String that does not have numeric value to integer, in this case the value of id is "David", this value should be a numeric value:

estDAO.borrar(Integer.parseInt(id));

Ensures that the value of id always contains a String with numerical value.

    
answered by 10.09.2016 в 05:08