Query for searching the name alphabetically

前端 未结 5 1262
梦谈多话
梦谈多话 2020-12-12 06:04

I have used the LIKE condition, but it required me to enter the full name into the database to find the name.



        
5条回答
  •  眼角桃花
    2020-12-12 06:41

    I'm don't really understand your question clearly, but from what I understand, I'll post something that may help in solving your problem.

    Put your search string inside % as EmCo commented "'%"+ tfsearch.getText()+"%'"

    The % before and after the search string has different uses when you omit either one of this. See String Comparison Functions on how to use it.

    Now your another problem will be:

    You want the name in alphabetical order. For this you need to include ORDER BY. Then your query string will be.

    "SELECT 
            Name 
       FROM FAMILYcensus 
      WHERE Name LIKE '%" + tfsearch.getText() +"%' 
      ORDER BY Name ASC"
    

    Your search may result in more than one. Using just rs.next(); and String names = rs.getString("NAME"); will get only the first records I guess.

    You can try this code:

    List names = new ArrayList();
    while(rs.next){
        names.add(rs.getString('Name'));
    }
    

    The code I've provided is not tested. Though it will be like that or similar. Have a try if you still have a problem with your current code.

提交回复
热议问题