how to modify a database from the Jtable?

1

Hi, I'm good at netbeans, help me, I need to find a way to modify data from bd directly from the jtable of Frame and validate the changes by pressing a button.

method that I used recently and does nothing ...

public void modificar(){

    Conectar conex = new Conectar();
    Connection con = conex.conectar();

        frmListar.tabla.getModel().addTableModelListener(new TableModelListener(){
        @Override
        public void tableChanged(TableModelEvent e){
               if(e.getType() == TableModelEvent.UPDATE){
                   String colname = null;
                   if(e.getColumn() ==0){
                       colname = "num_ficha";
                   }
                   else if(e.getColumn() ==1){
                       colname = "nombre";
                   }
                   else if(e.getColumn() ==2){
                       colname = "edad";
                   }
                   else if(e.getColumn() ==3){
                       colname = "vacunas";
                   }
                   else if(e.getColumn() ==4){
                       colname = "peso";
                   }
                   else if(e.getColumn() ==5){
                       colname = "altura";
                   }
                   else if(e.getColumn() ==6){
                       colname = "tipo";
                   }
                   else if(e.getColumn() ==7){
                       colname = "clasificacion";
                   }
                   else if(e.getColumn() ==8){
                       colname = "nom_duenno";
                   }
                   else if(e.getColumn() ==9){
                       colname = "fono";
                   }
                String sql = "UPDATE mascota SET "+colname+"="+frmListar.tabla.getModel().getValueAt(e.getFirstRow(), e.getColumn())+"WHERE num_ficha=?";
                Conectar conex = new Conectar();
                Connection con = conex.conectar();
                PreparedStatement ps;
                try {
                   ps = con.prepareStatement(sql);
                   int rows = ps.executeUpdate();
                }catch (SQLException ex) {
        Logger.getLogger(Registro.class.getName()).log(Level.SEVERE, null, ex);
               } 
        }
        };
    });
}

When I change a data in the tabla with this method and I press the modify button everything goes fine, but when I move the cursor to another cell, the following error occurs and no change is made:

  

Dec 12, 2018 12:11:02 PM Controller.Registro $ 2 tableChanged SEVERE:   null java.sql.SQLException: No value specified for parameter 1 at   com.mysql.jdbc.SQLError.createSQLException (SQLError.java:1074) at   com.mysql.jdbc.SQLError.createSQLException (SQLError.java:988) at   com.mysql.jdbc.SQLError.createSQLException (SQLError.java:974) at   com.mysql.jdbc.SQLError.createSQLException (SQLError.java:919) at   com.mysql.jdbc.PreparedStatement.checkAllParametersSet (PreparedStatement.java:2611)     at   com.mysql.jdbc.PreparedStatement.fillSendPacket (PreparedStatement.java:2586)     at   com.mysql.jdbc.PreparedStatement.executeUpdate (PreparedStatement.java:2432)     at   com.mysql.jdbc.PreparedStatement.executeUpdate (PreparedStatement.java:2375)     at   com.mysql.jdbc.PreparedStatement.executeUpdate (PreparedStatement.java:2359)     at Controller.Registro $ 2.tableChanged (Registro.java:253) at   javax.swing.table.AbstractTableModel.fireTableChanged (AbstractTableModel.java:296)     at   javax.swing.table.AbstractTableModel.fireTableCellUpdated (AbstractTableModel.java:275)     at   javax.swing.table.DefaultTableModel.setValueAt (DefaultTableModel.java:666)     at javax.swing.JTable.setValueAt (JTable.java:2741) at   javax.swing.JTable.editingStopped (JTable.java:4723) at   javax.swing.AbstractCellEditor.fireEditingStopped (AbstractCellEditor.java:141)     at   javax.swing.DefaultCellEditor $ EditorDelegate.stopCellEditing (DefaultCellEditor.java:368)     at   javax.swing.DefaultCellEditor.stopCellEditing (DefaultCellEditor.java:233)     at javax.swing.JTable $ GenericEditor.stopCellEditing (JTable.java:5467)     at   javax.swing.plaf.basic.BasicTableUI $ Handler.mousePressed (BasicTableUI.java:1010)     at   java.awt.AWTEventMulticaster.mousePressed (AWTEventMulticaster.java:280)     at   java.awt.AWTEventMulticaster.mousePressed (AWTEventMulticaster.java:279)     at java.awt.Component.processMouseEvent (Component.java:6530) at   javax.swing.JComponent.processMouseEvent (JComponent.java:3324) at   java.awt.Component.processEvent (Component.java:6298) at   java.awt.Container.processEvent (Container.java:2238) at   java.awt.Component.dispatchEventImpl (Component.java:4889) at   java.awt.Container.dispatchEventImpl (Container.java:2296) at   java.awt.Component.dispatchEvent (Component.java:4711) at   java.awt.LightweightDispatcher.retargetMouseEvent (Container.java:4897)     at   java.awt.LightweightDispatcher.processMouseEvent (Container.java:4531)     at java.awt.LightweightDispatcher.dispatchEvent (Container.java:4475)     at java.awt.Container.dispatchEventImpl (Container.java:2282) at   java.awt.Window.dispatchEventImpl (Window.java:2746) at   java.awt.Component.dispatchEvent (Component.java:4711) at   java.awt.EventQueue.dispatchEventImpl (EventQueue.java:760) 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:80)     at   java.security.ProtectionDomain $ JavaSecurityAccessImpl.doIntersectionPrivilege (ProtectionDomain.java:90)     at java.awt.EventQueue $ 4.run (EventQueue.java:733) at   java.awt.EventQueue $ 4.run (EventQueue.java:731) at   java.security.AccessController.doPrivileged (Native Method) at   java.security.ProtectionDomain $ JavaSecurityAccessImpl.doIntersectionPrivilege (ProtectionDomain.java:80)     at java.awt.EventQueue.dispatchEvent (EventQueue.java:730) at   java.awt.EventDispatchThread.pumpOneEventForFilters (EventDispatchThread.java:205)     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)

    
asked by luis nuñez 12.12.2018 в 16:15
source

1 answer

0

With the method addTableModelListener I did it and here I leave you my code I hope it serves you, here is more info example of use TableModelListener

modelo = new ModeloTabla(1);        
    tabla = new JTable(modelo); 
    tabla.getModel().addTableModelListener((TableModelEvent e) -> {
        if (e.getType() == TableModelEvent.UPDATE) {
            try {
                int fila = e.getFirstRow();
                int columna = e.getColumn();
                String idArchivo = tabla.getValueAt(tabla.getSelectedRow(), 0).toString();
                String textomodificado = tabla.getValueAt(fila,columna).toString();                                       
                String query = "update archivos set comentario = ? where idarchivo = ?";
                PreparedStatement preparedStmt = ConexionBaseDatos.obtener().prepareStatement(query);
                preparedStmt.setString   (1, textomodificado);
                preparedStmt.setString(2, idArchivo);                    
                preparedStmt.executeUpdate();      
                ConexionBaseDatos.cerrar();                    

            } catch (SQLException ex) {
                Logger.getLogger(Ventana.class.getName()).log(Level.SEVERE, null, ex);
            }


        }
    } //metodo para cuando se agrega informacion a la tabla
    );

every time you enter information to the table call this method automatically then here I take that information and enter it to the bd. You just have to save that info and when the button is pressed send that info to the bd

    
answered by 12.12.2018 / 21:03
source