0

我想比较从表单中获取的字符串与带有表值的值......但是有空指针异常
2)我如何让结果集不访问表的最后一条记录

        String occid = request.getParameter("occid");
        String date = request.getParameter("Date");
        String firstname = request.getParameter("FirstName");
        String lastname = request.getParameter("LastName");
        String village = request.getParameter("Village");
        String sonof = request.getParameter("Son");
        String district = request.getParameter("District");
        String weight = request.getParameter("Weight");
        String bags = request.getParameter("Bags");
        String rate = request.getParameter("Rate");
        String amount = request.getParameter("Amount");
        String advanceamt = request.getParameter("Advance");

        String sql = null;
        PreparedStatement prest = null;
        List<AcceptBean> list = new ArrayList<AcceptBean>();

        sql = "Select * from ColdStorage.OccupantMaster";
        prest = conn.prepareStatement(sql);
        ResultSet rs = prest.executeQuery();
        String first = "";
        String last = "";
        String vill = "";
        String son = "";
        String dist = "";
        while (rs.next()) {
            AcceptBean bean = new AcceptBean();
            first = rs.getString("FirstName");
            bean.setFirstName(first);
            System.out.println("first = " + first);
            last = rs.getString("LastName");
            bean.setLastName(last);
            System.out.println("last = " + last);
            vill = rs.getString("Village");
            bean.setVillage(vill);
            System.out.println("vill = " + vill);
            son = rs.getString("Sonof");
            bean.setSonOf(son);
            System.out.println("son = " + son);
            dist = rs.getString("District");
            bean.setDistrict(dist);
            System.out.println("dist = " + dist);
            list.add(bean);
        }
        for (AcceptBean s : list) {
            if ((first.equals(firstname) || first.equals("null")) && (last.equals(lastname) || last.equals("null")) &&
                    (vill.equals(village) || vill.equals("null")) && (son.equals(sonof) || son.equals("null")) &&
                    (dist.equals(district) || dist.equals("null"))) 

            {
                System.out.println("do nothing");
                ServletContext sc = getServletContext();
                RequestDispatcher rd = sc.getRequestDispatcher("/Index.jsp");
                System.out.println("it exists in database");
                rd.forward(request, response);

            } 

数据库值是 -

OccId 名 姓 村 SonOf 区

31 Sourodeep bag bengal aurobindo bad bengal

32 ARVINDBHAI 帕尔马萨乔德

第 67 章

72 urvashi parmar 孟买 arvindbhai parmar 孟买

90 迪扬 NULL NULL NULL NULL

91 迪扬 NULL NULL NULL NULL

4

3 回答 3

1
  1. 你必须检查空

    如果(第一!=空)

2.如果是空搅拌,null则不等于NULL,因为你使用了equals,使用equalsIgnoreCase代替。

于 2013-03-14T17:55:39.077 回答
1

文档ResultSet#getString说:

列值;如果值为 SQL NULL,则返回值为 null

例如,对于那些行,而last不是. 因此,您正在调用。nullString "null"null.equals

测试== null,而不是equals("null")

        if( first == null || first.equals(firstname) &&
            last == null || last.equals(lastname) &&
            vill == null || vill.equals(village) &&
            son == null || son.equals(sonof) &&
            dist == null || dist.equals(district) )
于 2013-03-14T17:55:49.883 回答
0

检查你的

如果(第一!=空)

然后将值与其他值进行比较

于 2013-03-14T18:01:20.443 回答