Created
November 3, 2011 12:58
-
-
Save aziz781/1336418 to your computer and use it in GitHub Desktop.
java: how to Call Stored Procedure and DB connection from JNDI
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static void processData(int param1, int param2) | |
{ | |
CallableStatement cs=null; | |
Connection conn=null; | |
try{ | |
// get JNDI JDBC connection | |
InitialContext ctxt = new InitialContext(); | |
DataSource ds = (DataSource) ctxt.lookup("java:/myAppDS"); | |
conn = ds.getConnection(); | |
// Call a procedure with one IN parameter | |
cs = conn.prepareCall("{call process_data_proc(?,?)}"); | |
// Set the value for the IN parameter | |
cs.setInt(1, param1); | |
cs.setInt(2, param2); | |
// Execute the stored procedure | |
cs.execute(); | |
}catch(Exception exp) | |
{ | |
exp.printStackTrace(); | |
}finally | |
{ | |
// This finally clause is always executed - even in error | |
// conditions CallableStatement and Connections will always be closed | |
try | |
{ | |
if (cs != null) | |
cs.close(); | |
} | |
catch(Exception e) {} | |
try | |
{ | |
if (conn != null) | |
conn.close(); | |
} | |
catch (Exception e){} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment