Home > Blockchain >  java.sql.SQLSyntaxErrorException: Youhave an error in your SQL syntax; check the manual that corresp
java.sql.SQLSyntaxErrorException: Youhave an error in your SQL syntax; check the manual that corresp

Time:08-05

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; 

CodePudding user response:

When using prepared statements you need to call executeQuery without passing any parameter, so

ResultSet resultado = pstm.executeQuery(); 

As per the documentation of the parametrized version:

Note:This method cannot be called on a PreparedStatement or CallableStatement.

CodePudding user response:

 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();
  • Related