无法将数据库结果存储到Map中

时间:2014-06-07 09:12:19

标签: java sql map linked-list resultset

我已经如上所示进行了查询,这使我获得了以下结果

mysql> SELECT DISTINCT category_id  , T1 FROM ca
+-------------+--------------------+
| category_id | T1                 |
+-------------+--------------------+
|           1 | Popcorn            |
|           2 | Popcorn            |
|           3 | Popcorn            |
|           4 | Popcorn            |
|           5 | Popcorn            |
|           6 | Popcorn            |
|           7 | Soft Drinks        |
|           8 | Soft Drinks        |
|           9 | Soft Drinks        |
|          10 | Soft Drinks        |

对于每个T1 coulmn,我正在尝试存储category_id

以便它看起来像

Popcorn=[
        1,
        2,
        3,
        4,
        5,
        6,
]

 SoftDrinks=[
        7,
        8,
        9,
        10,

]

我已按照以下方法完成此操作

Map<String,LinkedList<Integer>> categoryitemslist = new HashMap<String,LinkedList<Integer>>();

    PreparedStatement stmt2 = connection.prepareStatement("SELECT DISTINCT category_id  , T1 FROM categories  ;");
                   ResultSet rs2 = stmt2.executeQuery();
            LinkedList<Integer> llist = new LinkedList<Integer>();
            while(rs2.next())
            {
                int category_id = (int)rs2.getInt("category_id");
                llist.add(category_id);
                categoryitemslist.put(rs2.getString("T1"), llist);
            }

有人能告诉我这个错误吗?

我获得的结果是

{

    Popcorn=[
        1,
        2,
        3,
        4,
        5,
        6,
        7,
        8,
        9,
        10,
        11,
        12,
        13,
        14,
        15,
        16,
        17,
        18,
        19,
        20,
        21,
        22,
        23,
        24,
        25,
        26,
        27,
        28,
        29,
        30,
        31,
        32,
        33,
        34,
        35,
        36,
        37,
        38,
        39,
        40,
        41,
        42,
        43,
        44,
        45,
        46,
        47,
        48,
        49,
        50
    ],


    SoftDrinks=[
        1,
        2,
        3,
        4,
        5,
        6,
        7,
        8,
        9,
        10,
        11,
        12,
        13,
        14,
        15,
        16,
        17,
        18,
        19,
        20,
        21,
        22,
        23,
        24,
        25,
        26,
        27,
        28,
        29,
        30,
        31,
        32,
        33,
        34,
        35,
        36,
        37,
        38,
        39,
        40,
        41,
        42,
        43,
        44,
        45,
        46,
        47,
        48,
        49,
        50
    ],

}

2 个答案:

答案 0 :(得分:2)

现在,您有一个包含所有已提取ID的列表,并且您为每个T1添加了此列表,而您需要为每个唯一T1创建一个新列表,将结果添加到相应的列表中:

while (rs2.next()) {
    int cid = rs2.getInt("category_id");
    String t1 = rs2.getString("T1");

    if (!categoryItemsList.containsKey(t1)) {
        categoryItemsList.put(t1, new LinkedList<Integer>());
    }

     categoryItemsList.get(t1).add(cid);
}

答案 1 :(得分:1)

您应该阅读此documentation

所以你已经初始化了一个空的地图

Map<String,LinkedList<Integer>> categoryitemslist = new HashMap<>();

当您致电get()时,如果该密钥不存在,则会返回空值。

你的错误

  • 您只使用一个链表。这意味着将使用相同的值(您的情况下的值列表)映射所有键。我很确定这不是你的意图。
  • 您无法检查密钥是否存在值,这是避免NPE非常重要的一部分。

解决方案:

LinkedList<Integer> llist = new LinkedList<Integer>(); //remove this line
while(rs2.next()){
    int category_id = (int)rs2.getInt("category_id");
    String key = rs2.getString("T1");

    //get value
    LinkedList<Integer> llist = categoryitemslist.get(key);

    //check if value exists i.e. if value isn't null, else create a new value
    if(value == null){
        llist = new LinkedList<Integer>();
    }

    //now we're sure value isn't null
    llist.add(category_id);
    categoryitemslist.put(key, llist);
}