mysql - Efficient way to compare input Data with Sql table in Java -


first of explain use case:

i string array of names user(can of size 2,5,1)

e.g suppose user input this:  string[] names={"micheal", "joe","jim"} 

now after taking input user, have hit sql table called "users" , check whether of these names present in users table or not. if single name not present return false. if names present in users table return true.

my idea:

my idea hit users table. names of users table in string array (named all_names) , compare input string(i.e names) all_names string. if names subset of all_names return true else return false.

problem:

but think not efficient solution. when table expand have thousands of records technique exhaustive. other better , efficient solution please.

updated solution:

suppose names in users table unique.

thanks replies. have adopted approach after getting answers. want know solution better approach or not:

            string[] names={"micheal","jim","joe"};               string list2string =  stringutils.join(names, ", ");              //connection established previosuly             stmt = conn.createstatement();             system.out.println(list2string);             rs = stmt.executequery("select count(*) rowcount users name in (" +              list2string +              ")");              rs.next();             int count = rs.getint("rowcount");             rs.close();             if(names.length==count){                 system.out.println("all names in users table");             }else{                 system.out.println("all names not present in users table");             } 

want comments on updated solution please.

regards

you right, not efficient.

it database job such things.

you can either make select statement each name, eg.

select name users name = 'micheal' 

or

select name users name in ('micheal', 'joe', 'jim') 

and check returned rows.

it might quiet different depending on framework use query database.


Comments

Popular posts from this blog

matlab - "Contour not rendered for non-finite ZData" -

delphi - Indy UDP Read Contents of Adata -

javascript - Any ideas when Firefox is likely to implement lengthAdjust and textLength? -