尝试使用资源类变量获取null

时间:2017-05-29 06:53:57

标签: java exception exception-handling try-catch resultset

        protected void saveData() {
        Map<String, String> allStationsParams = new HashMap<>();
        List<String> stations = getAllStations();
        stmt = Database.getUpdateableStatement();
        today = (SysTime.currentTimeMillis() / DasStamp.TICKS_PER_DAY) * 
        DasStamp.TICKS_PER_DAY;
        String changeTimestamp = DasStamp.asCompactString(today);
        String keyName = "COM.MAPPINGTOOLTIP." + attributeValue;
        for (int row = 0; row < this.getTableModel().getRowCount(); row++) {
            String station = (String)this.getTableModel().getValueAt(row, 0);
            putInStationParams(this, station, allStationsParams, row);
        }
        for (String station : stations) {
            boolean sendToDB = false;
            try (ResultSet rs = this.rsParameters) {
                rs.beforeFirst();
                while (rs.next()) {
                    if (rs.getString("station").equals(station)) {
                        sendToDB = true;
                        break;
                    }
                }
                if (sendToDB) {
                    if (!rs.getString("value_text").equals(allStationsParams.get(station)) || !allStationsParams.containsKey(station)) {
                        sendToDB = true;
                    } else {
                        sendToDB = false;
                    }
                } else if (allStationsParams.containsKey(station)) {
                    sendToDB = true;
                }

                if (sendToDB) {
                    String  sql = "REPLACE INTO dss_parameter (key_name, station, valid_from, value_text"
                            + ", change_timestamp) VALUES ('"
                            + keyName + "','" + station + "','" + DasStamp.asDateOnlyString(today) + "','"
                            + Helper.nz(allStationsParams.get(station)) + "','"
                            + changeTimestamp + "') ;";
                    if (null != stmt) {
                        stmt.execute(sql);
                        if (!isResultSetEmpty(rs) && !rs.isAfterLast()) {
                            AdminLogger.log("dss_parameter", Action.UPDATE,
                                    "key_name='" + keyName + "' and station='" + station + "' and valid_from='" + DasStamp.asDateOnlyString(today) + "'",
                                    "value_text='" + rs.getString("value_text") + "'",
                                    "value_text='" + Helper.nz(allStationsParams.get(station)) + "', change_timestamp='" + changeTimestamp + "'");
                        } else {
                            AdminLogger.log("dss_parameter", Action.INSERT,
                                    "key_name='" + keyName + "' and station='" + station + "' and valid_from='" + DasStamp.asDateOnlyString(today) + "'",
                                    "", "value_text='" + Helper.nz(allStationsParams.get(station)) + "', change_timestamp='" + changeTimestamp + "'");
                        }
                    }
                }
            } catch (SQLException e) {
                AppFrame.msgBox("Error on insert: " + e.getMessage());
                Helper.printMessage(true, false, "Parameter save failed!!", e);
            }
        }
    }

其中rsParameters是类级别并且之前被提取。先是    迭代,rsParameters值变为null。这是try的问题    资源块?请帮忙

其中rsParameters是类级别并且之前被提取。先是    迭代,rsParameters值变为null。这是try的问题    资源块?请帮忙

1 个答案:

答案 0 :(得分:0)

您的rsParameters参数属于Resultset类型。 在第一次迭代中,在try {}块完成后,rsParameters的close()方法:调用ResultSet。

这在内部产生了resultSet NUll的所有属性。

这就是在第二次迭代中获取Null属性的原因。

参见:http://grepcode.com/file/repo1.maven.org/maven2/mysql/mysql-connector-java/5.1.27/com/mysql/jdbc/ResultSetImpl.java#ResultSetImpl.realClose%28boolean%29