我正在尝试通过从java代码调用过程来从oracle数据库读取数据。 如果我从 sqldeveloper 运行该过程,我可以看到该过程给了我正确的输出。但是当我尝试使用 java 程序运行时,我收到错误。
程序:
CREATE OR REPLACE PACKAGE BODY VISIONEMPLOYEES AS
PROCEDURE Employees(
p_id IN NUMBER,
p_ref_cursor OUT sys_refcursor )
AS
BEGIN
OPEN p_ref_cursor FOR
SELECT id, name, age FROM emp WHERE id = p_id;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END Employees;
END VISIONEMPLOYEES;
Java 代码:
public static void callOracleStoredProcOUTParameter()
{
Connection conn = null;
Statement stmt = null;
CallableStatement cb = null;
String qry = "{VISIONEMPLOYEES.Employees(?,?)}";
try {
conn = getDbConnection();
cb = conn.prepareCall(qry);
cb.setInt(1, 1);
cb.registerOutParameter(2, OracleTypes.CURSOR);
cb.executeUpdate();
String userName = cb.getString(2);
System.out.println("UserName is : " + userName);
} catch (Exception e) {
System.out.println(e);
}
}
java.sql.SQLSyntaxErrorException:ORA-00900:无效的 SQL 语句
请告诉我上面写的代码有什么问题。
最佳答案
您的 qry
字符串中缺少 call
关键字。
String qry = "{call VISIONEMPLOYEES.Employees(?,?)}";
(参见 https://docs.oracle.com/javase/7/docs/api/java/sql/CallableStatement.html )
关于java - 使用过程从数据库中获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35393415/