Fuzzy query for Java Connection database

Source: Internet
Author: User

1: Fuzzy query is a more common way of querying, such as in the Order table, contains the specific date of the order. If you want to query the order information for a certain month of the year, the best way is to use a fuzzy query. You need to use the keyword like for fuzzy queries. When using the LIKE keyword for fuzzy queries, you can use the wildcard character "%" instead of 0 or more characters, using an underscore _ to represent a character.

Note: It should be noted that when using like, the following query conditions need to add ', the English state of single quotation marks, or the error is as follows

You have a error in your SQL syntax; Check the manual-corresponds to your MySQL server version for the right syntax-use near '% ' on line 1

1  PackageCom.ningmeng;2 3 ImportJava.sql.*;4 5  Public classTest07 {6 7      Public Static voidMain (string[] args) {8         //TODO auto-generated Method Stub9         Try {TenClass.forName ("Com.mysql.jdbc.Driver");//Load Database driver OneSYSTEM.OUT.PRINTLN ("Load Database driver succeeded"); AString url= "Jdbc:mysql://localhost:3306/test";//URL that declares its own database test -String user= "root";//your own database user name -String pass= "123456";//your own database password the             //establish a database connection to get connected objects Conn -Connection conn=drivermanager.getconnection (url,user,pass); -SYSTEM.OUT.PRINTLN ("Connection database driver succeeded"); -Statement stmt=conn.createstatement ();//Create a statement object +String sql= "SELECT * from the users where username like '% of% '";//Generating SQL statements -ResultSet rs=stmt.executequery (SQL);//Execute SQL statement +             intId,age,sex; A String Username,password; atSystem.out.println ("id\t user name \ t password \ t gender \ t Age"); -              while(Rs.next ()) { -Id=rs.getint ("id"); -Username=rs.getstring (2); -Password=rs.getstring ("Password"); -Age=rs.getint (4); inSex=rs.getint ("Age"); -System.out.println (id+ "\ t" +username+ "\ T" +password+ "\ T" to+sex+ "\ t" +age);//Output Query Results +             } -SYSTEM.OUT.PRINTLN ("Fuzzy query succeeded"); theConn.close ();//To close a database connection *SYSTEM.OUT.PRINTLN ("Close database connection succeeded"); $}Catch(ClassNotFoundException e) {Panax Notoginseng             //TODO auto-generated Catch block - e.printstacktrace (); the}Catch(SQLException e) { +             //TODO auto-generated Catch block A e.printstacktrace (); the         } +          -     } $}

Fuzzy query for Java Connection database

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.