使用哈希映射使用相同表的同一列从数据库中获取数据

时间:2022-03-17 07:40:32

I want to fetch the data from database using hashmap.

我想使用hashmap从数据库中获取数据。

Example- Table Name Menu

示例 - 表名称菜单

Restaurant_ID Item_Name Price Category
    1101        Burger    59    A
    1101        pizza     101   A
    1101        colddrink  40   B
    1101        bread      30   B

Output must be like this

输出必须是这样的

Category A

Item_name      Price
Burger          59
Pizza           101

Category B

Item_name      Price
colddrink       40
bread           30

I want to fetch the data like this from table MENU to my jsp page.

我想从表MENU到我的jsp页面获取这样的数据。

Please help me . i have tried so many this but i did'nt get the output as i need.

请帮我 。我已经尝试了这么多,但我没有得到我需要的输出。

1 个解决方案

#1


1  

I've used ArrayList here but it will give the same output as you want from your code:

我在这里使用过ArrayList,但它会从你的代码中提供你想要的相同输出:

Step 1: Get all the categories from Table_Name_Menu table into an ArrayList

步骤1:从Table_Name_Menu表中获取所有类别到ArrayList

public static ArrayList<Characters> getCategories(Connection con) throws SQLException {

    Statement stmt = null;
    String query =
        "select distinct(Category) from Table_Name_Menu order by Category ASC";

    stmt = con.createStatement();
    ResultSet rs = stmt.executeQuery(query);
    ArrayList<Character> categories = new ArrayList<Character>();

    while (rs.next()) {
        char category = rs.getString("Category").charAt(0);
        categories.add(category);
    }
    //This way, all the categories will come in categories Arraylist

    stmt.close();
    return categories;
}

Note: This is an Uncompiled code. Please put Try and Catch blocks accordingly.

注意:这是一个未编译的代码。请相应地放置Try和Catch块。

Step 2: Iterate through your categories ArrayList and for each of these categories, prepare a new query that will fetch all the contents corresponding to that particular Category

步骤2:遍历您的类别ArrayList,并为每个类别准备一个新查询,该查询将获取与该特定类别对应的所有内容

for(Iterator<Character> i = category.iterator(); i.hasNext(); ) {
    Statement stmt = null;
    stmt = con.createStatement();

    char newCategory = i.next();
    String query2 = "select Item_name, Price from Table_Name_Menu where Category = \"" +  newCategory + "\"";
    ResultSet rs = stmt.executeQuery(query2);

    //This prints the contents of the new Category
    System.out.println("Category : " + newCategory):
    while(rs.next()){
        System.out.println(rs.getString(1)); //Item Name
        System.out.println(rs.getString(2)); //Item Price
    }
    stmt.close();

    System.out.println("\n\n"); //Gap Between categories
}

This way you'll get all the contents corresponding to a particular category.

这样您就可以获得与特定类别相对应的所有内容。

#1


1  

I've used ArrayList here but it will give the same output as you want from your code:

我在这里使用过ArrayList,但它会从你的代码中提供你想要的相同输出:

Step 1: Get all the categories from Table_Name_Menu table into an ArrayList

步骤1:从Table_Name_Menu表中获取所有类别到ArrayList

public static ArrayList<Characters> getCategories(Connection con) throws SQLException {

    Statement stmt = null;
    String query =
        "select distinct(Category) from Table_Name_Menu order by Category ASC";

    stmt = con.createStatement();
    ResultSet rs = stmt.executeQuery(query);
    ArrayList<Character> categories = new ArrayList<Character>();

    while (rs.next()) {
        char category = rs.getString("Category").charAt(0);
        categories.add(category);
    }
    //This way, all the categories will come in categories Arraylist

    stmt.close();
    return categories;
}

Note: This is an Uncompiled code. Please put Try and Catch blocks accordingly.

注意:这是一个未编译的代码。请相应地放置Try和Catch块。

Step 2: Iterate through your categories ArrayList and for each of these categories, prepare a new query that will fetch all the contents corresponding to that particular Category

步骤2:遍历您的类别ArrayList,并为每个类别准备一个新查询,该查询将获取与该特定类别对应的所有内容

for(Iterator<Character> i = category.iterator(); i.hasNext(); ) {
    Statement stmt = null;
    stmt = con.createStatement();

    char newCategory = i.next();
    String query2 = "select Item_name, Price from Table_Name_Menu where Category = \"" +  newCategory + "\"";
    ResultSet rs = stmt.executeQuery(query2);

    //This prints the contents of the new Category
    System.out.println("Category : " + newCategory):
    while(rs.next()){
        System.out.println(rs.getString(1)); //Item Name
        System.out.println(rs.getString(2)); //Item Price
    }
    stmt.close();

    System.out.println("\n\n"); //Gap Between categories
}

This way you'll get all the contents corresponding to a particular category.

这样您就可以获得与特定类别相对应的所有内容。