gpt4 book ai didi

java - 如何在 java 中循环时从 MySQL 结果集中添加值?

转载 作者:行者123 更新时间:2023-11-29 05:09:55 27 4
gpt4 key购买 nike

我想通过使用 while 循环迭代结果集来添加从 MySQL 数据库获得的整数列“优先级”的值。我的一段代码如下:

    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);
}
}

在这里,我可以打印结果:-

西部优先级:3

西部优先级:2

我想将这些值相加并存储在一个整数中。

如何将迭代中的所有“westpty”添加到“TotalWestPriority”?

最佳答案

只需将它们累加到另一个变量:

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);

关于java - 如何在 java 中循环时从 MySQL 结果集中添加值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41209515/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com