1
When optimizing my JDBC code I started using a condition within Try.
/** Função PreparedStatement stmt */
public PreparedStatement stmt;
/** Funçãoo ResultSet rs */
public ResultSet rs;
/** Método LoginDao - construtor */
public LoginDao() {
super();
}
public long adiciona(Object object) {
try {
if (object.getClass() != new Login().getClass()) {
throw new RuntimeException();
}
Login login = (Login) object;
// Inserção Banco de Dados
String sql = "INSERT INTO login "
+ "(email, senha, privilegio,ultimo_acesso,cpf)"
+ " VALUES (?,?,?,?,?)";
try(stmt = getConnection().prepareStatement(sql)){}
// prepared statement para inser��o
stmt = getConnection().prepareStatement(sql);
// criptografa os dados
String emailCriptografado = new CriptografiaUniDirecional()
.criptografar(login.getEmail());
String senhaCriptografada = new CriptografiaUniDirecional()
.criptografar(login.getSenha());
System.out.println(emailCriptografado);
System.out.println(senhaCriptografada);
// verifica se este email ja esta cadastrado
if (emailJaCadastrado(emailCriptografado) == false) {
/** seta os valores */
stmt.setString(1, emailCriptografado);
stmt.setString(2, senhaCriptografada);
stmt.setInt(3, login.getPrivilegio());
// Calendar cal = Calendar.getInstance();
Date data = new Date(System.currentTimeMillis());
stmt.setDate(4, data);
stmt.setString(5, login.getCpf());
stmt.execute();
} else {
stmt.close();
return 1L;
}
stmt.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
return -1L;
}
But he doesn’t see the variablePreparedStatement stmt;
not recognizing it as a type. I wonder if I have to declare the type of the variable being that it is already declared in my class.
Put the code where you declare the variable.
– Jéf Bueno
public Preparedstatement stmt; public Resultset rs; The code was running nice. However when trying to optimize and put it in Try the error object
– Victor Henrique
Dude, put in the question all the relevant code in your class. Only with a line of code can’t help you.
– Jéf Bueno
Puts the entire declaration of your method @Victorhenrique
– Wellington Avelino