How to get the sum of a Bigdecimal column

Asked

Viewed 148 times

0

I need to add a data column BigDecimal Mysql, which has already been filtered from a given range. I am trying with SELECT SUM(preco_total) FROM venda; in the code below, but I’m not getting results.

Here are my codes:

Table structure venda:

> `DESCRIBE venda;`
+-----------------+---------------+------+-----+---------+----------------+
| Field           | Type          | Null | Key | Default | Extra          |
+-----------------+---------------+------+-----+---------+----------------+
| codigo          | bigint(20)    | NO   | PRI | NULL    | auto_increment |
| bairro          | varchar(50)   | YES  |     | NULL    |                |
| cep             | varchar(30)   | YES  |     | NULL    |                |
| cidade          | varchar(30)   | YES  |     | NULL    |                |
| complemento     | varchar(30)   | YES  |     | NULL    |                |
| estado          | varchar(2)    | YES  |     | NULL    |                |
| horario         | datetime      | NO   |     | NULL    |                |
| logradouro      | varchar(40)   | YES  |     | NULL    |                |
| nome_cliente    | varchar(80)   | YES  |     | NULL    |                |
| preco_total     | decimal(10,2) | NO   |     | NULL    |                |
| vendedor_codigo | bigint(20)    | NO   | MUL | NULL    |                |
+-----------------+---------------+------+-----+---------+----------------+
11 rows in set (0.01 sec)

My code vendaBean:

public void soma() {
        try {
            VendaDAO vendaDAO = new VendaDAO();
            vendaDAO.soma();

        } catch (RuntimeException erro) {
            Messages.addGlobalError("Não foi possível exibir a soma. Contate o Suporte.");
        }
    }

}

Below is my code vendaDAO;

public List<Venda> soma() {
        List<Venda> vendas = null;
        Session sessao = HibernateUtil.getFabricaDeSessoes().openSession();
        try {
            StringBuilder sql = new StringBuilder();
            sql.append("SELECT SUM(preco_total) FROM venda ");

        } catch (RuntimeException erro) {
            throw erro;
        } finally {
            sessao.close();
        }
        return vendas;
    }

}

And finally my code venda.xhtml:

<p:outputLabel value="Total: R$" />
                <h:outputText value="#{vendaBean.soma()}">
                    <f:convertNumber locale="pt_BR"                     minFractionDigits="2" />
                </h:outputText>
  • In the VendaDao you create the query but do not execute, is that right or was it missing to put this code? Also, if the method soma will return a number (the sum of prices), the return of it should not be a list. And on VendaBean, the method soma is void (does not return any value), so nothing will be printed in xhtml.

  • Ok. Thank you so much I will fix. Missed the query? Could you give me a hint please? This is the first code to add a previously filtered column that I’m developing.

  • You created the Session and the StringBuilder with the query, it’s just join together <- this link has some examples, but if you do not know how to run a query and get the result, I suggest searching for tutorials of Hibernate (in Google, since requests for tutorials in this site are outside the scope). If after reading the tutorials, try and fail and you have some more specific question, you can come back here and [Edit] the question, detailing your new difficulty

  • 1

    Very grateful for the tip I’ll do as you said.

  • I have returned its edition to the Revision 4, because its last edition deleted the whole question and left it with very low quality.

No answers

Browser other questions tagged

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