简体   繁体   English

从Java应用程序运行oracle命令脚本文件

[英]Running oracle commands script file from java application

I have an application which has an oracle database, so the installation of the application needs running some oracle commands script files to create the database and perform some DDL operations. 我有一个具有oracle数据库的应用程序,因此该应用程序的安装需要运行一些oracle命令脚本文件才能创建数据库并执行一些DDL操作。 Those operations include some table space creation, schema definition etc. 这些操作包括一些表空间创建,模式定义等。

I was trying to prepare an installation wizard using java application. 我试图使用Java应用程序准备安装向导。 This wizard needs to run these commands. 该向导需要运行这些命令。 My specific question is: How to run oracle commands script files from inside my java application? 我的具体问题是:如何从Java应用程序内部运行oracle命令脚本文件? I exactly need a java function that takes the sql commands file path as input parameter and executes the commands within the script files from java taking into the eye of consideration that some parameters (eg some user-selected names)must be passed to the script file which to be executed 考虑到必须将某些参数(例如,某些用户选择的名称)传递给脚本文件,我确实需要一个将sql命令文件路径作为输入参数并从java执行脚本文件中的命令的java函数。要执行的

I used to use PL/SQL command line functionality to execute the sql commands as a privileged user. 我曾经使用PL / SQL命令行功能以特权用户身份执行sql命令。

Here is a section of the file as an example 这是文件的一部分作为示例

ACCEPT TS_NAME CHAR PROMPT 'Enter Table Space Name : ' 
ACCEPT DB_DATAFILE  CHAR PROMPT 'Enter DataBase File full path : '
ACCEPT DB_SIZE  NUMBER PROMPT 'Enter DataBase File Size  (MB) : '
ACCEPT DB_USER CHAR PROMPT 'Enter User Name : '
ACCEPT DB_PASS CHAR PROMPT 'Enter Table Password Name: ' HIDE
ACCEPT DB_TNSNAME CHAR PROMPT 'Enter DATABASE TNSNAME:'
ACCEPT DB_LOG_PATH   CHAR PROMPT 'Enter Log File Path : '
PROMPT Create Tablespace
pause Press Return to continue ...
CREATE TABLESPACE &TS_NAME DATAFILE '&DB_DATAFILE' SIZE &DB_SIZE M 
AUTOEXTEND ON NEXT 1M MAXSIZE UNLIMITED LOGGING PERMANENT 
EXTENT MANAGEMENT LOCAL AUTOALLOCATE BLOCKSIZE 8K SEGMENT SPACE MANAGEMENT MANUAL;
PROMPT Create User
pause Press Return to continue ...
CREATE USER &DB_USER IDENTIFIED BY &DB_PASS DEFAULT TABLESPACE &TS_NAME PROFILE DEFAULT 
QUOTA UNLIMITED ON USERS;
COMMIT;
GRANT CONNECT  TO &DB_USER;
GRANT RESOURCE  TO &DB_USER;
COMMIT;

I would first correct something from your question, the snippet is not PL/SQL but client-side extension by sqlplus and also you are doing a mistake "committing" after a DDL, you don't need to do that as DDL are not part of a transaction. 我首先会从您的问题中纠正一些问题,该代码段不是PL / SQL,而是sqlplus的客户端扩展,并且您在DDL之后犯了一个“提交”错误,您不需要这样做,因为DDL不属于交易。

The best here I think is converting to actual PL/SQL, say a procedure: 我认为最好的方法是转换为实际的PL / SQL,例如一个过程:

    create procedure create_user(ts_name in varchar, db_datafile in varchar, db_size in varchar, db_user in varchar, 
db_pass in varchar, db_tnsname in varchar, db_log_path in varchar)
is
begin
execute immediate 'CREATE TABLESPACE '||TS_NAME||' DATAFILE '|| db_datafile ||' SIZE ' ||db_size||'M'|| 
'AUTOEXTEND ON NEXT 1M MAXSIZE UNLIMITED LOGGING PERMANENT '||
'EXTENT MANAGEMENT LOCAL AUTOALLOCATE BLOCKSIZE 8K SEGMENT SPACE MANAGEMENT MANUAL';

execute immediate 'CREATE USER '||DB_USER||' IDENTIFIED BY '||DB_PASS||' DEFAULT TABLESPACE '||TS_NAME||' PROFILE DEFAULT QUOTA UNLIMITED ON USERS';

execute immediate 'grant connect, resource to '||db_user;

end;

/ /

and then from java just recall the procedure with the appropriate parameters, of course you should check the result of all the statements, mine is just an example and needs to be tested and completed with error checks. 然后从Java调用带有适当参数的过程,当然,您应该检查所有语句的结果,我的只是一个示例,需要进行测试并通过错误检查完成。

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

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