简体   繁体   English

如何从java执行sql文件

[英]How to execute sql file from java

I have an ORACLE SQL sctipt with several queries and tables, and I wan to run that script from my java program at the starting of the program to ensure that everything is on the right place. 我有一个带有几个查询和表的ORACLE SQL sctipt,我想在程序开始时从我的java程序运行该脚本,以确保一切都在正确的位置。 I found a code to run the script, but it doesn't work for some reason. 我发现了一个运行脚本的代码,但由于某些原因它不起作用。 Can anyone provide me samples so that I can follow it. 任何人都可以提供样品,以便我可以遵循它。

This is what I found : 这是我发现的:

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();
}

But it doesn't work though. 但它不起作用。

Error 错误

java.io.IOException: Cannot run program "psql": CreateProcess error=2, The system     
cannot find the file specified

It would be much better, if you have resources, to port SQLs from your script into Java program itself. 如果您有资源,将SQL从您的脚本移植到Java程序本身会好得多。

See Java JDBC tutorial . 请参阅Java JDBC教程

You are trying to run external program from your code, which is not a good solution. 您正在尝试从代码中运行外部程序,这不是一个好的解决方案。

If you use Ant, you can use sql task in Ant: http://ant.apache.org/manual/Tasks/sql.html 如果使用Ant,则可以在Ant中使用sql任务: http//ant.apache.org/manual/Tasks/sql.html

If you don't use Ant, you should just use the regular jdbc connection. 如果您不使用Ant,则应该使用常规的jdbc连接。

It is possible you're running EnterpriseDB Postgres Plus Advanced Server, which would allow you to run Oracle code using psql. 您可能正在运行EnterpriseDB Postgres Plus Advanced Server,它允许您使用psql运行Oracle代码。 The filename "Lab_05_Tables.sql" suggests an educational environment, so forgive me if it seems I'm unduly treating you as a novice. 文件名“Lab_05_Tables.sql”表明了一个教育环境,所以请原谅我,如果我觉得我过分对待你是一个新手。

Before getting this to work in Java, you should get your executed statement to run by itself at the command line. 在使用Java工作之前,您应该在命令行中单独运行已执行的语句。

psql -U sas -d oracle -h @localhost -f Lab_05_Tables.sql

Lots of things may be wrong here and you need to figure that out. 这里有很多东西可能是错的,你需要弄清楚。 Your command line suggests that you are expecting to run the java program on the same server that is running your database server and has the database client installed, and that you're using the user name "sas" to log into a database named "oracle" with no password required. 您的命令行建议您希望在运行数据库服务器的同一服务器上运行java程序并安装数据库客户端,并且您使用用户名“sas”登录名为“oracle”的数据库“无需密码。 If any of that is not correct then you should find help getting set up on your database before working on your Java app. 如果其中任何一个不正确,那么在使用Java应用程序之前,您应该在数据库上找到帮助。 I would omit the "-h @localhost". 我会省略“-h @localhost”。

If you're certain all that's working and the scope of your problem is just getting Java to execute your command, then you may just need to add psql to your PATH environment variable. 如果您确定所有工作正常并且问题的范围只是让Java执行您的命令,那么您可能只需要将psql添加到PATH环境变量中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM