I create a web-app for accounting IT resources. There is a form on the jsp page

enter image description here

<form action="Searche" method="post"> ФИО: <input type="text" name = "fioSearche"/><br/> Устройство: <select name="devaiceSearche"> <option></option> <option>Ноутбук</option> <option>Монитор</option> <option>Компьютер</option> <option>Процессор</option> <option>Мат.Плата</option> <option>Оперативная память</option> <option>Жесткий диск</option> <option>Мышка</option> <option>Клавиатура</option> <option>Иное</option> </select><br/> <input type="submit" name="Searche" value="searche"/> </form> 

The servlet has this code to determine what we are looking for.

  if(request.getParameter("fioSearch").equals("")) { query = "SELECT fio, device, id, SN, stats, date, period FROM resurces " + "WHERE device = ?"; pst = (PreparedStatement) con.prepareStatement(query); pst.setString(1,request.getParameter("deviceSearch")); } else if(request.getParameter("deviceSearch")==null) { query = "SELECT fio, device, id, SN, stats, date, period FROM resurces " + "WHERE fio = ?"; pst = (PreparedStatement) con.prepareStatement(query); pst.setString(1,request.getParameter("fioSearch")); } else if((request.getParameter("deviceSearch")!=null) && (!request.getParameter("fioSearch").equals(""))) { query = "SELECT fio, device, id, SN, stats, date, period FROM resurces " + "WHERE fio = ? AND device = ?"; pst = (PreparedStatement) con.prepareStatement(query); pst.setString(1,request.getParameter("fioSearch")); pst.setString(2,request.getParameter("deviceSearch")); } 

The first and second points work, the search is carried out either by full name or by device, but they don’t want to work together, as far as I understand, I don’t correctly carry out a logical check, but I can’t understand what exactly is wrong. I have already tried many options, tell me what you need to do to perform a search in both fields?

  • the first time you check fioSearche for an empty string, and in the last if already null. - MrModest
  • By the way, unsolicited advice, before using English words as variables, google their correct spelling .. For example: search (without e ), device (without a ). Plus, it would be cool if you didn't send requests as a string, but worked with entity classes, so you protect yourself from typos .. - MrModest
  • Yes, I know, the trouble is that I do not quite understand how to check for a NOT empty line. I tried through length ()> 0, the result is the same - Timur Zalimov
  • !request.getParameter("fioSearche").equals("") - for example, like this .. (note the exclamation mark at the beginning). - MrModest
  • in general, I would recommend using the StringUtils class from org.apache.commons.lang3 . there are such wonderful methods as isBlank() and isNotBlank() that check for null, an empty string and a string with only spaces - MrModest

1 answer 1

 StringBuilder builder - new StringBuilder("SELECT fio, device, id, SN, stats, date, period FROM resurces "); String fio = null; if(!request.getParameter("fioSearch").equals("")){ fio = "fio = " + request.getParameter("fioSearch"); } String device = null; if(request.getParameter("deviceSearch") != null) devace = "device = " + request.getParameter("deviceSearch"); if( fio == null && device == null) return builder.toString();//тут дальше логика если оба пустые builder.append("WHERE "); if(fio != null){ builder.append(fio); if(device != null) builder.append(" AND "); } if(device != null) builder.append(device); return builder.toString();