Java-You have an error in your SQL syntax

0

Good morning. I am doing a java program that connects to a MySQL database and it is giving me problems to make a select. The code is as follows:

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(); 
    } 

And the 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)

If I perform the query as it is directly in MySQL, it works for me, but not from Java and I can not find the error (the connection with the bd and inserting data makes them correctly).

Thanks and best regards.

    
asked by Frandalf 11.11.2017 в 11:03
source

2 answers

2

To handle errors in SQL statements, better to capture exceptions SQL . A failure could be in the quotes of the character string of the SQL statement, you can (optionally) create a String with the statement and pass it by parameter. I have also declared the variables Statement and ResultSet as follows:

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
    } 
    
answered by 11.11.2017 / 11:34
source
0

You did not assign a name to the Resultset object that you are creating here:

ResultSet = St.executeQuery ("SELECT * FROM lxgtt WHERE web='google' AND clave='user'");

And then you use to read the results, thinking that it is called rs :

while (rs.next()){ ....

You must then assign the name:

ResultSet rs = St.executeQuery ("SELECT * FROM 'lxgtt' WHERE web='google' AND clave='user'"); 

Also the variable St imagine that you have it stated above like this:

Statement St = null;

Note: Consider applying an appropriate naming convention. The code will be easier to read and analyze, not only for you, but for any programmer.

In Java, the naming convention indicates that the names of Clases start with capital letters, and variable names are lower case, so your variable St does not comply with the naming convention.

For more details: What is the convention for writing variables in Java?

    
answered by 11.11.2017 в 11:31