Pass typed value to another method

Asked

Viewed 87 times

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);

2 answers

3

You are already passing the String cpf when you do:

 contarSolicitacoesEmprestimo.Contar(cpf);

Now, the problem was at the time you were assembling your sql. When you do, sql.append("WHERE socio.cpf = '11111111111' "); ends up searching only Cpfs with 11111111111.

One option would be to mount it as follows, using the String cpf:

sql.append("SELECT COUNT(*) ");
sql.append("FROM solicitacaoemprestimo ");
sql.append("INNER JOIN socio ON (solicitacaoemprestimo.socio_codigo = socio.codigo) ");
sql.append("WHERE socio.cpf =" +cpf); 
  • 1

    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).

2

I figured it out, I’ll post it here, maybe it helps someone.

I modified that class:

public String pegarCpf(){
    cpf = getCpf();
    return cpf;
}

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 = ? ");

    Connection conexao = FabricaDeConexao.conectar();
    PreparedStatement comando = conexao.prepareStatement(sql.toString());


    comando.setString(1, pegarCpf());

    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("Solicitações Pendentes: "+contadorDeSolicitacoesEmprestimosPendentes(cpf));


}

}

And in the compare class I modified Login to Static.

public class CompararLogin {

    private String nome;
    public static 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;
    }

    @SuppressWarnings("static-access")
    public void setCpf(String cpf) {
        this.cpf = cpf;
    }

    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.