When we query information in the database, we often need to query the information based on the conditions entered by the user. Some conditions are available, but some conditions are unavailable. Therefore, we need to assemble the condition statements. The following method is compiled based on this situation, hoping to help the reader:
String SQL = "select * from table where"; // the next space after where stringbuffer STB = new stringbuffer (SQL); If (! (Name = NULL | "". equals (name) {Stb. append ("name = '" + name + "' and"); // and the last 3 spaces} If (! (Age = NULL | "". equals (AGE) {Stb. append ("age = '" + age + "' and"); // and the last 3 spaces} If (! Textutils. equals (STB. tostring (), "") {SQL = Stb. substring (0, Stb. length ()-6);} // the length of one space after where and three spaces after and is both 6 characters, this is to facilitate the final removal of excess part length is 6 return SQL;