title: SQL query to do INNER JOIN from two tables of a data base
body: <p>I have two tables:</p>
  
  <ol>
  <li>usuario (user in english): nombre, apellido, usuario, contrasena, id_perfil</li>
  <li>perfil (profile in english): id_perfil, nombre</li>
  </ol>
  
  <p>I have a login on my program using Java and MySQL and I want to know if the username and password entered by the user is correct, to send him to another Jframe.</p>
  
  <p>I don't know much about SQL query but, I did my best here (I'm passing the username and password directly to the function):</p>
  
  <pre><code>public boolean login(String usuario, String contrasena) {
          Dato_login d_lgn = new Dato_login();
          boolean resultado = false;
  
          sSQL = "SELECT u.nombre, u.apellido, u.usuario, u.contrasena, u.id_perfil, p.nombre AS perfil_nombre FROM "
                  + "usuario U INNER JOIN perfil P u.id_perfil = p.id "
                  + "WHERE u.usuario='" + usuario + "' AND u.contrasena='" + contrasena + "'";
  
          // Java 7 try-with-resources
          try (PreparedStatement pstm = con.prepareStatement(sSQL);
               ResultSet rs = pstm.executeQuery(sSQL)) {
  
              while (rs.next()) {
                  if (d_lgn.getContrasena().equals(contrasena)) {
                      resultado = true;
                  } else {
                      resultado = false;
                  }
  
                  d_lgn.setPerfil(rs.getString("perfil_nombre"));
                  d_lgn.setUsuario(rs.getString("usuario"));
                  d_lgn.setNombre(rs.getString("nombre"));
                  d_lgn.setApellido(rs.getString("apellido"));
                  d_lgn.setContrasena(rs.getString("contrasena"));
              }
  
          } catch (SQLException e) {
              JOptionPane.showMessageDialog(null, "SQLException:\n" + e, "Error: login(String usuario, String contrasena)", JOptionPane.ERROR_MESSAGE);
              }
  
          return resultado;
      }
  </code></pre>
  
  <p>Is not working, it keeps telling me that there is not any user to log in.
  What I need to change?</p>
  
  <p>Also I would like to receive the name of the profile instead of the id of the user, I tried with INNER JOIN but I don't understand how it works correctly yet.</p>
  
  <p>Table:</p>
  
  <p><a href="https://i.stack.imgur.com/ib6AX.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ib6AX.png" alt="enter image description here"></a></p>
  
  <p>Error received:</p>
  
  <blockquote>
    <p>Error in SQL Syntax</p>
  </blockquote>