java - The correct way to update/query from DB -
i abit new working datatbases java, , have been wondering if working in correct way. in code db interface done within 1 class called dataaccess, example of code:
please note opened connection (connblng
) before entering function isvalidoperator()
.
is correct way work or should open , close connection everytime need access db?
if(da.startblngconnection() == null) return "error" dataaccess da = new dataaccess(); da.isvalidoperator("123") //this code dataaccess class public boolean isvalidoperator(string dn) { system.out.println( npgreqid + " - " + log_tag + "inside isvalidoperator : " + dn); preparedstatement prepstmt = null; resultset queryresult = null; try{ prepstmt = connblng.preparestatement("select network_id, network_identifier operators network_identifier = ?"); prepstmt.setstring(1, dn); queryresult = prepstmt.executequery(); if (queryresult.next()) { return true; } }catch(exception e){ system.out.println(npgreqid + " - " + e); dblog("", new date(),"" , npgreqid , "" ,"" , makesurenotoutofrange(getstacktrace(e),4000), "" , ""); return false; } finally{ closestatmentandrs(queryresult, preparedstatement); } return false; }
jdbc plain not easy use api. maybe can take @ dalesbread https://github.com/blackrush/dalesbred lightwight jdbc wrapper. here discussion jdbc wrappers in common: simple jdbc wrapper.
i not suggest close db connection time, should use pooling that. creating db connection expensive.
Comments
Post a Comment