oracle 如何使用 JDBC 执行过程
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41997729/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to execute a procedure with JDBC
提问by westman379
This is my function :
这是我的功能:
public static void execute_delete_on_db(String pass, String login, String port,
String host, String table_name, String file_path) throws Exception {
Class.forName("oracle.jdbc.OracleDriver");
Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@//"
+ host + ":" + port + "/xe", login, pass);
PreparedStatement statement = null;
Statement stmt = null;
String query = "delete from AA_ALL";
stmt = conn.createStatement();
stmt.executeQuery(query);
sql_statement.close();
conn.commit();
conn.close();
}
At least this is the most important part of my function. Above code works fine. I tried to execute my procedure by calling this sql statement :
至少这是我职能中最重要的部分。上面的代码工作正常。我试图通过调用这个 sql 语句来执行我的过程:
EXECUTE delete_all_rows_from_table('all');
In Java it looked like this :
在 Java 中它看起来像这样:
String query = "EXECUTE delete_all_rows_from_table('all')";
On the database it is working fine, but in Java this is giving me the error. Can you tell me what am I doing wrong?
在数据库上它工作正常,但在 Java 中这给了我错误。你能告诉我我做错了什么吗?
回答by YCF_L
You can call
your procedure and not execute it like so :
你可以call
你的程序,而不是像这样执行它:
String query = "{call delete_all_rows_from_table(?)}";
CallableStatement statement = connection.prepareCall(query);
statement.setString(1, "all");
statement.execute();
You can learn more here : JDBC CallableStatement – Stored Procedure OUT parameter exampleand JDBC Stored Procedure
您可以在此处了解更多信息:JDBC CallableStatement – 存储过程 OUT 参数示例和JDBC 存储过程