下面的代码在java中不起作用。
Class.forName('oracle.jdbc.driver.OracleDriver');
Connection connection = DriverManager.getConnection('jdbc:oracle:thin:@localhost:1521:test', 'xyz', 'pass');
PreparedStatement stmt = connection.prepareStatement("set role ? identified by ?");
String role = "TEST_ROLE";
String pswd = "TEST_PASS";
stmt.setString(1, role);
stmt.setString(2, pswd);
stmt.execute();
上面的代码抛出异常:
java.sql.SQLSyntaxErrorException: ORA-01937: missing or invalid role name
我在命令模式下尝试了相同的角色和密码,它成功执行了。
最佳答案
您只能使用PreparedStatement的绑定(bind)变量来绑定(bind)值,而不能使用角色名称或其密码等标识符。您必须诉诸字符串操作才能获得此效果(假设这些值没有硬编码,如上所示,在这种情况下您可以将它们放入字符串中并完成它)。例如:
String role = "TEST_ROLE";
String pswd = "TEST_PASS";
String sql = String.format("set role %s identified by %s", role, pswd);
关于java - Oracle - SET ROLE 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42466171/