• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

connection object

 
deepak bagdiya
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All,
Following are the lines from the code for getting a connection in a class:
public static Class MyDbConnect {
public static Connection dbConnect(){
InitialContext ctx = null;
Connection conn = null;
Hashtable parms = new Hashtable();
try {
parms.put(Context.INITIAL_CONTEXT_FACTORY, ---
----
javax.sql.DataSource ds = null;
ctx = new InitialContext(parms);
ds = (javax.sql.DataSource) ctx.lookup(DATASOURCE);
conn = ds.getConnection(LoginId,Passwd);
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}


}
In other programs we are using the class and this method to get the connection .Something similar to following lines of code:

Class GetNames extends MyDbConnect
public Collection getResults(String Id, String name,String fname, String fid,, Connection con) throws Exception {
CallableStatement cs = null;
ResultSet rs = null;
ArrayList names = new ArrayList();
boolean local = true;
try {
if(con == null || con.isClosed())
con = getConnection();
else
local = false;
if(Id.equalsIgnoreCase("all"))
Id = null;
cs = con.prepareCall("{call SearchNames(?,?,?,?)}");
cs.setString(1, Id);
---
---
rs = cs.executeQuery();
while(rs != null && rs.next()) {
----
} catch(Exception e) {
e.printStackTrace();
} finally {
try {
if (rs != null) rs.close();
if (cs != null) cs.close();
if (local && con != null) con.close();
} catch(Exception e) {
}
}
return names;
}

Could you please advise if it is a good practice to use this type of code to get the Connection object or not .
 
Scott Selikoff
author
Saloon Keeper
Posts: 4014
18
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This type versus ...?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic