Problem with Jtable

Asked

Viewed 56 times

0

I got a problem with a jtable. I made a method in DAO that calculates a total of consumed products, this method is called on the Screen on the sign up button, every time I register an accurate consumption that will calculate the amount according to the value of the product and shows a total value. Actually it is calculating but only in the first registration, if I register other consumptions, what shows me on the screen is only the first consumption, if I delete others and let only the consumption of the first row of the table is only the one that will be visualized. Below the method code.

DAO method:

public Object mostra(String buscar) {

    Consumo consumoVO = new Consumo();
    ConsumoDAO consumoDAO = new ConsumoDAO();
    ModeloTabelaConsumo modelo = new ModeloTabelaConsumo();
    totalConsumo = 0.0;

    String sql = ("SELECT c.codConsumo, c.codHospedagem, c.codProduto, p.nomeProduto, " +
                  " c.quantidade, c.valorConsumo, c.status " +
                  " from consumo c " +
                  " INNER JOIN produto  p ON c.codProduto = p.codProduto " +
                   "INNER JOIN hospedagem AS H ON H.codHospedagem = C.codHospedagem " 
                 + " WHERE c.codHospedagem = " + buscar + " order by c.codConsumo ");

    getBanco().abrir();
    try {
        Statement stm = getBanco().getConexao().createStatement();
        //Faz a leitura no banco
        ResultSet rs = stm.executeQuery(sql);

        if (rs.next() == true) { //Achou
            consumoVO = new Consumo();
            consumoVO.setCodConsumo(rs.getInt("codConsumo"));
            consumoVO.setCodHospedagem(rs.getInt("codHospedagem"));
            consumoVO.setCodProduto(rs.getInt("codProduto"));
            consumoVO.setCodProduto(rs.getInt("nomeProduto"));                               
            consumoVO.setQuantidade(rs.getInt("quantidade"));
            consumoVO.setValorConsumo(rs.getDouble("valorConsumo"));
            consumoVO.setStatus(rs.getString("status"));

            totalConsumo = totalConsumo + (rs.getInt("quantidade") * rs.getDouble("valorConsumo"));

           } 

    } catch (SQLException ex) {
        ex.printStackTrace();
    }
    return consumoVO;
}

Method of Screen Consumption:

void mostrar(String buscar){
   consumoDAO.mostra(buscar);     
    lblTotalConsumo.setText("Valor R$. " + consumoDAO.totalConsumo);

}   
  • Add a [mcve]. Not to analyze only with the code informed.

  • Obg Diego F! This method is working, but I believe you are not seeing the add lines in the table and do not know how to do, register new consumptions, but the calculation appears only in the first registration I do, or in the first line, if I delete other consumptions.

1 answer

1

What happened there is a logic error. The problem is in your if to check if you found the item, but actually in place of the if should be a while. So it only returns the value of the first item. With the while your totalConsumo will return correctly.

How are you: if (rs.next()) { //Achou

As it should be: while (rs.next()) { //Achou

And as our colleague Diego F. reported, there is no need to compare with true, because the method next() already returns a boolean.

  • No need to compare with true,, resulset.next() will always return a boolean, true when there is another "line", false when the cursor is over. Therefore, this is redundant rs.next() == true

  • really, because rs.next() already returns a Boolean. .

  • Thank you Bruno Silva!!!!! It worked!! I’m sorry I’m beginner in java!!!

  • Imagine Lizy, any doubt just ask.

  • Just put that my answer solved your problem, please :)rs

Browser other questions tagged

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