Spring 如何使用JPA或JDBC使用REF游标调用特定的PL/SQL函数
我的问题是,我想在Spring Boot中调用@PostConstruct方法中的PL/SQL函数 此函数返回有关数据库中表的信息,如上所示:Spring 如何使用JPA或JDBC使用REF游标调用特定的PL/SQL函数,spring,oracle,hibernate,jpa,jdbc,Spring,Oracle,Hibernate,Jpa,Jdbc,我的问题是,我想在Spring Boot中调用@PostConstruct方法中的PL/SQL函数 此函数返回有关数据库中表的信息,如上所示: CREATE OR REPLACE FUNCTION dbINFO return sys_refcursor AS table_info sys_refcursor; begin open table_info for select table_name from all_tables where owner = 'HOMEUS
CREATE OR REPLACE FUNCTION dbINFO
return sys_refcursor AS
table_info sys_refcursor;
begin
open table_info
for select table_name from all_tables where owner = 'HOMEUSER';
return table_info;
end;
我试图使用@NamedStoreProcedureQuery注释,但我没有实体可以使用它。
所以我决定尝试用JDBC来实现它,到目前为止我已经得到了类似的东西
private EntityManager entityManager;
public HospitalApplication(EntityManager entityManager) {
this.entityManager = entityManager;
}
@PostConstruct
private void init() throws SQLException {
System.out.println("\n\n\n\n\n\n");
EntityManagerFactoryInfo info = (EntityManagerFactoryInfo) entityManager.getEntityManagerFactory();
Connection connection = info.getDataSource().getConnection();
CallableStatement stmt = connection.prepareCall("BEGIN dbINFO(); END;");
stmt.registerOutParameter(1, OracleTypes.CURSOR);
stmt.execute();
ResultSet rs = ((OracleCallableStatement)stmt).getCursor(1);
while (rs.next()){
System.out.println(rs.getString(1));
}
rs.close();
stmt.close();
System.out.println("\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
}
问题是,我的程序正在抛出java.sqlException关于列的索引不正确
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hospitalApplication': Invocation of init method failed; nested exception is java.sql.SQLException: Incorrect column index
***编辑****
我尝试了类似的方法,但收到错误消息:
PLS-00905: obiekt HOMEUSER.DBINFO is invalid
ORA-06550: line 1, column 7:
PL/SQL: Statement ignored
请编辑问题并发布完整的错误详细信息,以及哪一行代码是例外。
begindbinfo();结束
在PL/SQL中是非法的-如果不捕获函数的返回值,就不能调用函数。在我看来,您最好将dbInfo
作为过程进行,并为SYS\u REFCURSOR
设置OUT
参数。然后您可以调用它作为CallableStatement cStmt=conn.prepareCall(“{call demoSp(?)}”)代码>@BobJarvis,问题是它必须是一个函数。我想捕获返回值,但我不知道该怎么做。是的,我也尝试了类似的操作,但收到错误PLS-00905 HOMEUSER.DBINFO无效(查看编辑)请编辑问题并发布完整的错误详细信息,以及您的代码中哪一行是例外。BEGIN DBINFO();结束
在PL/SQL中是非法的-如果不捕获函数的返回值,就不能调用函数。在我看来,您最好将dbInfo
作为过程进行,并为SYS\u REFCURSOR
设置OUT
参数。然后您可以调用它作为CallableStatement cStmt=conn.prepareCall(“{call demoSp(?)}”)代码>@BobJarvis,问题是它必须是一个函数。我想捕获返回值,但我不知道该怎么做。是的,我也尝试了类似的操作,但收到错误PLS-00905 HOMEUSER.DBINFO无效(查看编辑)
@PostConstruct
private void init() throws SQLException {
System.out.println("\n\n\n\n\n\n");
EntityManagerFactoryInfo info = (EntityManagerFactoryInfo) entityManager.getEntityManagerFactory();
Connection connection = Objects.requireNonNull(info.getDataSource()).getConnection();
CallableStatement stmt = connection.prepareCall("{ ? = call dbINFO()}");
stmt.registerOutParameter(1, OracleTypes.CURSOR);
stmt.execute();
ResultSet rs = (ResultSet) stmt.getObject(1);
while (rs.next()){
System.out.println(rs.getCursorName());
}
rs.close();
stmt.close();
System.out.println("\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
}