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.
– user28595
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.
– Lizy