Fuzzy query for the value of all fields in the database

Fuzzy query of all values ​​in the database, for example: input Zhang to display all information containing Zhang in the database  

   Because it is still a fuzzy query, the solution I think of is to query all fields. At this time, I encountered a problem.
General fuzzy query is to use select * from table name where column name like '%%'; but it is impossible to manually enter all columns to query all columns. After checking, there is a concat(str1,str2,...,strn) function in sql. The parameter of this function is the column name, so that we can query all columns, but we still need to enter all column names. So is there a way to get all the column names, store them, and put them into the concat() function.

    The answer is definitely yes. I thought of a way to connect to the database first and put all the column names obtained into the list. Then the content in the list is [column name 1, column name 2, .....]. Obviously what we want is the content in the list, we don't need this []. Use list.toString().replaceAll("\\[|\\]","") to remove the [] outside the list, and you can use concat() to query all columns in the sql statement.

   It should also be noted that the concat() function has a disadvantage. If there is NULL in the parameter (column name), this data will return a null value, so if you want to fuzzy query all fields, you must first ensure that the database does not exist. NULL value, otherwise it is easy to miss data without fuzzy query. You can judge whether there is a NULL value in this column while obtaining the column name. If it exists, replace it with an empty string of '', so that data will not be leaked.
    Get all fields (column names) and strip NULL values ​​code:

    Class.forName("com.mysql.jdbc.Driver").newInstance();
   conn=DriverManager.getConnection("jdbc:mysql://172.17.143.230/info?useUnicode=true&characterEncoding=UTF-8&useSSL=false","root","123456");
   String sql="select * from people";
   PreparedStatement stmt;
   stmt = (PreparedStatement) conn.prepareStatement(sql);
   ResultSet rs=stmt.executeQuery(sql);
   ResultSetMetaData data=(ResultSetMetaData) rs.getMetaData();
   if(rs.next()){
   for(int i = 1 ; i<= data.getColumnCount();i++){
     String columnName = data.getColumnName(i);
     General.list.add(columnName);
     String sql1="update people set "+columnName+"='' where "+columnName+" is null;";
     stmt.executeUpdate(sql1);
    }
   }
    Fuzzy query for all field codes:

    String sql2="select * from people where concat("+General.list.toString().replaceAll("\\[|\\]","")+") like '%"+ZD+"%';";
   rs=stmt.executeQuery(sql2);
   while(rs.next())
   {
    HashMap<String, Object> map1 = new HashMap<String, Object>();
    for(int j=1;j<data.getColumnCount();j++)
    {
     map1.put(data.getColumnLabel(j), rs.getObject(j));
    }
    list.add(map1);
   }


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324696350&siteId=291194637