简体   繁体   中英

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 function that takes the sql commands file path as input parameter and executes the commands within the script files 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;

You have a SQL*Plus script. You cannot simply run that in Java, it can only be run by SQL*Plus (or some other utility that supports whatever subset of SQL*Plus commands the script uses).

Assuming that your Java application is running on the client machine (rather than on a middle tier that the client is accessing via a browser) and that the Oracle client is installed on the client machine, your Java application could spawn the SQL*Plus executable, pass it the script, and let SQL*Plus handle prompting the user for the input parameters. Of course, that would mean that the user would see a separate window pop up with SQL*Plus running that would ask for input.

Alternately, you could rewrite the SQL*Plus script in Java. That would mean that your Java application would need to prompt the user for a tablespace name, for example, using whatever interface is appropriate to your application. Your application would then need to construct the SQL statements like CREATE USER based on the user's inputs and send those SQL statements to the database.

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