通过选中复选框显示项目

时间:2014-05-13 08:33:40

标签: java mysql jsp servlets

您好我正在尝试按照BRAND,FLAVOR过滤产品并在页面上显示它们。因为我使用了复选框来选择不同的品牌和口味。因为我使用了一个函数,我已经写了一个SOL查询。如果我同时选择BRANDS和FLAVOR选项,但是当我仅按品牌或风味过滤它时,此功能正常工作它给了我NULL指针EXCEPTION。 代码如下:

 public List<Products> DisplayProducts2(String[] a, String b[]) {

    ResultSet rs;
    List<Products> data = null;
    PreparedStatement stmt;
    try {
        StringBuilder param = new StringBuilder();
        for (String str : a) {
            param.append("'").append(str).append("', ");
        }
        StringBuilder param1 = new StringBuilder();
        for (String str : b) {
            param1.append("'").append(str).append("', ");
        }

        String query = "select  * from products where Brand in (" + param.substring(0, param.length() - 2) + ") or Flavour in (" + param1.substring(0, param1.length() - 2) + ")";
        stmt = DataBaseConnection.DBConn.getConnection().prepareStatement(query);
        rs = stmt.executeQuery();
        if (rs != null) {
            data = new ArrayList<Products>();
            while (rs.next()) {
                Products p = new Products();
                p.setTitle(rs.getString("Ttile"));
                p.setCategory(rs.getString("Category"));
                p.setSubCategory(rs.getString("SubCategory"));
                p.setSubCategoryTwo(rs.getString("SubCategorytwo"));
                p.setPrice(rs.getInt("Price"));
                p.setFlavour(rs.getString("Flavour"));
                p.setSize(rs.getString("Size"));
                p.setImage(rs.getString("image"));
                p.setBrand(rs.getString("Brand"));
                p.setInstock(rs.getString("instock"));
                p.setInstockQty(rs.getInt("instockqty"));
                data.add(p);
            }
        }
    } catch (Exception e) {

        System.out.println(e.getStackTrace());
        System.out.println("----------------------");
        System.out.println(e.getMessage());
        System.out.println("----------------------");
        System.out.println(e.getSuppressed());
        System.out.println("----------------------");
        e.printStackTrace();
        return null;
    }
    return data;

}

我试图使用if条件来检查哪个值为空目的会有这么多的比较..请告诉我该怎么做

1 个答案:

答案 0 :(得分:0)

更改

StringBuilder param = new StringBuilder();
        if (a != null) {
            for (String str : a) {
                param.append("'").append(str).append("', ");
            }
        }
        StringBuilder param1 = new StringBuilder();
        if (b != null) {
            for (String str : b) {
                param1.append("'").append(str).append("', ");
            }
        }

        String prm = param.toString().length() > 2 ? param.toString()
                .substring(0, param.toString().length() - 2) : null;
        String prm1 = param1.toString().length() > 2 ? param1.toString()
                .substring(0, param1.toString().length() - 2) : null;

        String query = "select  * from products where ";

        if (prm != null && prm1 != null) {
            query += "Brand in (" + prm + ") or Flavour in (" + prm1 + ")";
        } else if (prm != null && prm1 == null) {
            query += "Brand in (" + prm + ")";
        } else if (prm1 != null && prm == null) {
            query += "Flavour in (" + prm1 + ")";
        }