我有一个方法来显示满足条件的表的值:显示表tblFuel中的所有值alarmType != 60
和最近的alarmType == 60
,我的所有代码都在这里:
public ArrayList<FuelData> BaoCaoDoXang(String accountID, String deviceID,
String fromTime, String toTime, String timezone)
throws DBException, SQLException {
ArrayList<FuelData> list = new ArrayList<FuelData>();
ResultSet rs = null;
DBConnection dbc = null;
Statement stmt = null;
FuelData reportFuel;
int alarmType = -1;//(1)initialize here*********************************************************1
try {
dbc = DBConnection.getDefaultConnection();
long epf = ConvertToEpoch(fromTime + " 00:00:00", timezone);
long epl = ConvertToEpoch(toTime + " 23:59:59", timezone);
String sql = "SELECT * "
+ " FROM tblFuel F INNER JOIN tblDoXang DX "
+ " ON DX.thoiGian = F.timestamp "
+ " where (F.timestamp BETWEEN " + epf + " and " + epl
+ ") " + " and F.accountID = '" + accountID
+ "' and F.deviceID = '" + deviceID
+ "' and F.alarmType = '" + alarmType//expectation value(4)*************************4
+ "' order by F.timestamp asc";
stmt = dbc.execute(sql);
rs = stmt.getResultSet();
double lastValue = 0;
int temp = -1;
while (rs.next()) {
// double fuel = rs.getDouble("nhienLieu");
temp = rs.getInt("alarmType");
if(temp != 60)
alarmType = temp;
if(temp == 60 && alarmType != -1)//(2)alarmType value after calculation*****************2
{
double currentValue = rs.getDouble("fuelLevel");
double changeValue = lastValue == 0 ? 0 : currentValue
- lastValue;
lastValue = currentValue;
reportFuel = new FuelData(rs.getString("accountID"),
rs.getString("deviceID"), rs.getInt("timestamp"),
rs.getDouble("latitude"), rs.getDouble("longitude"),
rs.getString("address"), currentValue,
rs.getDouble("odometerKM"), rs.getInt("status"),
changeValue,alarmType ,//(3)here************************************************3
rs.getDouble("nhienLieu"));
list.add(reportFuel);
/*
* if(fuel > 0){ changeValue = fuel; }
*/
}
}
} catch (SQLException sqe) {
throw new DBException("ReportByStatusCode", sqe);
} finally {
if (rs != null) {
try {
rs.close();
} catch (Throwable t) {
}
}
if (stmt != null) {
try {
stmt.close();
} catch (Throwable t) {
}
}
DBConnection.release(dbc);
}
return list;
}
我在(1)int alarmType = -1;//(1)initialize here*********************************************************1
中初始化了我的变量(alarmType)
然后检查给定条件并在(2)中计算并得到(3)中的满意值。我希望将alarmType的值放在(3)
double lastValue = 0;
int temp = -1;
while (rs.next()) {
// double fuel = rs.getDouble("nhienLieu");
temp = rs.getInt("alarmType");
if(temp != 60)
alarmType = temp;
if(temp == 60 && alarmType != -1)//(2)alarmType value after calculation*****************2
{
double currentValue = rs.getDouble("fuelLevel");
double changeValue = lastValue == 0 ? 0 : currentValue
- lastValue;
lastValue = currentValue;
reportFuel = new FuelData(rs.getString("accountID"),
rs.getString("deviceID"), rs.getInt("timestamp"),
rs.getDouble("latitude"), rs.getDouble("longitude"),
rs.getString("address"), currentValue,
rs.getDouble("odometerKM"), rs.getInt("status"),
changeValue,alarmType ,//(3)here************************************************3
rs.getDouble("nhienLieu"));
list.add(reportFuel);
/*
* if(fuel > 0){ changeValue = fuel; }
*/
}
}
到(4)然后执行查询
String sql = "SELECT * "
+ " FROM tblFuel F INNER JOIN tblDoXang DX "
+ " ON DX.thoiGian = F.timestamp "
+ " where (F.timestamp BETWEEN " + epf + " and " + epl
+ ") " + " and F.accountID = '" + accountID
+ "' and F.deviceID = '" + deviceID
+ "' and F.alarmType = '" + alarmType//expectation value(4)*************************4
+ "' order by F.timestamp asc";
。我已检查并认识到(3)中的值返回为真,但似乎(4)(alarmType)中的值来自(1)(alarmType = -1),而不是来自(3)所以我的代码返回错误的结果。所以我的问题是:如何从(3)中正确检索值到put(4)?(换句话说:如何在while循环中获取alarmtype
的值以放入SQL查询?)。(如果我在我的表中添加一个列来放置(3)中计算的值(通过在某处编写服务),这很容易,但是我能做什么而不用这样做?
答案 0 :(得分:0)
执行类似
的操作alarmType
loopingConstruct {
sql String
execute sql string to get result set (rs)
loop through rs
get alarm type value and set it to alarm type
create FuelData object
add FuelData to the list
}
循环构造上下文在执行期间保存alarmType
的修改值,将在sql String
中使用。
假设 - 返回的结果集(rs
)仅包含一行。如果不是这种情况,那么alarmType
的值将设置为rs
中最后一行返回的值。