如何在java中循环时从MySQL结果集中添加值?
我想通过使用while循环迭代resultset来添加从MySQL数据库获得的整数列“priority”的值。我的代码如下:如何在java中循环时从MySQL结果集中添加值?,java,mysql,jdbc,resultset,addition,Java,Mysql,Jdbc,Resultset,Addition,我想通过使用while循环迭代resultset来添加从MySQL数据库获得的整数列“priority”的值。我的代码如下: 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
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);
}
}
int TotalWestPriority=0;
int-WestPty=0;
浮动lat=0;
float-lon=0;
浮动Wb_SWLat=0;//当前保存其他进程的一些值
浮动Wb_NELat=0;//当前保存其他进程的一些值
浮点数Wb_SWLon=0;//当前保存其他进程的一些值
浮动Wb_NELon=0;//当前保存其他进程的一些值
//SQL查询:
String qryVcl=“从tbl\U vcl中选择优先级、纬度、经度”;
结果集Vp=stmt.executeQuery(qryVcl);
while(Vp.next()){
纬度=Vp.getFloat(“纬度”);
lon=Vp.getFloat(“经度”);
系统输出打印项次(“收到的总Lat:+Lat”);
如果(lat>=Wb_SWLat&&lat=Wb_SWLon&&lon只需将它们累加到另一个变量:
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);
long总计=0L;
while(vp.next()){
纬度=vp.getFloat(“纬度”);
lon=vp.getFloat(“经度”);
如果(lat>=Wb_SWLat&&lat=Wb_SWLon&&lon嗨,我只想将结果集迭代的while循环中的值相加,并存储在整数中。任何代码都将受到高度赞赏并提前感谢。你的意思是想将每次迭代中检索到的所有值相加,如3+2=5
?@pawneetsigh是的,你说得对太简单了,WestPty=WestPty+Vp.getInt(“优先级”);
@PavneetSingh我会试试的。谢谢