sql - Fetch the data from database using the same column of same table using hash map -
i want fetch data database using hashmap.
example- table name menu
restaurant_id item_name price category 1101 burger 59 1101 pizza 101 1101 colddrink 40 b 1101 bread 30 b
output must this
category a
item_name price burger 59 pizza 101
category b
item_name price colddrink 40 bread 30
i want fetch data table menu jsp page.
please me . have tried many did'nt output need.
i've used arraylist
here give same output want code:
step 1: categories table_name_menu
table arraylist
public static arraylist<characters> getcategories(connection con) throws sqlexception { statement stmt = null; string query = "select distinct(category) table_name_menu order 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, categories come in categories arraylist stmt.close(); return categories; }
note: uncompiled code. please put try , catch blocks accordingly.
step 2: iterate through categories
arraylist , each of these categories, prepare new query fetch contents corresponding particular category
for(iterator<character> = category.iterator(); i.hasnext(); ) { statement stmt = null; stmt = con.createstatement(); char newcategory = i.next(); string query2 = "select item_name, price table_name_menu category = \"" + newcategory + "\""; resultset rs = stmt.executequery(query2); //this prints contents of 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 contents corresponding particular category.
Comments
Post a Comment