I have problem, this is my code :
file GenericCategories.java
package com.menu.dao.generic;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.menu.entities.Categories;
import com.menu.entities.Menu;
import com.menu.includes.DBConnect;
public class GenericCategories {
public List SelectAllC() throws Exception {
List sac = new ArrayList();
PreparedStatement ps = null;
ResultSet rs = null;
PreparedStatement ps1 = null;
ResultSet rs1 = null;
DBConnect db = new DBConnect();
Connection con = db.getConnect();
String sql = "SELECT id, category FROM categories";
try {
ps = con.prepareStatement(sql);
rs = ps.executeQuery();
while(rs.next()){
Categories c = new Categories();
c.setCategory(rs.getString("category"));
c.setId(rs.getInt("id"));
String sql2 = "SELECT m.id, m.menu FROM menu m, categories c WHERE c.id = m.cat AND m.cat = ?";
ps1 = con.prepareStatement(sql2);
ps1.setInt(1, c.getId());
rs1 = ps1.executeQuery();
while(rs1.next()) {
Menu m = new Menu();
m.setMenuid(rs1.getInt("id"));
m.setMenu(rs1.getString("menu"));
///// // How can i code here ... ////////////
}
sac.add(c);
}
} finally {
}
return sac;
}
}
and my Categories.java:
package com.menu.entities;
public class Categories {
private int id;
private String category;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
}
file Menu.java
package com.menu.entities;
public class Menu {
private int menuId;
private String menu;
public int getMenuid() {
return this.menuId;
}
public void setMenuid(int menuId) {
this.menuId = menuId;
}
public String getMenu() {
return this.menu;
}
public void setMenu(String menu) {
this.menu = menu;
}
}
and index.java
package com.menu;
import java.io.*;
import java.util.*;
import freemarker.template.*;
import javax.servlet.*;
import javax.servlet.http.*;
import com.menu.dao.generic.GenericCategories;
import com.menu.dao.generic.GenericMenu;
public class Index extends HttpServlet {
private Configuration cfg;
public void init() {
cfg = new Configuration();
cfg.setServletContextForTemplateLoading(getServletContext(), "templates");
cfg.setObjectWrapper(ObjectWrapper.BEANS_WRAPPER);
}
public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
res.setContentType("text/html");
res.setCharacterEncoding("utf-8");
Template t = cfg.getTemplate("demo.html");
Map root = new HashMap();
GenericCategories gc = new GenericCategories();
GenericMenu gm = new GenericMenu();
try {
root.put("allCategories", gc.SelectAllC());
} catch (Exception e1) {
System.out.println(e1);
}
Writer out = res.getWriter();
try {
t.process(root, out);
} catch (TemplateException te) {
te.printStackTrace();
}
}
}
I want list categories two level, i need you help, thank you very much ...