Problems getting value from Resultset

Asked

Viewed 355 times

1

I’m trying to get the values of a select in the derby but they just returns correctly a field from Resulteset. Follow DB access class code:

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 Object Counts:

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

        }
    }
}

Structure of DB(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 set to 0 and a "Invalid Number" message appears which I believe to be an SQL Exception.

  • A question? why use String.format to replace fields in select if you already have Preparedstatement?

1 answer

1

It seems to me that the logic of the setters methods is wrong and so the value is not assigned. Not because it does not return the database data.

Let’s assume that returns the number 1 of the account, will be executed this method Setter, has no Lse to treat, 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 the program does? Nothing, does not assign the value to the account attribute. Take the if and simply do this.Account = Account;

Browser other questions tagged

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