When optimizing my JDBC code I started using a condition inside the 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 it does not see the variable PreparedStatement stmt;
not recognizing it as a type. I want to know if I have to declare the type of the variable being it already declared in my class.