Use another class method to register in the database using DAO

Asked

Viewed 35 times

1

I have to record data in a database, but one of these data is calculated by another class, I wanted to know how to take the database data and put it in the method calculation and then record the result.

DAO

package persistencia;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import lojabrinquedos.Brinquedo;
import lojabrinquedos.Cliente;
import lojabrinquedos.ClienteBrinquedo;


public class ClienteBrinquedoDAO {
    private static Connection connection;
    private static PreparedStatement st;
    private static PreparedStatement st1;
    private static ResultSet rs;
    private static ResultSet rs1;

    public static int gravaPorCliente(int idBrinquedo, int idCliente, String data, int quantidade, double valorTotal, int matricula) throws Exception {
        int ret = 0;
        ClienteBrinquedo cb = new ClienteBrinquedo();
        Brinquedo brinquedo = new Brinquedo();
        try {
            String sql = "INSERT INTO clientebrinquedo (Brinquedo_idBrinquedo, Cliente_idCliente, data, quantidade, valorTotal, Funcionario_matricula) VALUES (?,?,?,?,?,?)";
            String sql2 = "Select * FROM Brinquedo where idBrinquedo = ?";
            connection = GerenteDeConexao.getConnection();
            st = connection.prepareStatement(sql);
            st1 = connection.prepareStatement(sql2);
            st1.setInt(1, idBrinquedo);
            rs1 = st1.executeQuery();
            if (rs1.next()) {
                brinquedo.setPreco(rs1.getInt("preco"));
                Brinquedo.setDesconto(rs1.getInt("desconto"));
            }
            st.setInt(1, idCliente);
            st.setInt(2, idBrinquedo);
            st.setString(3, data);
            st.setInt(4, quantidade);
            st.setDouble(5, cb.calculaValorTotal());
            st.setInt(6, matricula);
            ret = st.executeUpdate();
            st.close();
        } catch (SQLException e) {
            System.out.println(e.getMessage());
        }
        return ret;
    }

}

Calculus method

    public double calculaValorBrinquedo() {
        return quantidade * Brinquedo.preco;
    }

    public double converterDesconto() {
        return (100 - Brinquedo.getDesconto())/100;
    }

    public double adicionarImpostos() {
        return calculaValorBrinquedo() + (Brinquedo.preco * impostos);
    }

    public double adicionarLucro() {
        return adicionarImpostos() + (Brinquedo.preco * lucro);
    }

    public double calculaValorTotal() {
        return valorTotal = adicionarLucro() * converterDesconto();
    }

I tried this code but it’s only logged 220.

1 answer

1

Karlos, I do not understand very well what you want to do. But I will try to explain here. If you do not understand can send your question again.

1- I do not know what data you want to calculate, but said they are in the bank. Then make a query in the bank (in your DAO class) in a method that has the return of the data type you want to search for.

2- In your calculation class, call the DAO class method to actually perform the query and return the value you want, example:

int idade = dao.buscaIdade()

3- In your calculation method receive the value as a parameter, example:

public void calculaSalario(int idade){
   //....... cálculo aqui
}

4-Store the calculation value in a variable.

5- To register the calculation value in the database, take the value as a method parameter in your DAO class, example:

public void insereValorCalculado(double salario){
   //...... sql aqui
}

If it got hard to understand, edit your question again!! Abç!!

Browser other questions tagged

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