• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

connection object  RSS feed

 
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 .
 
author
Posts: 4096
21
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
Boost this thread!