简体   繁体   English

JAVA MySql 多词搜索

[英]JAVA MySql multiple word search

i have a database in MySql that has a name column in it which contains several words(description).我在 MySql 中有一个数据库,其中有一个名称列,其中包含几个单词(描述)。 I am connected to database with java through eclipse.我通过 eclipse 用 java 连接到数据库。 I have a search, that returns results if only name field contains one word.我有一个搜索,如果只有 name 字段包含一个单词,则返回结果。 id:..................name:..............info:......................type:>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 1...................balloon..........big red balloon................big>>>>>>>>>>>>>>>>>>> 2....................house..........expensive beautiful............luxury>>>>>>>>>>>>>>>>>>>>>>>>> 3................chicken wings.......deep fried wings...............tasty id:..................名称:..信息:.. ..........类型:>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >> 1...................气球.......大红气球....... ..大>>>>>>>>>>>>>>>>>>> 2..........房子...... ....昂贵的美丽........豪华>>>>>>>>>>>>>>>>>>>>>>>>> 3..... ......鸡翅.......炸鸡翅.......美味

these are just random words but as an example my search can only see ex.这些只是随机词,但作为一个例子,我的搜索只能看到 ex。 balloon and then show info, but if i type chicken wings, it does nothing.气球,然后显示信息,但如果我输入鸡翅,它什么也不做。 so it possible somehow to search from columns with multiple words?所以有可能以某种方式从包含多个单词的列中进行搜索吗? this is my search code below这是我下面的搜索代码

import java.io.*;
import java.sql.*;
import java.util.*;

class Search {

public static void main(String[] args) {
    Scanner inp``ut = new Scanner(System.in);

    try {
        Connection con = DriverManager.getConnection(
                "jdbc:mysql://example/mydb", "user", "password");
        Statement stmt = (Statement) con.createStatement();

        System.out.print("enter search: ");
        String name = input.next();

            String SQL = "SELECT * FROM menu where name LIKE '" + name + "'";

        ResultSet rs = stmt.executeQuery(SQL);

        while (rs.next()) {
            System.out.println("Name: " +rs.getString("name"));
            System.out.println("Description:  " + rs.getString("info") );
            System.out.println("Price: " + rs.getString("Price"));

        }

    } catch (Exception e) {
        System.out.println("ERROR: " + e.getMessage());
    }
}
} 

The way you are performing the search now, you are at first vulnerable to sql injection and might want to use the PreparedStatement of JDBC instead of the normal statement.按照您现在执行搜索的方式,您一开始很容易受到sql 注入的攻击,并且可能希望使用 JDBC 的 PreparedStatement 而不是普通语句。

Second, I would recommend splitting the user input at the space and searching for all the terms alone with "OR".其次,我建议在空格处拆分用户输入并使用“OR”单独搜索所有术语。

Example: User inputs house expensive beautiful示例:用户输入house expensive beautiful

Search would be:搜索将是:

SELECT * FROM menu where name LIKE '%house%' OR name LIKE '%expensive%' OR name LIKE '%beaufitful%'

I have put percentage signs because if you would search for 'house', only the exact term "house" will be matched.我放置了百分比符号,因为如果您要搜索“房屋”,则只会匹配确切的术语“房屋”。 By searching with "%house%", the strings "somethingbeforehouse" and "houseandsomethingafter" will be also found.通过使用“%house%”进行搜索,还可以找到字符串“somethingbeforehouse”和“houseandsomethingafter”。

Hope this helps you!希望这对你有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM