Java-You have an error in your SQL syntax

publicado por: Anonymous

Buenas. Ando haciendo un programa en java que conecta con una base de datos MySQL y me está dando problemas hacer un select. El código es el siguiente:

try{ 
        St = conexion.createStatement();
        ResultSet = St.executeQuery ("SELECT * FROM `lxgtt` WHERE web='google' AND clave='user'"); 
        while (rs.next()){ 
            web=rs.getString(1);
            mayus=rs.getString(2);
            numeros=rs.getString(3);
            letras=rs.getString(4);
            simbolos=rs.getString(5);
            modificacion=rs.getString(6);
            clave=rs.getString(7);
            }
    } catch (Exception e ) { 
        e.printStackTrace(); 
    } 

Y el error:

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 'WHERE web='google'' at line 1
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)
at com.mysql.jdbc.Util.getInstance(Util.java:408)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:943)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3973)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3909)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2527)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2680)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2486)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2444)
at com.mysql.jdbc.StatementImpl.executeQuery(StatementImpl.java:1381)
at Model.conexionHost.consultaWeb(conexionHost.java:75)
at view.ventanaPrincipal.comprobarWebHost(ventanaPrincipal.java:882)
at view.ventanaPrincipal.clickBoton(ventanaPrincipal.java:1002)
at view.ventanaPrincipal$16.mouseReleased(ventanaPrincipal.java:577)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

Si realizo la consulta tal cual directamente en MySQL me da resultado, pero no desde Java y no consigo encontrar el error (la conexión con la bd e inserción de datos los hace correctamente).

Gracias y un saludo.

solución

Para manejar errores en sentencias SQL mejor capturar excepciones SQL. Un fallo podría estar en las comillas de la cadena de caracteres de la sentencia SQL, puedes (opcionalmente) crearte un String con la sentencia y pasarla por parámetro. También he declarado las variables Statement y ResultSet de la siguiente manera:

try{ 
        // Preparamos la consulta 
        Statement st = conexion.createStatement();
        String selectSQL = "SELECT * FROM lxgtt WHERE web=google AND clave=user";
        ResultSet rs= st.executeQuery (selectSQL); 
        // Recorremos el resultado, mientras haya registros para leer
        while (rs.next()){ 
            web=rs.getString(1);
            mayus=rs.getString(2);
            numeros=rs.getString(3);
            letras=rs.getString(4);
            simbolos=rs.getString(5);
            modificacion=rs.getString(6);
            clave=rs.getString(7);
            }
    } catch (SQLException ex ) { 
        System.out.println(ex.getErrorCode()); // si tu aplicación es por consola
    } 
Respondido por: Anonymous

Leave a Reply

Your email address will not be published. Required fields are marked *