javamysqlprepared-statement

java.sql.SQLSyntaxErrorException: Youhave an error in your SQL syntax; check the manual that corresponds to your MariaDB server version


I use MySQL, why MariaDB in the error message? I have tried replacing the parameters `? by known values and it worked. The problem is in the "?" as a placeholder for parameters 1 and 2 in the code

try {
            

             //THE ISSUE IS HERE!
            String sql = "Select * From dusuario Where nome_usuario= ? and senha_usuario= ?"; 
            
            
            PreparedStatement pstm = conn.prepareStatement(sql);
            

            
            pstm.setString(1, usuarioDTO.getNome_usuario()); 
            pstm.setString(2, usuarioDTO.getSenha_usuario()); 
            
            
            ResultSet resultado = pstm.executeQuery(sql);
            
            return resultado;
            
        } catch (SQLException erro) {
            JOptionPane.showMessageDialog(null, "Erro na classe UsuarioDao: "+ erro);
            
            return null; 

Solution

  •  ResultSet resultado = pstm.executeQuery(sql);
    

    In the above line you doesn't need to pass sql String again as it is already assign to the pstm object and the values are updated using setString()

    So, the line can ne rewritted as

    ResultSet resultado = pstm.executeQuery();