简体   繁体   中英

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. Those operations include some table space creation, schema definition etc.

I was trying to prepare an installation wizard using java application. This wizard needs to run these commands. My specific question is: How to run oracle commands script files from inside my java application? 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

I used to use PL/SQL command line functionality to execute the sql commands as a privileged user.

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.

The best here I think is converting to actual PL/SQL, say a procedure:

    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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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