SQL query statement query condition field assembly

Source: Internet
Author: User

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;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.