This article describes the connection database and fuzzy query functions implemented by Java. Share it for your reference, as follows:
Fuzzy query is a common query method, for example, in the order table, it contains the specific date of the order. If you want to query the order information for a certain year and month, the best way is to use fuzzy query. To perform fuzzy queries, you need to use the keyword LIKE. When using the LIKE keyword for fuzzy query, the wildcard "%" can be used instead of 0 or more characters, and the underscore_ is used to represent one character.
Note: It should be noted that when using LIKE, the subsequent query conditions need to be included in single quotes in English, otherwise the error will be reported as follows
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '%not%' at line 1
package com.ningmeng;import java.sql.*;public class Test07 { public static void main(String[] args) { // TODO Auto-generated method stub try { Class.forName("com.mysql.jdbc.Driver");//Load the database driver System.out.println("Load the database driver successfully"); String url="jdbc:mysql://localhost:3306/test";//Declare the url of your own database test String user="root";//Own database username String pass="123456";//Own database password//Create a database connection and obtain the connected object conn Connection conn=DriverManager.getConnection(url,user,pass); System.out.println("Connect the database driver successfully"); Statement stmt=conn.createStatement();//Create a Statement object String sql="select * from users where username like '%not%' ";//Generate a sql statement ResultSet rs=stmt.executeQuery(sql);//Execute the sql statement int id,age,sex; String username,password; System.out.println("id/t Username/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); sex=rs.getInt("age"); System.out.println(id+"/t"+username+"/t"+password+"/t" +sex+"/t"+age);//Output query result} System.out.println("fuzzy query succeeded"); conn.close();//Close the database connection System.out.println("Close the database connection successfully"); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } }}Running results:
For more information about Java-related content, please check out the topics of this site: "Java+MySQL database programming summary", "Java data structure and algorithm tutorial", "Java file and directory operation skills summary", "Java operation DOM node skills summary" and "Java cache operation skills summary"
I hope this article will be helpful to everyone's Java programming.