繁体   English   中英

如何将 SQL 数据库中的数据放入 Hashmap?

[英]How do i put data from an SQL Database to a Hashmap?

我需要从 SQL 数据库中检索数据并将其放入 hashmap,我对如何做到这一点有点迷茫,尤其是对于我给定的数据类型,如果有人可以帮助并向我解释这个过程,那就太好了,这里是在我的代码下面:

public void load (Path p) throws DataLoadingException {
    try {
        
         String pathString = p.toString();
         Connection c = DriverManager.getConnection("jdbc:sqlite:"+pathString);
         Statement s = c.createStatement();
         ResultSet rs = s.executeQuery("select * from PassengerNumbers;");
        
          
            while (rs.next()) {
                LocalDate  date = LocalDate.parse(rs.getString("Date"));
                int  flightnumber = Integer.parseInt(rs.getString("FlightNumber"));
                int loadestimate = Integer.parseInt(rs.getString("LoadEstimate"));
            }
         
            rs.close();
            c.close();
            
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

SqlDb 中的数据如下所示:

(PK TEXT)             (PK INTEGER)                     (INTEGER)
   Date                FlightNumber                   LoadEstimate
2020-07-01               618                              124   

Hashmap 包含键值对并将每个唯一键映射到一个值。

根据 SQL 数据库信息,您提供的表具有复合主键,换句话说,您的主键由两列(TEXT 类型的日期和 INTEGER 类型的航班号)组成。

如您所知,主键具有唯一值,以帮助您在查询表中的数据时进行区分。 因此,您应该在 hashmap 中存储表的主键作为键。

现在,由于您的主键由两列组成,并且它们的值的组合可以帮助您识别唯一性,因此您可以创建一个数组或列表并将日期和 * flightNumber * 存储在那里。 然后,您将将此数组/列表作为键添加到 hashmap 中,并将所需字段的 rest(在我们的示例中为 INTEGER 类型的loadEstimate )作为其值。

上面的代码应该是这样的:

HashMap<ArrayList<Object>, int> myMap = new HashMap<>(); //Create your hashmap

while (rs.next()) {
    LocalDate  date = LocalDate.parse(rs.getString("Date"));
    int  flightnumber = Integer.parseInt(rs.getString("FlightNumber"));
    int loadestimate = Integer.parseInt(rs.getString("LoadEstimate"));

    //Create array resembling the primary key
    ArrayList<Object> mapKey = new ArrayList<>();
    mapKey.add(date);
    mapKey.add(new Integer(flightnumber));

    //Store to Map the key and the value
    myMap.put(mapKey, loadestimate);
}
//then do sth with the hashmap

请注意,我创建了一个类型为 Object 的通用对象数组,以便能够存储不同类型的对象。 这是可能的,因为它们都是 class Object 的子类。

此外,Java 中的安全模式是在您完成后在 finally 块中关闭您的 ResultSet、Statement 和 Connection(按此顺序)。 因此,在 catch 块之后添加这个 finally 块:

finally {
    try { rs.close(); } catch (Exception e) { /* ignored */ }
    try { s.close(); } catch (Exception e) { /* ignored */ }
    try { c.close(); } catch (Exception e) { /* ignored */ }
}

我的建议是创建一个 Flight class,您可以在其中将所有三个值存储为属性,这样您就可以将三个值存储在一个地方。 这使得可以将每个 Flight object 存储在 Hashmap<Integer,Flight> 中。 类似于下面的代码。

HashMap<Integer,Flight> hashMap = new HashMap<Integer,Flight>;
Integer key = 0;
while (rs.next()) {
            LocalDate  date = LocalDate.parse(rs.getString("Date"));
            int  flightnumber = Integer.parseInt(rs.getString("FlightNumber"));
            int loadestimate = Integer.parseInt(rs.getString("LoadEstimate"));
            Flight newFlight = new Flight(date,flightnumber,loadestimate);
            hashMap.put(key, newFlight);
            key++;
}

通过这种方式,您可以通过遍历 hashmap 来访问所有数据,因为您知道它的大小。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM