• 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:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

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
Bartender
Posts: 4096
21
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This type versus ...?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!