有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何在计算后检索值?

我有一个方法来显示满足条件的表的值:如果alarmType != 60和最近的alarmType == 60,则在表tblFuel中显示所有值,我的所有代码如下:

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)中初始化了变量(alarmType)int alarmType = -1;//(1)initialize here*********************************************************1 然后检查给定条件并在(2)中进行计算,得到(3)中满意的值。我希望在(3)中输入alarmType的值

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)中检索到(4)的值?(换句话说:如何在while循环中获取alarmtype的值以放入SQL查询?)。(另一种方式)如果我在表中再添加一列来放置(3)中计算的值(通过在某处写入服务)是非常容易的,但是如果不这样做,我能做什么呢


共 (0) 个答案