简体   繁体   English

Java中将输入数据与Sql表进行比较的有效方法

[英]Efficient way to compare input Data with Sql table in Java

First of all I will explain my use case: 首先,我将解释我的用例:

I will get a String Array of names from user(Can of size 2,5,1) 我将从用户那里得到一个名称的字符串数组(大小为2,5,1)

e.g Suppose user input is like this: 
String[] names={"Micheal", "Joe","Jim"}

Now after taking input from user, I have to hit SQL table called "USERS" and check whether all of these names are present in USERS table or not. 现在,在从用户那里获取输入之后,我必须点击名为“ USERS”的SQL表,并检查所有这些名称是否都出现在USERS表中。 If any single name is not present then return false. 如果不存在任何单个名称,则返回false。 If all names are present in USERS table then return true. 如果所有名称都存在于USERS表中,则返回true。

My Idea: 我的点子:

My idea is to hit USERS table. 我的想法是打USERS表。 Get all names of USERS table in a String array (named as all_names) and then compare my input string(ie names) with this all_names String. 获取String数组中USERS表的所有名称(命名为all_names),然后将我的输入字符串(即名称)与此all_names字符串进行比较。 So if names is subset of all_names then return true else return false. 因此,如果名称是all_names的子集,则返回true,否则返回false。

Problem: 问题:

But I think this is not an efficient solution. 但是我认为这不是一个有效的解决方案。 When this table will expand then I will have thousands of records so this technique will be very exhaustive. 当此表扩展时,我将具有成千上万的记录,因此该技术将非常详尽。 Any other better and efficient solution for this please. 还有其他更好,更有效的解决方案。

Updated Solution: 更新的解决方案:

Suppose names in USERS table are unique. 假设USERS表中的名称是唯一的。

Thanks for your replies. 多谢您的回覆。 Now I have adopted this approach after getting help from your answers. 现在,从您的答案中获得帮助后,我已经采用了这种方法。 I want to know that this solution is a better approach or not: 我想知道此解决方案是否是更好的方法:

            String[] names={"Micheal","Jim","Joe"};


            String list2string =  StringUtils.join(names, ", "); 
            //connection was established previosuly
            stmt = conn.createStatement();
            System.out.println(list2string);
            rs = stmt.executeQuery("SELECT COUNT(*) AS rowcount FROM USERS WHERE name IN (" + 
            list2string + 
            ")");

            rs.next();
            int count = rs.getInt("rowcount");
            rs.close();
            if(names.length==count){
                System.out.println("All names are in users table");
            }else{
                System.out.println("All names are not present in users table");
            }

Want your comments on this updated solution please. 希望您对此更新的解决方案发表评论。

Regards 问候

You are right, this is not really efficient. 没错,这不是很有效。

It is the database job to do such things. 做这些事情是数据库的工作。

You can either make a select statement for each name, eg. 您可以为每个名称做出选择语句,例如。

SELECT name FROM users WHERE name = 'Micheal'

or 要么

SELECT name FROM users WHERE name IN ('Micheal', 'Joe', 'Jim')

and check the returned rows. 并检查返回的行。

It might be quiet different depending on which framework you use to query the database. 取决于您用来查询数据库的框架,它可能会有所不同。

you can form a string out of string array using loop 您可以使用循环从字符串数组中形成字符串
for example if you have string array like this: 例如,如果您有这样的字符串数组:
String[] names={"Micheal", "Joe","Jim"} get a string lets say s -> "Micheal", "Joe","Jim" String [] names = {“ Micheal”,“ Joe”,“ Jim”}得到一个字符串,说s->“ Micheal”,“ Joe”,“ Jim”
now query like this: 现在这样查询:
String sql = SELECT name FROM users WHERE name IN (" + s + ")". (you can check the format). 字符串sql = SELECT用户名,用户名IN(“ + s +”)“。(您可以检查格式)。
get the output collection and compare with the given collection. 获取输出集合并与给定集合进行比较。

One way to do it, could be 一种方法是

SELECT
    COUNT(DISTINCT name)
FROM
    users
WHERE
    name IN ('Micheal', 'Joe', 'Jim')

Then check if the count is equal to your parameter count, in our case, we should get 3. 然后检查计数是否等于您的参数计数,在本例中,我们应该得到3。

I will get a String Array of names from user(Can of size 2,5,1) 我将从用户那里得到一个名称的字符串数组(大小为2,5,1)

You get the input from user, you hit the database with query: 您从用户那里获得输入,并通过查询访问数据库:

SELECT (WHATEVER_YOU_NEED) FROM SCHEMA_NAME.TABLE_NAME WHERE COLUMN IN (USER_PROVIDED_INPUT); 从SCHEMA_NAME中选择(WHATEVER_YOU_NEED)。TABLE_NAME列在(USER_PROVIDED_INPUT)中;

You store this result in List. 您将此结果存储在列表中。

Get all names of USERS table in a String array (named as all_names) and then compare my input string(ie names) with this all_names String. 获取String数组中USERS表的所有名称(命名为all_names),然后将我的输入字符串(即名称)与此all_names字符串进行比较。 So if names is subset of all_names then return true else return false. 因此,如果名称是all_names的子集,则返回true,否则返回false。

Yes, you are right, so you will use 是的,您是对的,因此您将使用

Use Collection.containsAll() : 使用Collection.containsAll()

boolean isSubset = listA.containsAll(listB);

And, if your database has unique names (which I guess can be duplicate), you can simply get the count from SQL Query and match it with the user input. 而且,如果您的数据库具有唯一的名称(我想可以重复),则只需从SQL Query中获取计数并将其与用户输入进行匹配即可。

I hope this will help. 我希望这将有所帮助。

SELECT IF(
    ( SELECT COUNT(DISTINCT name) FROM users WHERE name IN ({toSearch}) ) = {Count},
    , 1 , 0
) as Result

replace {toSearch} with eg 'Micheal', 'Joe', 'Jim' {count} is the number of searche, in this example 3. so if all exist the column "Result" has the value 1 else 0 将{toSearch}替换为“ Micheal”,“ Joe”,“ Jim”。{count}是searche的数量,在此示例中为3。因此,如果全部存在,则“ Result”列的值为1,否则为0

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

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