sql - How do I update just retrieved records without issuing an additional query? -
i have following piece of code:
string sql = "select * users email = " + email; prestat = dbconnection.preparestatement(sql); rs = prestat.executequery(); boolean isempty = !rs.first(); if (isempty) { // special marker nonexistent user return "$null"; } else if (password.equals(rs.getstring(2))) { string uuid = uuid.randomuuid().tostring(); // here want insert uuid database } considering have searched database, i'm wondering if there way can row number/position,and use update uuid column, hence preventing db search.
it's not clear trying here, , doesn't seem understand goes on. example, when using prepared statement, 1 doesn't feed 2 concatenated strings, instead does:
preparedstatement stmt = conn.preparestatement("select * foo bar = ?"); stmt.setstring(1, "hello world!"); then, if want avoid double search, can assume optimistic point of view:
update users set uuid = ? email = ? , password = ? ensure email unique, way. also, maybe it, don't save plaintext passwords in db. use cryptographic hashes instead, , salt them.
since specify backend apache derby, maybe this guide can solve problem (it's 100% java not portable because of different backends may not implement of required features). basically, pass 2 flags when preparing statement:
statement stmt = con.createstatement( resultset.type_forward_only, resultset.concur_updatable); resultset res = stmt.executequery("select * users email = ?"); and have resultset backed updateable cursor, can call:
res.updatestring("uuid", uuid); res.updaterow(); i think may avoid additional search, didn't test , cannot if there's real performance gain. sounds premature optimization, though.
Comments
Post a Comment