Summary of information Query method for Java Web development _jsp Programming

Source: Internet
Author: User

This article describes the Java Web Development Information Query method summary. Share to everyone for your reference. Specifically as follows:

The query methods described here are:

① according to a specific field query;
② query in multiple fields;
③ according to any field query;
④ any field combination query;
⑤ multiple value query.

Query based on a specific field

The user enters the value of the field to query in the input interface, and the system looks for it based on that value.
The following example queries the user's details based on the user name, and the simple effect diagram is as follows:

The key code is as follows:

<p> Please enter the name to query:</p>
<form action= "select.jsp" method= "POST" >
 <p><input type= " Text "name=" username "size=" ><input type= "Submit" value= "submitted" name= "B1" ><input type= "reset" value= "all rewrite "Name=" B2 ></p>
</form>

The code to process the file is as follows:

<%@ page contenttype= "text/html;charset=gb2312"%> <% stringbuffer sql=new stringbuffer
  ();
  Query string string
  username = Request.getparameter ("username");
  The condition of the query
  if (username==null| | Username.length () ==0)
  {
   out.println () Please enter the query criteria! ");
  }
  else
  {
   sql.append ("select * from user where username like");
   Sql.append (username);
   Sql.append ("'");
  }
  Next, based on the SQL generated above query can be
%>

This is primarily concerned with SQL statements, and the query process and the display process refer to other topics.
The above is a precise query that can be used for fuzzy queries, such as querying the user name containing a word of the user's information.

You only need to modify the section of the query statement in your code, and the modified code is as follows:

Sql.append ("SELECT * from user where username like '%");
Sql.append (username);
Sql.append ("%");

Add only the wildcard character "%" of the fuzzy query, if you want to query what to start with, remove the preceding wildcard, if the query ends with what, remove the following wildcard.

querying in multiple fields

Suppose there is a BBS on Java technology, each record has a title, summary, content and other fields, here we ignore the other fields, if the user wants to see the problem of coding, he would like to be in the title, summary or content as long as the information contained in his input should be able to query, This is the time to query in more than one field.

The key code is as follows:

<p> Please enter the keyword to query:</p>
<form action= "select.jsp" method= "POST" >
 <p><input type= " Text "name=" condition "size=" ><input type= "Submit" value= "submitted" name= "B1" ><input "type=" reset "value=" Rewrite all "name=" B2 ></p>
</form>

The processing code for the query is as follows:

<%@ page contenttype= "text/html;charset=gb2312"%> <% stringbuffer sql=new stringbuffer
  ();
  Query string string
  condition =request.getparameter ("condition");
  The condition of the query
  if (condition==null| | Condition.length () ==0)
  {
   out.println () Please enter the query criteria! ");
  }
  else
  {
   sql.append ("select * from user where title like '%");
   Sql.append (condition);
   Sql.append ("%" and summary like '% ");
   Sql.append (condition);
   Sql.append ("%" and detail like '% ");
   Sql.append (condition);
   Sql.append ("%");
  }
  Next, based on the SQL generated above query can be
%>

Query based on any field

The query is more complex, but it is most convenient for users to choose what to query and then enter the values to query.

Working with files:

<%@ page contenttype= "text/html;charset=gb2312"%> <% stringbuffer sql=new stringbuffer
  ();
  Query string string
  condition = Request.getparameter ("condition");
  The query's field
  String v = request.getparameter ("V");
  The value of the query
  if (v.length () ==0)
  {
   out.println () Enter the query criteria! ");
  }
  else
  {
   sql.append ("select * from user where");
   Processing
   if (Condition.equals ("a"))
     sql.append ("a like") according to the field selected by the user;
   else if (condition.equals ("B"))
     sql.append ("B like");
   else
     sql.append ("C like '");
   Sql.append (v);
   Sql.append ("'");
   /* If the condition in the
   drop-down list in the interface is the name of the field in the database, the above code is very simple:
   sql.append ("select * from user where");
   Sql.append (condition);
   Sql.append ("like");
   Sql.append (v);
   Sql.append ("'");
   *
  //Next query based on the SQL generated above
%>

Any combination query

So you can query the fields are all listed, each field corresponds to an input box, the user can be based on a field or a few fields to query, according to what query, in the corresponding input box input information can be.

Processing file for query:

<%@ page contenttype= "text/html;charset=gb2312"%> <% stringbuffer sql=new stringbuffer
  ();
  Query string string
  a = Request.getparameter ("a");
  String B = Request.getparameter ("b");
  String C = request.getparameter ("C");
  String d = request.getparameter ("D");
  Gets the query criteria entered by the user
  sql.append ("select * from user where");
  if (A.length ()!=0)
  {
   sql.append ("A Like '");
   Sql.append (a);
   Sql.append ("' and");
  }
  if (B.length ()!=0)
  {
   sql.append ("B Like '");
   Sql.append (b);
   Sql.append ("' and");
  }
  if (C.length ()!=0)
  {
   sql.append ("C like '");
   Sql.append (c);
   Sql.append ("' and");
  }
  if (D.length ()!=0)
  {
   sql.append ("D like '");
   Sql.append (d);
   Sql.append ("' and");
  }
  Sql.trim (sql.length-3,3);
  Delete the last 4 letters ("and")
  //Then query based on the SQL generated above
%>

Multi-valued Query

Users enter multiple data in a single input box to query for a field that contains multiple keywords, such as a "Java" or "C + +" record in a query subject, or a range of digits, such as a record between 19 and 22, or a time frame, For example, a record was born between January 1, 1981 and December 31, 1983.

This is only described in terms of the number of search, other cases are basically the same implementation.

Working with files:

<%@ page contenttype= "text/html;charset=gb2312"%> <% stringbuffer sql=new stringbuffer
  ();
  Query string string
  condition = Request.getparameter ("condition");
  Gets the query condition
  int index = Condition.indexof ("");
  Finds the space position, with a space as a delimiter
  String min=condition.substring (1,index) of two digits;
  The preceding value
  String max=condition.substring (index+1,condition.length-1);
  The value in the following
  try
  {
   integer.parseint (min);
   Integer.parseint (max);
  } catch (Exception e)
  {
   out.println ("The information entered is illegal!") ");
   return;
  }
  Sql.append ("SELECT * from user where age between");
  Sql.append (min);
  Sql.append ("and");
  Sql.append (max);
  Construct the query string
  //Next query based on the SQL generated above
%>

I hope this article will help you with the JSP program design.

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.