How to retrieve values from Result set and use it for calculations

╄→гoц情女王★ 提交于 2019-12-23 05:41:05

问题


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,

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"); has retrieved values 248.36, 125.36, 452.36, 578.35, 456.77


回答1:


You should do it as follows:

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.



来源:https://stackoverflow.com/questions/59158463/how-to-retrieve-values-from-result-set-and-use-it-for-calculations

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!