2
I’m having a doubt when passing a typed amount. I’m posting here because I stayed the whole afternoon yesterday and found nothing.
In first class:
public class CompararLogin {
private String nome;
public String cpf;
Scanner scan = new Scanner(System.in);
public String solicitarCpfLogin() {
System.out.print("Cpf: ");
cpf = scan.nextLine();
return cpf;
}
public String solicitarNome() {
System.out.print("Nome: ");
nome = scan.nextLine();
return nome;
}
public String getCpf() {
return cpf;
}
public void setCpf(String cpf) {
this.cpf = cpf;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
}
In Second Class:
public class SocioDAO {
public void fazerLogin() throws SQLException, NomeUsuarioNaoInformadoExecption, CpfUsuarioNaoInformadoException, TelefoneUsuarioNaoInformadoException {
StringBuilder sql = new StringBuilder();
sql.append("SELECT codigo, nome, cpf ");
sql.append("FROM socio ");
Connection conexao = FabricaDeConexao.conectar();
PreparedStatement comando = conexao.prepareStatement(sql.toString());
ResultSet rs = comando.executeQuery();
CompararLogin compararUsuarioExistente = new CompararLogin();
String verificaNomeCadastrado = compararUsuarioExistente.solicitarNome();
String verificaSenhaUsuario = compararUsuarioExistente.solicitarCpfLogin();
boolean achou = false;
while (rs.next()) {
String nome = rs.getString("nome");
String cpf = rs.getString("cpf");
int codigo = rs.getInt("codigo");
if (verificaNomeCadastrado.equals(nome) && verificaSenhaUsuario.equals(cpf)){
System.out.println("Login Efetuado Com Sucesso!!! ");
achou = true;
SolicitacoesEmprestimosPendentesExceptions contarSolicitacoesEmprestimo = new SolicitacoesEmprestimosPendentesExceptions();
contarSolicitacoesEmprestimo.Contar(cpf);
System.err.println("SEU CÓDIGO P/ CADASTRO DOS DVD'S É: " + codigo);
MenuChamarCadastroDvdEEmprestimo mostrarOpcao = new MenuChamarCadastroDvdEEmprestimo();
mostrarOpcao.escolherOpcaoDvdEmprestimo();
}
} if (achou == false) {
System.out.println("Usuário não Cadastrado!!!");
TratadorDeIniciarAplicacao voltando = new TratadorDeIniciarAplicacao();
voltando.main(null);
}
}
}
In Third Class: This method makes a count of loan requests IE, the guy is in the mood to rent a DVD and is on a waiting list, I intend to pass this amount when the Login is successfully performed.
public class SolicitacoesEmprestimosPendentesExceptions extends CompararLogin {
public int contadorDeSolicitacoesEmprestimosPendentes(String cpf) throws SQLException, CpfUsuarioNaoInformadoException{
StringBuilder sql = new StringBuilder();
sql.append("SELECT COUNT(*) ");
sql.append("FROM solicitacaoemprestimo ");
sql.append("INNER JOIN socio ON (solicitacaoemprestimo.socio_codigo = socio.codigo) ");
sql.append("WHERE socio.cpf = '11111111111' ");
Connection conexao = FabricaDeConexao.conectar();
PreparedStatement comando = conexao.prepareStatement(sql.toString());
ResultSet resultado = comando.executeQuery();
int nCont = 0;
while(resultado.next()){
nCont = resultado.getInt("COUNT(*)");
}
return nCont;
}
public void Contar(String cpf) throws CpfUsuarioNaoInformadoException, SQLException, NomeUsuarioNaoInformadoExecption, TelefoneUsuarioNaoInformadoException{
SolicitarEmprestimoDAO dao = new SolicitarEmprestimoDAO();
System.out.println(contadorDeSolicitacoesEmprestimosPendentes(cpf));
}
}
So, guys, here’s my idea:
When I type the CPF to Login I would like my method contadorDeSolicitacoesEmprestimosPendentes
received this entered value from CPF. Then I would show the result pending value within my method FazerLogin()
, that that would be:
SolicitacoesEmprestimosPendentesExceptions contarSolicitacoesEmprestimo = new SolicitacoesEmprestimosPendentesExceptions();
contarSolicitacoesEmprestimo.Contar(cpf);
Although the answer may work correctly, please do not encourage the creation of SQL Injection problems. It is best to demonstrate how to use the
comando.setParameter(1, cpf)
.– Victor Stafusa