| | |
please help in Jdbc connectivity
![]() |
•
•
Join Date: Nov 2008
Posts: 2
Reputation:
Solved Threads: 0
Hi
I've two files for JDBC connectivity.
1)DBUtil.java - to establish DB connection using JDBC and close DB objects
2) GetEmpl.java - Uses DBUtil to connect to DB and retreives data.
[code language=java]
public class DBUtil {
private static String dataSourceName = getJNDI();
private Connection conn = null;
private PreparedStatement pstmt = null;
private static String getJNDI(){
ResourceBundle rbQuery = ResourceBundle.getBundle("MDA_Common");
String dataSourceName = rbQuery.getString("JDBC_JNDI");
return dataSourceName;
}
private Connection getConnection(){
try{
Context ctx = new InitialContext();
DataSource ds = (DataSource)ctx.lookup(dataSourceName);
conn = ds.getConnection();
}catch(Exception ex)
{
LogUtil.logger("MDALibrary","DBUtil.class","ERROR","Exception occurred during establishing connection with database."+ex.getMessage());
}
return conn;
}
public PreparedStatement getPreparedStatement(String Query)
{
try{
conn = getConnection();
pstmt = conn.prepareStatement(Query);
}catch(Exception ex)
{
LogUtil.logger("MDALibrary","DBUtil.class","ERROR","Exception occurred during calling procedure"+ex.getMessage());
ex.printStackTrace();
closePreparedStatement();
}
return pstmt;
}
public void closePreparedStatement()
{
try{
this.pstmt.close();
pstmt = null;
this.conn.close();
conn = null;
}catch(Exception e)
{
LogUtil.logger("MDALibrary","DBUtil.class","ERROR","Exception occurred during calling procedure"+e.getMessage());
e.printStackTrace();
}
}
}
public class GetPrevEmplTypeImpl {
public DataObject getTransactionDetails(DataObject transactionInput) {
LogUtil.logBO("MDADB","MDADB --> GetPrevEmplTypeImpl","INFO"," Input of GetPrevEmplTypeImpl is : --> transactionInput: ",transactionInput);
DataObject TransOutput = BOUtil.createDataObject("Transaction");
String TaleoReq = transactionInput.getString("Transaction_Type");
DBUtil db = new DBUtil();
PreparedStatement pstmt = null;
try {
String sQuery = "";
ResourceBundle rbQuery = ResourceBundle.getBundle("MDA_Queries");
sQuery = rbQuery.getString("GetPrevEmplType");
LogUtil.logger("MDADB","MDADB --> GetPrevEmplTypeImpl","INFO","Query::"+sQuery);
pstmt = db.getPreparedStatement(sQuery);
pstmt.setString(1, TaleoReq);
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
TransOutput.setString("Status", rs.getString("PREV_EMPL_TYPE"));
}
} catch (Exception sqle) {
LogUtil.logger("MDADB","MDADB --> GetPrevEmplTypeImpl","ERROR","Exception occurred in GetPrevEmplTypeImpl ");
LogUtil.logger("MDADB","MDADB --> GetPrevEmplTypeImpl","ERROR","OutPut from getMessage() "+sqle.getMessage());
LogUtil.logger("MDADB","MDADB --> GetPrevEmplTypeImpl","ERROR","OutPut from getCause() "+sqle.getCause());
}
finally{
db.closePreparedStatement();
try{
pstmt.close();
pstmt=null;
LogUtil.logger("MDADB","MDADB --> GetPrevEmplTypeImpl","INFO","dbConnection closed in GetPrevEmplTypeImpl");
}catch(Exception e){
LogUtil.logger("MDADB","MDADB --> GetPrevEmplTypeImpl","ERROR","Exception occurred while closing the connection for pstmt : "+pstmt);
LogUtil.logger("MDADB","MDADB --> GetPrevEmplTypeImpl","ERROR","OutPut from getMessage() "+e.getMessage());
LogUtil.logger("MDADB","MDADB --> GetPrevEmplTypeImpl","ERROR","OutPut from getCause() "+e.getCause());
}
}
return TransOutput;
}
}
[/code]
In DBUtil.java, does closePreparedStatement() closes the preparedStatement and connection used for the GetPrevEmplTypeImpl transaction? In DBUtil.java, conn and pstmt are declared as private global variables and during closing these DBObjects in GetPrevEmplTypeImpl.java, no reference to the object used is passed to db.closePreparedStatement().
Please share the best way of handling these connections.
Thanks
Usha.
I've two files for JDBC connectivity.
1)DBUtil.java - to establish DB connection using JDBC and close DB objects
2) GetEmpl.java - Uses DBUtil to connect to DB and retreives data.
[code language=java]
public class DBUtil {
private static String dataSourceName = getJNDI();
private Connection conn = null;
private PreparedStatement pstmt = null;
private static String getJNDI(){
ResourceBundle rbQuery = ResourceBundle.getBundle("MDA_Common");
String dataSourceName = rbQuery.getString("JDBC_JNDI");
return dataSourceName;
}
private Connection getConnection(){
try{
Context ctx = new InitialContext();
DataSource ds = (DataSource)ctx.lookup(dataSourceName);
conn = ds.getConnection();
}catch(Exception ex)
{
LogUtil.logger("MDALibrary","DBUtil.class","ERROR","Exception occurred during establishing connection with database."+ex.getMessage());
}
return conn;
}
public PreparedStatement getPreparedStatement(String Query)
{
try{
conn = getConnection();
pstmt = conn.prepareStatement(Query);
}catch(Exception ex)
{
LogUtil.logger("MDALibrary","DBUtil.class","ERROR","Exception occurred during calling procedure"+ex.getMessage());
ex.printStackTrace();
closePreparedStatement();
}
return pstmt;
}
public void closePreparedStatement()
{
try{
this.pstmt.close();
pstmt = null;
this.conn.close();
conn = null;
}catch(Exception e)
{
LogUtil.logger("MDALibrary","DBUtil.class","ERROR","Exception occurred during calling procedure"+e.getMessage());
e.printStackTrace();
}
}
}
public class GetPrevEmplTypeImpl {
public DataObject getTransactionDetails(DataObject transactionInput) {
LogUtil.logBO("MDADB","MDADB --> GetPrevEmplTypeImpl","INFO"," Input of GetPrevEmplTypeImpl is : --> transactionInput: ",transactionInput);
DataObject TransOutput = BOUtil.createDataObject("Transaction");
String TaleoReq = transactionInput.getString("Transaction_Type");
DBUtil db = new DBUtil();
PreparedStatement pstmt = null;
try {
String sQuery = "";
ResourceBundle rbQuery = ResourceBundle.getBundle("MDA_Queries");
sQuery = rbQuery.getString("GetPrevEmplType");
LogUtil.logger("MDADB","MDADB --> GetPrevEmplTypeImpl","INFO","Query::"+sQuery);
pstmt = db.getPreparedStatement(sQuery);
pstmt.setString(1, TaleoReq);
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
TransOutput.setString("Status", rs.getString("PREV_EMPL_TYPE"));
}
} catch (Exception sqle) {
LogUtil.logger("MDADB","MDADB --> GetPrevEmplTypeImpl","ERROR","Exception occurred in GetPrevEmplTypeImpl ");
LogUtil.logger("MDADB","MDADB --> GetPrevEmplTypeImpl","ERROR","OutPut from getMessage() "+sqle.getMessage());
LogUtil.logger("MDADB","MDADB --> GetPrevEmplTypeImpl","ERROR","OutPut from getCause() "+sqle.getCause());
}
finally{
db.closePreparedStatement();
try{
pstmt.close();
pstmt=null;
LogUtil.logger("MDADB","MDADB --> GetPrevEmplTypeImpl","INFO","dbConnection closed in GetPrevEmplTypeImpl");
}catch(Exception e){
LogUtil.logger("MDADB","MDADB --> GetPrevEmplTypeImpl","ERROR","Exception occurred while closing the connection for pstmt : "+pstmt);
LogUtil.logger("MDADB","MDADB --> GetPrevEmplTypeImpl","ERROR","OutPut from getMessage() "+e.getMessage());
LogUtil.logger("MDADB","MDADB --> GetPrevEmplTypeImpl","ERROR","OutPut from getCause() "+e.getCause());
}
}
return TransOutput;
}
}
[/code]
In DBUtil.java, does closePreparedStatement() closes the preparedStatement and connection used for the GetPrevEmplTypeImpl transaction? In DBUtil.java, conn and pstmt are declared as private global variables and during closing these DBObjects in GetPrevEmplTypeImpl.java, no reference to the object used is passed to db.closePreparedStatement().
Please share the best way of handling these connections.
Thanks
Usha.
•
•
Join Date: Aug 2008
Posts: 1,158
Reputation:
Solved Threads: 136
you probably want to move this to DBUtil and return a resultset from it
also unless its an abstract class with the prepared statement that you manipulate, i wouldn't try to declare in two places, either let it be passed in, or let it be constructed in DBUtil
Java Syntax (Toggle Plain Text)
ResultSet rs = pstmt.executeQuery();
also unless its an abstract class with the prepared statement that you manipulate, i wouldn't try to declare in two places, either let it be passed in, or let it be constructed in DBUtil
Custom Application & Software Development
www.houseshark.net
www.houseshark.net
![]() |
Similar Threads
- facing problem in database connectivity in java to mysql (Java)
- JDBC Driver for SQL Server 2005, Class not found Exception (Java)
- facing problem in database connectivity in jsp to mysql (JSP)
- MySQL connectivity issue (Java)
- Xml Database Connectivity in Java (Java)
Other Threads in the Java Forum
- Previous Thread: What's the most efficent way to randomize a Queue?
- Next Thread: Internet Browser
| Thread Tools | Search this Thread |
2dgraphics @param account affinetransform android api apple applet application arc arguments array automation banking binary binarytree bluetooth chatprogramusingobjects class client code color compare component count database derby design detection eclipse eclipsedevelopment encryption error fractal game givemetehcodez graphics gridlayout gui guitesting helpwithhomework homework html if_statement image integer j2me java java.xls javadesktopapplications javaprojects jlabel jni jpanel jtextfield julia keytool keyword linux list macintosh method methods midlethttpconnection mobile monitoring netbeans nullpointerexception object open-source pong problem producer program projectideas property reference replaysolutions ria rim scanner server set size sms sourcelabs splash sql sqlite swing terminal testautomation threads transforms tree ui unicode validation web windows






