Kingmotta 0 Denunciar post Postado Janeiro 29, 2016 Pessoal, to aprendendo java web agora, e fiz o seguinte código: public boolean logarUsuario(Usuario usuario) throws Exception { PreparedStatement ps = null; try { String SQL = "SELECT id FROM usuario WHERE nome =? and senha =?"; ps = this.conn.prepareStatement(SQL); ps.setString(1, usuario.getLogin()); ps.setString(2, usuario.getPassword()); ps.executeQuery(SQL); ResultSet rs = ps.getResultSet(); boolean usuarioLogado = Boolean.valueOf(SQL); return usuarioLogado; } catch (SQLException sqle) { throw new Exception("Erro ao gravar dados: " + sqle); } finally { if (ps != null) { ps.close(); } ConnectionFactory.closeConnection(this.conn); } } Esse está dando o seguinte erro: java.lang.Exception: Erro ao gravar dados: 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 '?' at line 1 Mas o insert está funcionando perfeitamente, e também tem os '?' nele. E o meu outro select sem where funciona tbm. Procurei na net mas não achei algo que ajudasse. Compartilhar este post Link para o post Compartilhar em outros sites
rockrgo 138 Denunciar post Postado Janeiro 29, 2016 tenta dar um espaço entre o ? e o = nome = ? and senha = ? Compartilhar este post Link para o post Compartilhar em outros sites
Kingmotta 0 Denunciar post Postado Janeiro 29, 2016 tenta dar um espaço entre o ? e o = nome = ? and senha = ? Obrigado por tentar me ajudar, mas não funcionou. ;/ Compartilhar este post Link para o post Compartilhar em outros sites
rockrgo 138 Denunciar post Postado Janeiro 29, 2016 tenta dar um sysout no login e na senha para ver se os valores estão chegando. Compartilhar este post Link para o post Compartilhar em outros sites
Kingmotta 0 Denunciar post Postado Janeiro 29, 2016 Estão chegando.. Para testar alterei essa parte do código: String sWhere = "nome= " + usuario.getLogin() + " and senha = " + usuario.getPassword(); try { String SQL = "SELECT id FROM usuario WHERE " + sWhere; ps = this.conn.prepareStatement(SQL); // ps.setString(1, usuario.getLogin() + " and senha = " + usuario.getPassword()); //ps.setString(2, usuario.getPassword()); ps.executeQuery(SQL); ResultSet rs = ps.getResultSet(); return rs; } catch (SQLException sqle) { e o erro mudou para: java.lang.Exception: Erro ao procurar dados: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'lucas' in 'where clause' Mas o lucas é o getLogin, e meio que ele ta invertendo e colocando lucas como coluna na hora do execute... Pois fiz o teste também enviando uma string do meu select montado, e ficou perfeito.. Compartilhar este post Link para o post Compartilhar em outros sites
rockrgo 138 Denunciar post Postado Janeiro 29, 2016 Faz este teste com esta query. String sWhere = "nome= '" + usuario.getLogin() + "' and senha = '" + usuario.getPassword() + "'"; Compartilhar este post Link para o post Compartilhar em outros sites
Kingmotta 0 Denunciar post Postado Janeiro 29, 2016 Faz este teste com esta query. String sWhere = "nome= '" + usuario.getLogin() + "' and senha = '" + usuario.getPassword() + "'"; Funcionou, só não entendo o porque do "?" não funcionar, mas de qualquer forma esta gambiarra funcionou ahha. Obrigado! Compartilhar este post Link para o post Compartilhar em outros sites
rockrgo 138 Denunciar post Postado Janeiro 29, 2016 Tenta assim String sWhere = "nome = '?' and senha = '?'"; Compartilhar este post Link para o post Compartilhar em outros sites