Problem with JButtton update MySQL

1

I have this method to update the elements of a table in a class that is responsible for controlling the events of the Jframe :

    if(e.getSource()== estMostrar.getBtnModificar()){
        int fila= estAdmin.getTablaEstudiante().getSelectedRow();

        if(fila>=0){
        String nombre= estMostrar.getTxtNombre().getText();
        String materia= estMostrar.getTxtMatricula().getText();
        String nota= estMostrar.getTxtNota().getText();

        List<Estudiante> estudiantes = estDAO.extraerTodos();
        Estudiante es = estudiantes.get(fila);

            if(es!=null){
                int id = es.getId(); 
                this.estDAO.actualizar(new Estudiante(nombre, materia, Integer.parseInt(nota)), id);

                this.LimpiarCampos();
                this.estMostrar.dispose();
            } else {
                JOptionPane.showMessageDialog(estAdmin, "Seleccione Estudiante");
            }
                this.ListarTabla(estAdmin.getTablaEstudiante());
        }else{
            JOptionPane.showMessageDialog(estAdmin, "No ha selecionado fila");
        }
    }

The above method sends the data as parameters to the following method to update the object in the Database :

public void actualizar(Estudiante e, int Id) {
    Connection con= null;
    PreparedStatement pstm= null;

        try {
            con= dbcon.conectar();

            String sql= "UPDATE notas SET nombre= ?, matricula= ?, nota= ?"
                    + "WHERE id = ?";
            pstm= con.prepareStatement(sql);

            pstm.setString(1, e.getNombre());
            pstm.setString(2, e.getMatricula());
            pstm.setInt(3, e.getNota());
            pstm.setInt(4, Id);

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

When I try to update an item, it gives me an error in console, this is the stackTrace:

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'id = 1' at line 1
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:404)
at com.mysql.jdbc.Util.getInstance(Util.java:387)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:942)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3966)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3902)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2526)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2673)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2549)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1861)
at com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2073)
at com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2009)
at com.mysql.jdbc.PreparedStatement.executeLargeUpdate(PreparedStatement.java:5098)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1994)
at comm.estudiante.dao.mysql.EstudianteDAOImpl.actualizar(EstudianteDAOImpl.java:115)
at comm.estudiante.dao.controlador.ControladorEstudiante.actionPerformed(ControladorEstudiante.java:241)
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.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
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)

The line number 115 that says the stackTrace corresponds to the update (Student e, int Id) is as follows:

pstm.executeUpdate();

And the following is line 241 that mentions the stackTrace corresponds to the if of the modify button, which is at the beginning:

this.estDAO.actualizar(new Estudiante(nombre, materia, Integer.parseInt(nota)), id);

I really do not know what happens then, the method receives the id of the object.

    
asked by David Calderon 10.09.2016 в 21:30
source

1 answer

1

The problem is indicated here:

  

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an   error in your SQL syntax; check the manual that corresponds to your   MySQL server version for the right syntax to use near 'id = 1' at line   1

and it is because in your Query it is incorrect since the value of nota is next to the word "WHERE" :

 String sql= "UPDATE notas SET nombre= ?, matricula= ?, nota= ?"
                    + "WHERE id = ?";

You need to add a space before the word "WHERE" :

String sql= "UPDATE notas SET nombre= ?, matricula= ?, nota= ?"
                    + " WHERE id = ?";
    
answered by 10.09.2016 / 23:10
source