4.22

/**
* 建立站点名称和对应的ID的哈希表
*/
public static Map<String, Integer> buildStationIdMap() {
Connection connection = JDBCUtils.getConn();
Map<String, Integer> stationIds = new HashMap<>();

try {
String sql = "SELECT Starting_Station, Terminus_Station FROM station ";

// 从数据库中检索起始站和终点站
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(sql);

int currentId = 0;

// 填充stationIds map 并找到最大的站点ID
while (resultSet.next()) {
String startingStation = resultSet.getString("Starting_Station");
String terminusStation = resultSet.getString("Terminus_Station");
if (!stationIds.containsKey(startingStation)) {
stationIds.put(startingStation, currentId++);
}
if (!stationIds.containsKey(terminusStation)) {
stationIds.put(terminusStation, currentId++);
}
}

} catch (SQLException e) {
e.printStackTrace();
// 在这里处理任何可能的异常...
} finally {
// 关闭资源
try {
if (connection != null) {
connection.close();
}
// 关闭 statement 和 resultSet
} catch (SQLException e) {
e.printStackTrace();
}
}

// 返回填充后的站点名称和对应的ID的哈希表
return stationIds;
}
posted @ 2024-06-13 16:04  kxzzow  阅读(10)  评论(0)    收藏  举报