Wednesday, November 01, 2006

JDBC:A framework for calling a stored procedure.

Here is a generic code snippet for calling a PLSQL stored procedure using JDBC.
It takes the stored procedure callablestatement and an array of variables which need to be passed to this procedure.

 protected void callStoredProcedure(String stmt, Object[] bindVars) {
PreparedStatement st = null;
try {
st = getConnection().createPreparedStatement("begin " + stmt + "; end;", 0);
if (bindVars != null) {
for (int z = 0; z != bindVars.length-1; z++) {
st.setObject(z + 1, bindVars[z]);
}
}
st.executeUpdate();
} catch (SQLException e) {
throw new SQLException(e);
} finally {
if (st != null) {
try {
st.close();
} catch (SQLException e) {
}
}
}
}

No comments: