java - Running the same select query multiple times with different parameters: Mysql -
i have java program needs iterate through hashmap
parameters used query mysql database. code follows:
iterator<entry<string, double>>it = ws.entryset().iterator(); connection con = null; while(it.hasnext()) { entry<string, double>pairs = it.next(); preparedstatement ps = con.preparestatement("select doc_freq lookup word=?"); ps.setstring(1, pairs.getkey()); resultset rs = ps.executequery(); }
the process of repeatedly accessing database every iteration of loop (which 500 times) slowing down application. there way can send these parameters @ once access database once?
considering ws map, can single query way:
connection con = getconnection(); set<string> ks = ws.keyset(); if (ks.size() > 0) { stringbuilder instatement = new stringbuilder("?"); (int = 1; < ks.size(); i++) { instatement.append(", ?"); } preparedstatement ps = con.preparestatement("select doc_freq lookup word in (" + instatement.tostring() + ")"); int k = 1; (string key : keyset) { ps.setstring(k++, key); } resultset rs = ps.executequery(); }
Comments
Post a Comment