1

I'm familiar with Java JDBC and use it often to run simple SQLs. However, I want to run something like the below. This is more PL/SQL than regular SQL, hence my problems. I am not running this from the Oracle box but from my own computer. Can someone help me to run this kind of PL/SQL using Java?

spool C:\count.txt;
DEFINE date="TO_DATE ('08-29-2011','mm-dd-yyyy')";
SET NEWPAGE 0;
SET SPACE 0;
SET LINESIZE 500;
SET PAGESIZE 0;
SET ECHO OFF;
SET FEEDBACK OFF;
SET VERIFY OFF;
SET HEADING OFF;
SET TRIMSPOOL ON;
alter session set nls_date_format='yyyy-mm-dd hh24:mi';
select 'TABLE1', count(*) from SCHEMA.TABLE1 where modifyts < &date;
select 'TABLE2', count(*) from SCHEMA.TABLE2 where modifyts < &date;
select 'TABLE3', count(*) from SCHEMA.TABLE3 where modifyts < &date;
spool off;
2
  • Hmm, are you asking how to run pl\sql from your Java Client. I am sorry I am having trouble understanding your question and especially your solution. If you are just trying to run PL\SQL, then you should create a function or procedure in your oracle database and use java to just call the routine. Commented Sep 7, 2011 at 17:58
  • Did you already try plain Statement.execute(String)? For the SELECTs you'll probably have to use the getResultSet(), getUpdateCount(), getMoreResults() mentioned in that methods @SeeAlso. Commented Sep 7, 2011 at 18:00

1 Answer 1

10

SPOOL, DEFINE, and SET are all SQL*Plus commands. They are not valid in PL/SQL or in SQL. You cannot, therefore, run this sort of script through a tool other than SQL*Plus (or a tool that supports SQL*Plus commands like SQL Developer or Toad).

You could, of course, have your Java application call out to the operating system to invoke the SQL*Plus executable (assuming it is installed on the machine that the Java application is running on) and pass the script to SQL*Plus. But that's generally way more complexity than you need. It would make more sense to either just use SQL*Plus or to issue just the SELECT statements from your Java application and use Java's file I/O classes to write the results to a file.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, this is what I should be doing. I didn't remember that most of those commands are SQL*Plus only.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.