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