-1
Well I made a method to change password
where it does a select in the database to see if the login and password entered are equal in the database
String sql = "SELECT id_usuario,nome,senha FROM usuario where login = ? and senha = ?";
try {
PreparedStatement stmt = con.prepareStatement(sql);
stmt.setString(1, u.getLogin());
stmt.setString(2, u.getSenha());
ResultSet rs = stmt.executeQuery();
and then I did that check with an if:
if (rs.next()){
String update = "UPDATE usuario SET senha=? WHERE id_usuario = " +Sessao.getInstancia().getUsuario().getId()+ "";
PreparedStatement stmt2 = con.prepareStatement(sql);
stmt2.setString(1, u.getNovaSenha());
ResultSet rs2 = stmt2.executeQuery();
return true;
}
to update the password if id equals the id of the connected user I debugged and checked that it is getting the user id normally, but it is giving this error:
GRAVE: null
java.sql.SQLException: No value specified for parameter 2
I know it has to do with structure, but I have no idea where I went wrong and how and where I would close the connection with the comic dps of that?
Because you started doing the code the right way and then decided to open your application to have a SQL Injection and be invaded?
– Maniero
I’m new in java I did it in the attempt, but I would close again the connection.
– Felipe
Could you give me a hand how I would improve this? or an easier way.
– Felipe
So start by learning the basics, doing the kick, trying to guess things don’t work out so well. Just now you tried to guess again, thinking that closing something will eliminate SQL Injection. You will have serious problems trying to programmer this way, you have to understand everything that is happening with the code. Whatever you put in it and don’t master what’s there, it’s already wrong, even if it works.
– Maniero
My mistake was to open another connection? Could you help me explain better about it and a way to resolve ?
– Felipe
Perhaps the best way is to update when login / password / id is equal to login password?
– Felipe
Read my first two comments, let’s stay in circles here.
– Maniero
I recommend: https://answall.com/q/172909/132
– Victor Stafusa
Edit the question and post the entire code of the method in question. I have the impression that without this you cannot answer the question satisfactorily.
– Victor Stafusa
Ah, and how do you get this variable
con
?– Victor Stafusa
I also recommend: https://answall.com/q/2402/132
– Victor Stafusa
@Victorstafusa opa man vlw I have already solved with a method, I get the con through the user class constructor :
– Felipe
ended up like this:
– Felipe
public Boolean changeSenha(User u){ String sql = "UPDATE user SET password=? WHERE id_usuario = " +Sessao.getInstancia(). getUsuario(). getId()+" and login='"+Sessao.getInstancia(). getUsuario(). getLogin()+"' and password=?" ;
 try {
 PreparedStatement stmt = con.prepareStatement(sql);
 stmt.setString(1, u.getNovaSenha());
 stmt.setString(2, u.getSenha());
 stmt.execute();
 stmt.close();
 con.close();
– Felipe
will now be good?
– Felipe