Poniżej kilka przykładów wywołań:
1) Wywołanie funkcji zwracającej liczbę osób o podanym imieniu i nazwisku:
create or replace PACKAGE EXAMPLE_PACKAGE AS
FUNCTION COUNT_USERS ( fname VARCHAR2, lname VARCHAR2 ) RETURN NUMBER;
END EXAMPLE_PACKAGE;
create or replace PACKAGE BODY EXAMPLE_PACKAGE AS
FUNCTION COUNT_USERS ( fname VARCHAR2, lname VARCHAR2 ) RETURN NUMBER AS
users_count NUMBER(12);
BEGIN
begin
select count(id) into users_count from USERS where f_name = fname and l_name = lname;
exception
when NO_DATA_FOUND then
users_count := 0;
end;
return users_count;
END EXAMPLE_PACKAGE;
public long countUsersByName(final String fname,
final String lname) {
Long result = (Long) hibernateTemplate.execute(new HibernateCallback() {
Long count= null;
public Object doInHibernate(final Session session)
throws HibernateException, SQLException {
session.doWork(new Work() {
public void execute(Connection conn) throws SQLException {
CallableStatement stmt = conn.prepareCall("CALL EXAMPLE_PACKAGE.COUNT_USERS (?,?) into ?");
stmt.setString(1, fname);
stmt.setString(2, lname);
stmt.registerOutParameter(3, OracleTypes.VARCHAR);
stmt.execute();
count= Long.valueOf(stmt.getString(3));
}
});
return count;
}
});
return result;
}
2) Wywołanie procedury niezwracającej wyniku:
public void updateUsers(final String oldUsername,final String newUsername) {
hibernateTemplate.execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
Query query = session.createSQLQuery("CALL pck_example.update_username(" +
":old_username, :new_username)");
query.setParameter("old_usernamename", oldUsername);
query.setParameter("new_username", newUsername);
return query.executeUpdate();
}
});
}
Brak komentarzy:
Prześlij komentarz