在Java中调用存储函数错误

时间:2022-01-17 23:12:36

I am trying to call a stored function in Java. Function has no package, it's placed under user (schema) USER and returns a cursor. I tried twoways to call it and none of these works.

我试图用Java调用存储的函数。函数没有包,它放在用户(模式)USER下并返回一个游标。我试着打电话给它,但这些都没有。

First one

Query query = coreDao.getEntityManager().createNativeQuery("{call USER.gen_rephead_sm_task_report(?, ?) }");
query.setParameter(1, dateFrom);
query.setParameter(2, dateTo);
List<?> queryResult = query.getResultList();

I got a not a procedure or not defined

我得到的不是程序,也没有定义

I also tried this approach with

我也试过这种方法

select gen_rephead_sm_task_report(?, ?) from dual

as a createNativeQuery parameter but with the same result.

作为createNativeQuery参数但具有相同的结果。

Second one

Connection connection = dataSource.getConnection();//javax.sql.DataSource
CallableStatement statement = connection.prepareCall("{? = call USER.gen_rephead_sm_task_report(?, ?) }");
statement.registerOutParameter(1, OracleTypes.CURSOR);//oracle.jdbc.OracleTypes
statement.setDate(2, new java.sql.Date(dateFrom.getTime()));
statement.setDate(3, new java.sql.Date(dateTo.getTime()));
statement.executeQuery();
ResultSet set = ((OracleCallableStatement) statement).getCursor(1);

I got the ClassCastException on the last line (obviously OracleCallableStatement doesn't implement the CallableStatement). So which types shoud I use here?

我在最后一行得到了ClassCastException(显然OracleCallableStatement没有实现CallableStatement)。我在这里使用哪种类型?

1 个解决方案

#1


0  

Ok, so I found a link which solves my problem (iDevelopment). Here is what I used

好的,所以我找到了一个解决我的问题的链接(iDevelopment)。这是我用的

connection = dataSource.getConnection();
CallableStatement statement = connection.prepareCall(TIMESHEET_QUERY);
statement.registerOutParameter(1, OracleTypes.CURSOR);
statement.setDate(2, new java.sql.Date(dateFrom.getTime()));
statement.setDate(3, new java.sql.Date(dateTo.getTime()));
statement.execute();

ResultSet set = (ResultSet) statement.getObject(1);

Everything is straight from javax.sql.* so there is no need for Oracle Specific API.

一切都直接来自javax.sql。*所以不需要Oracle Specific API。

#1


0  

Ok, so I found a link which solves my problem (iDevelopment). Here is what I used

好的,所以我找到了一个解决我的问题的链接(iDevelopment)。这是我用的

connection = dataSource.getConnection();
CallableStatement statement = connection.prepareCall(TIMESHEET_QUERY);
statement.registerOutParameter(1, OracleTypes.CURSOR);
statement.setDate(2, new java.sql.Date(dateFrom.getTime()));
statement.setDate(3, new java.sql.Date(dateTo.getTime()));
statement.execute();

ResultSet set = (ResultSet) statement.getObject(1);

Everything is straight from javax.sql.* so there is no need for Oracle Specific API.

一切都直接来自javax.sql。*所以不需要Oracle Specific API。