title: Search from database java servlet
body: <p>im trying to create a servlet where i can add , delete and search customers, now i got stuck with search method, im not sure why my method doesnt work but heres the code i´ve created so far. I am using DAO pattern with a following method for filtering customers by name and surname:</p>
  
  <pre><code>public List&lt;Customers&gt; filterCustomer(String name)
  {
      List&lt;Customers&gt; customers = new ArrayList&lt;Customers&gt;();
  
      try{
          st = getConnection().createStatement();     
          rs = st.executeQuery("SELECT * from customer where first_name  like '%" + name + "%'"
                  + "and surname like '%" + name+ "%'");
          while(rs.next())
          {
              Customers customer = new Customers();
              customer.setId(rs.getInt(1));
              customer.setFirst_name(rs.getString(2));
              customer.setSurname(rs.getString(3));
              customer.setCode(rs.getString(4));
  
              customers.add(customer);
  
  
          }
  </code></pre>
  
  <p>In servlet first i create List of customers to print them out, but if the searchString parameter is not null then i want to create new list of these customers which match the searchString. Heres the servlet part i`ve created :</p>
  
  <pre><code>out.print("&lt;form method=\"get\" action =\"" + request.getContextPath() +"/Search\" &gt;"
      + "&lt;input id='searchStringBox' name='searchString'/&gt;"
      + "&lt;input type='submit' value='Filtreeri'&gt;");
  CustomerDao dao = new CustomerDao();
      List&lt;Customers&gt; customers;
      customers = dao.getAllCustomers();
      String name = request.getParameter("searchString"); 
      if(name != null) 
  
      customers = dao.filterCustomer(name); 
  
      out.println("&lt;table id='listTable' class='listTable&gt;'" +
      "&lt;thead&gt;&lt;tr&gt;&lt;th scope='col'&gt;Nimi&lt;/th&gt;&lt;th scope='col'&gt;Perekonnanimi&lt;/th&gt;"
      +"&lt;th scope='col'&gt;Kood&lt;/th&gt;&lt;th scope='col'&gt;&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;");
  
      for(Customers customer : customers)
      {
          String value3 = String.valueOf(customer.getId());
          request.setAttribute("value3",value3);
          customer.getId();
          out.println(        
      "&lt;tbody&gt;&lt;tr&gt; "      
      + "&lt;td&gt;"+ customer.getFirst_name()+" "+"&lt;/td&gt;"
      + "&lt;td&gt;"+ customer.getSurname()+" "+"&lt;/td&gt;"
      + "&lt;td&gt;"+ customer.getCode()+" " +"&lt;/td&gt;"
      + " "+"&lt;td&gt;&lt;a href='"+request.getContextPath()+"/Search?id="+value3+"'&gt;Delete&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;"                    
       );     
  </code></pre>