Use array in mysql query java -


i have 2 questions java mysql

first, have array containing either 1 or 0.

if want if statement how can check if opt[0] = 1? like:

if (options[0] == 1 { sqlbuilder.append("and a.value1 = val[1]") } 

next question. have array containing different values, how can use them in mysql query.

right got

stringbuilder sqlbuilder = new stringbuilder() .append("select * ") .append("from `table1` ") .append("cross join `table2` b ") .append("cross join ` table3` c ") .append("cross join `table4` d") .append("where c.`id` =" . val[0] ); 

so need c.id = val[0]

basically, code correct.

the thing incorrect java syntax. coming php? strings concatenated + in java, last line should

.append("where c.`id` =" + val[0] ); 

in addition, seem mess types; if option array string[] need compare strings or cast value int, first if must this:

if("1".equals(options[0])) 

or

if(integer.parseint(options[0]) == 1) 

note:

as thomas correctly states in comment, should consider using preparedstatments instead of hardcoded statements avoid sql injection attacks.


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -