创建一个超市软件,当我进入添加产品页面时,如果尚未添加类别和品牌,则页面重定向到类别添加并 品牌添加页面。
表格中已有数据,但页面重定向到添加类别页面。
类别的 UserDAO
PreparedStatement ps =null;
String query = "SELECT CategoryId, CategoryName from tbl_category";
List <CategoryModel> categories = new ArrayList<CategoryModel>();
CategoryModel categoryModel = new CategoryModel();
CategoryModel u = null;
try{
ps = con.prepareStatement(query);
ps.setString(1, categoryModel.getCategoryId());
ps.setString(2, categoryModel.getCategoryName());
ResultSet rs = ps.executeQuery();
while(rs.next()){
u.setCategoryId(rs.getString("CategoryId"));
u.setCategoryName(rs.getString("CategoryName"));
categories.add(u);
}
}catch(Exception e){
e.printStackTrace();
return null;
}
return categories;
}
品牌的 UserDAO
PreparedStatement ps =null;
String query = "SELECT BrandId, BrandName from tbl_brand";
List <BrandModel> brands = new ArrayList<BrandModel>();
BrandModel brandModel = new BrandModel();
BrandModel u = null;
try{
ps = con.prepareStatement(query);
ps.setString(1, brandModel.getBrandId());
ps.setString(2, brandModel.getBrandName());
ResultSet rs = ps.executeQuery();
while(rs.next()){
u.setBrandId(rs.getString("BrandId"));
u.setBrandName(rs.getString("BrandName"));
brands.add(u);
}
}catch(Exception e){
e.printStackTrace();
return null;
}
return brands;
}
用于检索数据和重定向的 Servlet
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
session = request.getSession();
List<BrandModel> brands=null;
List<CategoryModel> categories=null;
UserDAOImplementation brand = new UserDAOImplementation();
brands = brand.brands();
UserDAOImplementation category = new UserDAOImplementation();
List<CategoryModel> catagories = category.catagories();
if(category==null){
response.sendRedirect("views/products/Category.jsp");
}else if(brands==null){
response.sendRedirect("views/products/Brands.jsp");
}else if(categories.isEmpty()){
response.sendRedirect("views/products/Category.jsp");
}else if(brands.isEmpty()){
response.sendRedirect("views/products/Brands.jsp");
}else{
session.setAttribute("brands", brand);
session.setAttribute("categories", category);
response.sendRedirect("views/products/Product.jsp");
}
}
最佳答案
PreparedStatement ps =null; String query = "SELECT CategoryId, CategoryName from tbl_category";
List <CategoryModel> categories = new ArrayList<CategoryModel>(); CategoryModel categoryModel = new CategoryModel();
CategoryModel u = null; try{
ps = con.prepareStatement(query);
ps.setString(1, categoryModel.getCategoryId());
ps.setString(2, categoryModel.getCategoryName());
ResultSet rs = ps.executeQuery();
和
PreparedStatement ps =null; String query = "SELECT BrandId, BrandName from tbl_brand";
List <BrandModel> brands = new ArrayList<BrandModel>();
BrandModel brandModel = new BrandModel();
BrandModel u = null;
try{
ps = con.prepareStatement(query);
ps.setString(1, brandModel.getBrandId());
ps.setString(2, brandModel.getBrandName());
在这两种情况下,您都为查询设置输入参数,而没有输入参数的任何占位符 (?
)。因此,我假设尝试调用 setString 会引发异常,您可以捕获该异常并将输出返回为 null,然后导致重定向。
关于java - Arraylist 不添加数据,调用时将我重定向到另一个页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39074741/