I am abit new with working with DatatBases from Java, and I have been wondering if I am working in a correct way. In my code all the DB interface is done within one class called DataAccess, an example of my code:
Please note that I opened a connection (connBlng
) before entering the function isValidOperator()
.
Is this the correct way to work or should I open and close a connection everytime I need to access the DB?
if(da.StartBlngConnection() == null)
return "ERROR"
DataAccess da = new DataAccess();
da.isValidOperator("123")
//this is code from 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 from operators where 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;
}