Problems getting ResultSet value

1

I'm trying to get the values from a select in the derby but they only correctly return a field from the ResulteSet. Here is the code for the DB access class:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Locale;

import javax.management.RuntimeErrorException;

public class Conexao {
    private Connection con;
    private String URL = "jdbc:derby://localhost:1527/ATM";

    public Conexao() {
        // Obter conexao
        try {

            con = DriverManager.getConnection(URL);
        } catch (SQLException e) {
            // mostra erro
            System.err.println(e.getMessage());
            throw new RuntimeException(e);
        }
    }

    public Connection getConnection() {
        return con;
    }

    public Conta autenticacao(int agencia, int conta, String senha)
            throws SQLException {
        Statement stmt = getConnection().createStatement();
        String sql = "SELECT AGENCIA, CONTA, SENHA, SALDO FROM contas WHERE AGENCIA=%d AND CONTA=%d AND SENHA='%s'";
        ResultSet rs = stmt.executeQuery(String.format(sql, agencia, conta, senha));

        if (rs.next()) {
            // conta autenticada
            Conta conta1 = new Conta(rs.getInt(1), rs.getInt(2),rs.getString(3), rs.getDouble(4));
            return conta1;
        } else {

            throw new RuntimeException("Erro de autenticacao");
        }
    }
}

The Account Object:

public class Conta {

    private int Agencia;
    private int Conta;
    private String Senha;
    private double Saldo;
    private boolean Tipo;

    public Conta() {};


    public Conta(int agencia, int conta, String senha, double saldo) {
        setAgencia(agencia);
        setConta(conta);
        setSenha(senha);
        setSaldo(saldo);
    }

    private void setAgencia(int agencia) {
        if (agencia > 999 && agencia <= 0) {
            Agencia = agencia;
        } else {
            System.out.println("numero invalido");
        }
    }

    private void setConta(int conta) {

        if (conta < 0 && conta > 99999) {
            Conta = conta;
        }
    }

    public void setSaldo(double saldo) {

        Saldo = saldo;
    }

    private String getSenha() {
        return Senha;
    }

    private void setSenha(String senha) {
        Senha = senha;
    }

    public int getAgencia() {
        return Agencia;
    }

    public int getConta() {
        return Conta;
    }

    public double getSaldo() {
        return Saldo;
    }

}

Class that performs the connection test:

import java.util.Scanner;

public class TesteO {

    public static void main(String[] args) throws Exception {
        while (true) {
            // Ler dados
            Scanner sc = new Scanner(System.in);
            System.out.println("Agencia");
            int agencia = sc.nextInt();
            System.out.println("Numero");
            int numero = sc.nextInt();
            System.out.println("Senha");
            String senha = sc.next();

            Conexao atm = new Conexao();
            Conta conta = atm.autenticacao(agencia, numero, senha);

             //exibe saldo atual

            System.out.println("Agencia = " + conta.getAgencia());
            System.out.println("Conta = " + conta.getConta());
            System.out.println("Saldo = " + conta.getSaldo());
            //System.out.println("Valor para deposito: ");
            //double valor = sc.nextDouble();
            //if (valor > 0) {
            //  atm.atualizaSaldo(saldo + valor);
            //}
            //saldo = atm.saldo();
            //System.out.println("Novo Saldo = " + saldo);

        }
    }
}

DB Structure (DERBY):

connect 'jdbc:derby://localhost:1527/ATM;create=true';
create table contas (
 agencia int not null,
 conta int not null,
 senha char(6) not null,
 saldo double not null,
 primary key (agencia,conta)
 );
insert into contas values(1,123,'123',1000);

insert into contas values(1,321,'321',1100);

 describe contas;

When executing the function the account object receives only the balance the other fields are 0 and a "Invalid Number" message appears that I believe is an SQL exception.

    
asked by anonymous 09.06.2016 / 15:17

1 answer

1

It seems to me that the logic of the setters methods is wrong and therefore the value is not assigned. Not because it does not return the bank's data.

Suppose you return the number 1 of the account, this setter method will run, you do not have else to handle, just because the account does not meet the condition. 1 is not less than zero and greater than 99999.

private void setConta(int conta) {
     if (conta < 0 && conta > 99999) {
          Conta = conta;
     }
}

Example: enter 1, as account, 1 is not between the IF range, what does the program do? Nothing, does not assign the value to the account attribute. Take the if and simply do this.Conta = account;

    
09.06.2016 / 18:46