如何从结果集中检索值并将其用于计算 [英] How to retrieve values from Result set and use it for calculations
问题描述
我正在使用Java黄瓜,我想计算税收。为此,我要连接到数据库并从数据库中获取总金额。
以下是结果集返回的养老金总值
I am using cucumber with Java and I want to calculate the tax. To do so I am connecting to data base and fetching the Gross Amount from data base. Below are the Gross pension values which result set has returned
248.36, 125.36,452.36,578.35,456.77,
但是现在仅针对最后一个值(即456.77)计算税。我想对所有价值观。我该怎么做?下面是我尝试过的代码
But now tax is getting calculated only for last value i.e, 456.77.I want to calculate tax for all the values. How do I do it? Below is the code which I have tried
while(rs.next()) {
//To retrieve the first column
GrossPension = rs.getFloat("GrossPension");
log.info("GrossPension is :" +GrossPension);
float tax = this.GrossPension/5;
System.out.println("Tax is: "+tax);
}
GrossPension = rs.getFloat( GrossPension) ;
已检索到值248.36、125.36、452.36、578.35、456.77
GrossPension = rs.getFloat("GrossPension");
has retrieved values 248.36, 125.36, 452.36, 578.35, 456.77
推荐答案
List<Float> grossPensionList = new ArrayList<Float>();
float grossPension;
while(rs.next()) {
//To retrieve the first column
grossPension = rs.getFloat("GrossPension");
log.info("GrossPension is :" +grossPension);
grossPensionList.add(grossPension/5);
}
for (float tax: grossPensionList){
System.out.println(tax);
}
逻辑是将从每一行获取的退休金总额添加到列表中并显示读取所有行后,该列表就会显示出来。
The logic is to add gross pension retrieved from each row to a list and display the list once all the rows are read.
这篇关于如何从结果集中检索值并将其用于计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!