java - How sql works and why this result is returned? -
hi i'm using preparedstatement in java execute query in db.
the table:
when comes update, delete , insert it's fine, when comes select( ex. i've done "select ?,?,?,?,? person" , set strings afterwards) , following result returned:
i'm assuming because it's strings replacing ? did not come out expected:(please correct me if it's wrong)
expected sql: "select no,name,tel,birthday,address person"
actual sql: "select \"no\",\"name\",\"birthday\",\"address\" person"
i've tested second 1 in in navicat:
i'd understand why executing query statement return result this?
if here's java code:
// data assist object public class dao { static string jdbcurl; static string username; static string password; static{ try { class.forname("com.mysql.jdbc.driver"); resourcebundle rb = resourcebundle.getbundle("db"); jdbcurl = rb.getstring("jdbcurl"); username = rb.getstring("username"); password = rb.getstring("password"); } catch (classnotfoundexception ex) { ex.printstacktrace(); } } // insert, delete , update public int modify(string sql, string[] args){ int x=0; try(connection con = drivermanager.getconnection( jdbcurl,username ,password); preparedstatement ps = con.preparestatement(sql);){ (int = 0; < args.length; i++) { ps.setstring(i+1, args[i]); } x =ps.executeupdate(); system.out.println(x); }catch(sqlexception e){ e.printstacktrace(); } return x; } // select public list<map<string,string>> query(string sql, string[] params){ list<map<string,string>> reslist = new arraylist<>(); try(connection con = drivermanager.getconnection( jdbcurl,username ,password); preparedstatement ps = con.preparestatement(sql);){ (int = 0; < params.length; i++) { ps.setstring(i+1, params[i]); } try(resultset res =ps.executequery();){ resultsetmetadata mdata = res.getmetadata(); int num = mdata.getcolumncount(); while(res.next()){ hashmap<string,string> data = new hashmap<>(); (int = 1; <= num; i++) { string result = res.getstring(i); string columnname = mdata.getcolumnname(i); data.put(columnname,result); } reslist.add(data); } } }catch(exception e){ e.printstacktrace(); } return reslist; } public static void main(string[] args) throws sqlexception { dao dao = new dao(); string sql = "insert person(name,tel,birthday,address) values(?,?,?,?)"; sql = "select ?,?,?,?,? person"; list<map<string,string>> res = dao.query(sql, new string[]{"no","name","tel","birthday","address"}); for(map m:res){ system.out.print("no: "+m.get("no")+","); system.out.print("name: "+m.get("name")+","); system.out.print("tel: "+m.get("tel")+","); system.out.print("birthday: "+m.get("birthday")+","); system.out.println("address: "+m.get("address")); } } } thanks help.
sql works on show me these columns criteria true basis.
in statement:
"select \"no\",\"name\",\"birthday\",\"address\" person" you're getting
select "no", "name", "birthday", "address" person when hits database. "" operator creates string in sql. in plain english, means you're telling database return specified set of strings each row in person criteria listed met.
since didn't list where clause, rows true default 1 row of strings every single row in person table. first query same thing, instead of directly passing strings, you're adding them in bind variables.
if want see values in table, write query without "'s
select no, name, birthday, address person unless otherwise specified, bind functions pass value string. why query behaved way did. don't recommend using bind variables in select clause. that's strange practice.
edit:
as adrian pointed out in comments, " denotes columns in sql. apologies not catching that. assume meant use ' operator denotes strings.
if not, else going on here entirely.



Comments
Post a Comment