我有一个有趣的要求。
我们需要在Java存储过程中进行命令行交互。尽管使用dbms_java.grant_permission
命令授予了适当的权限,但我遇到java.io.IOException
,我使用System.in
从java.io.InputStreamReader
读取。{/ p>
问题出在哪里?
Java Source在这里:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.Connection;
import java.sql.SQLException;
import oracle.jdbc.driver.OracleDriver;
public class ExecuteInteractiveBatch {
public static void aFunction() {
Connection connection = null;
try {
int rowFetched = 0;
connection = new OracleDriver().defaultConnection();
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery("SELECT count(1) cnt from sometable where c = 2");
int count = 0;
if (rs.next()) {
count = rs.getInt(1);
}
rs.close();
stmt.close();
rs = null;
stmt = null;
if (count == 1) {
System.out.println("Do you want to continue?");
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String response = reader.readLine();
if ("Y".equalsIgnoreCase(response)) {
stmt = connection.createStatement();
int rowsAffected = stmt.executeUpdate("DELETE from sometable where c=2");
System.out.println("" + rowsAffected + " row(s) deleted");
stmt.close();
}
}
} catch (IOException ex) {
ex.printStackTrace();
} catch (SQLException ex) {
ex.printStackTrace();
} finally {
try {
if (connection != null || !connection.isClosed()) {
connection.close();
}
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
}
答案 0 :(得分:6)
您无法从Oracle内部对控制台执行I / O ...而不是在Java中,而不是在PL / SQL中,无处可去。 Oracle在与用户分开的进程中运行,很可能甚至是不同的计算机,并且在该进程中运行Java存储过程。您将需要在客户端系统上运行一些Java代码来执行控制台I / O.