Skip to content

Instantly share code, notes, and snippets.

@aziz781
Created November 3, 2011 12:58
Show Gist options
  • Save aziz781/1336418 to your computer and use it in GitHub Desktop.
Save aziz781/1336418 to your computer and use it in GitHub Desktop.
java: how to Call Stored Procedure and DB connection from JNDI
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