我有一个包含多个查询和表的 ORACLE SQL 脚本,我想在程序开始时从我的 Java 程序运行该脚本,以确保一切都在正确的位置。我找到了运行脚本的代码,但由于某种原因它不起作用。任何人都可以向我提供 sample ,以便我可以遵循它。
这是我发现的:
try {
String line;
Process p = Runtime.getRuntime().exec ("psql -U sas -d oracle -h @localhost -f Lab_05_Tables.sql");
BufferedReader input =new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((line = input.readLine()) != null) {
System.out.println(line);
}
input.close();
}
catch (Exception err) {
err.printStackTrace();
}
但它不起作用。
错误
java.io.IOException: Cannot run program "psql": CreateProcess error=2, The system
cannot find the file specified
最佳答案
如果您有资源,将 SQL 从您的脚本移植到 Java 程序本身会好得多。
参见 Java JDBC tutorial .
关于java - 如何从java执行sql文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8304516/