简体   繁体   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 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 我确实需要一个函数,该函数将sql命令文件路径作为输入参数,并在脚本文件内执行命令,同时考虑到必须将某些参数(例如某些用户选择的名称)传递给脚本文件。被执行

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;

You have a SQL*Plus script. 您有一个SQL * Plus脚本。 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). 您不能简单地在Java中运行它,它只能由SQL * Plus(或支持脚本使用的SQL * Plus命令的任何子集的某些其他实用程序)运行。

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. 假设您的Java应用程序在客户端计算机上运行(而不是在客户端通过浏览器访问的中间层上),并且在客户端计算机上安装了Oracle客户端,那么您的Java应用程序可以生成SQL * Plus可执行文件,将脚本传递给它,然后让SQL * Plus处理提示用户输入输入参数。 Of course, that would mean that the user would see a separate window pop up with SQL*Plus running that would ask for input. 当然,这意味着用户将看到一个单独的窗口,该窗口运行SQL * Plus,并要求输入。

Alternately, you could rewrite the SQL*Plus script in Java. 或者,您可以用Java重写SQL * Plus脚本。 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. 这意味着您的Java应用程序将需要提示用户输入表空间名称,例如,使用适合您应用程序的任何接口。 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. 然后,您的应用程序将需要根据用户的输入来构造CREATE USER类的SQL语句,并将这些SQL语句发送到数据库。

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

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