8/12/2010

How to call database store procedure with return parameter from EJB3


@Resource(mappedName = "java:/myDatasource")
private DataSource ds;

public void callSetLoginUserId(String userId) throws Exception {
Connection conn = null;
CallableStatement st = null;
try {
String sql = "{ ? = call my_pkg.create_user(?) }";
conn = ds.getConnection();
st = conn.prepareCall(sql);
st.registerOutParameter(1, Types.BIGINT);
st.setString(2, "username");
st.execute();
Long userId = st.getLong(1);
}
catch(Exception e) {
log.error("calling my_pkg.create_user has failed", e);
throw e;
}
finally {
try {
if (st != null) {
st.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
// ignore
}
}

No comments: