我有一个 Oracle SQL 过程,我想从我的 Java 代码中调用它,但我不太清楚如何做到这一点。我知道我的程序运行良好,因为当我在 SQLDeveloper 中使用它们时没有任何问题。当我从 Java 代码调用时,我收到 SQLSyntaxErrorException:无效的 SQL 语句。
这是我到目前为止的代码...
private DataSource datasource;
private static final Map<String, String> SQL_KEYS = new HashMap<String, String>();
static {
SQL_KEYS.put("updatePassword",
"EXEC MY_VIEW_SECURITY.CHANGE_PASSWORD(?, ?, ?)");
}
public void updatePassword(String username, String old_password, String new_password) throws SQLException {
Connection con = null;
CallableStatement cstmt = null;
try {
con = datasource.getConnection();
cstmt = con.prepareCall(SQL_KEYS.get("updatePassword"));
cstmt.setString(1, username);
cstmt.setString(2, old_password);
cstmt.setString(3, new_password);
cstmt.executeUpdate();
} catch(Exception ex) {
ex.printStackTrace();
} finally {
con.close();
cstmt.close();
}
}
我的 Java 代码的 updatePassword 字符串语法是否不正确,因为它在 SQLDeveloper 中工作正常?
我尝试在语句中将 EXEC 切换为 CALL,该语句给出了 SQLSyntaxErrorException:从属 session 中不允许 COMMIT。我不确定这是否是朝着正确方向迈出的一步。
最佳答案
EXEC
不是 SQL command所以你不能从 JDBC 使用它。
致run a stored procedure准备CallableStatement
时需要使用{call ...}
:
cstmt = con.prepareCall("{call Y_VIEW_SECURITY.CHANGE_PASSWORD(?, ?, ?)}");
cstmt.setString(1, username);
cstmt.setString(2, old_password);
cstmt.setString(3, new_password);
注意 {
和 }
是传递给 prepareCall()
的字符串的一部分
关于java - 从 Java 调用 SQL 包过程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50844664/