[英]how to add values from a MySQL result-set while loop in java?
I want to add the values of integer column "priority" obtained from the MySQL db by iterating resultset using while loop. 我想通过使用while循环迭代结果集来添加从MySQL db获得的整数列“ priority”的值。 My piece of code is as below: 我的代码如下:
int TotalWestPriority = 0;
int WestPty = 0;
float lat = 0;
float lon = 0;
float Wb_SWLat = 0; // Currently holds some value from other process
float Wb_NELat = 0; // Currently holds some value from other process
float Wb_SWLon = 0; // Currently holds some value from other process
float Wb_NELon = 0; // Currently holds some value from other process
//SQL Query:
String qryVcl = "select priority, latitude, longitude from tbl_vcl";
ResultSet Vp=stmt.executeQuery(qryVcl);
while(Vp.next()){
lat = Vp.getFloat("latitude");
lon = Vp.getFloat("longitude");
System.out.println("Total Lat received:"+lat);
if(lat >=Wb_SWLat && lat <=Wb_NELat && lon>=Wb_SWLon && lon<=Wb_NELon){
WestPty = Vp.getInt("priority");
System.out.println("West Priority:"+WestPty);
}
}
Here, I'am able to print the result:- 在这里,我可以打印结果:
West Priority:3 西部优先:3
West Priority:2 西部优先:2
I want to add those values and store in an integer. 我想将这些值相加并存储为整数。
how to add all the "westpty" from the iteration to "TotalWestPriority" ? 如何将迭代中的所有“ westpty”添加到“ TotalWestPriority”?
Just accumulate them to another variable: 只需将它们累加到另一个变量中:
long total = 0L;
while (vp.next()) {
lat = vp.getFloat("latitude");
lon = vp.getFloat("longitude");
if (lat >= Wb_SWLat && lat <= Wb_NELat &&
lon >= Wb_SWLon && lon <= Wb_NELon) {
westPty = Vp.getInt("priority");
System.out.println("West Priority:"+WestPty);
total += westPty;
}
}
System.out.println("Total west priority: " + total);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.