how to handle SQL exceptions to call script having args via java ?


 
Thread Tools Search this Thread
Top Forums Programming how to handle SQL exceptions to call script having args via java ?
# 1  
Old 09-28-2009
Error how to handle SQL exceptions to call script having args via java ?

Hi,

i want to call shell script via java + in that shell script i m doing some sql operation by connecting to sqlplus .

i want to return 0 if successful exeution of SQL operations else 1 ;

is it possible ?
Code:
#!/bin/sh
Name=$1;
export ORACLE_HOME

$ORACLE_HOME/bin/sqlplus scott@ORA/tiger <<EOF
spool /root/spool1.txt;
select * from tab where tname like '$Name%';
spool off;
EOF

echo "got query data"

exit;


java code
Code:
import java.util.*;
import java.io.*;

public class Script {
	public static void main ( String[] s)
	{
		Runtime r= Runtime.getRuntime();//reference to runtime java environment
		Process P=null;
		String cmd2 = "/root/script.sh school";
		
		try {
			P=r.exec(cmd2);
	
			System.out.println(	P.waitFor());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}


is it possible to get info whether SQL query executed properly or not ?
# 2  
Old 09-28-2009
you have a few choices:

1) eval spool1.txt in the shell script and return a value based on the result, then check the value in java

2) do step one entirely in java by reading in spool1.txt

3) if you don't actually need your spool.txt for later processing, you could instead nest your query in case statement and return the success/fail as different conditions. you still have to eval the result though.

4) used jdbc to connect to your system and do it all in java.
# 3  
Old 09-29-2009
Error

yes.
how shell script script will come to know weather sql thing is executed properly or not ?

can we return variable from shell to java ?

i have sample code in above ..could u plz add up there to achieve this ?
# 4  
Old 09-29-2009
eval your spool file in shell script
Code:
#!/bin/sh
Name=$1;
export ORACLE_HOME

$ORACLE_HOME/bin/sqlplus scott@ORA/tiger <<EOF
spool /root/spool1.txt;
select * from tab where tname like '$Name%';
spool off;
EOF

LINECOUNT=`wc -l spool1.txt|cut -f1 d' '`

if [ $LINECOUNT -gt 0 ]
then
  exit 0 #SUCCESS
else
  exit 1 #FAIL
fi

eval your shell output in java (I haven't done the java bit in a while, but try this : )
Code:
   
try {
   P=r.exec(cmd2);
   String exitVal = P.exitValue();
}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Getopts how to handle missing '-' in command line args.

I'm using getopts to process command line args in a Bash script. The code looks like this: while getopts ":cfmvhs:t:" option; do case $option in c) operationMode="CHECK" ;; f) operationMode="FAST" ;; m) ... (6 Replies)
Discussion started by: gencon
6 Replies

2. UNIX for Dummies Questions & Answers

Call sql script

I want to call the sql query from UNIX..but how to set page size and other necessary parameters i don't know plz guide me how to do this (2 Replies)
Discussion started by: sagar_1986
2 Replies

3. UNIX for Advanced & Expert Users

call sql through shell script

Hi i am not able to connect sqlplus my script is as follows $ORACLE_HOME/bin/sqlplus << ! > /tmp/extract/DM.txt and output is SQL*Plus: Release 11.1.0.7.0 - Production on Wed Jan 18 02:53:54 2012 Copyright (c) 1982, 2008, Oracle. All rights reserved. Enter user-name: t175481... (1 Reply)
Discussion started by: tushar_spatil
1 Replies

4. UNIX for Advanced & Expert Users

java Exceptions color

Hi, I call a java program from a cron job and i need to display the exceptions or Errors thrown by java(basically stacktrace) in the unix/linux console in the red color.Is it possible to do that? If so, pls. give me some pointers how to do that. eg: Exception in thread "main"... (0 Replies)
Discussion started by: ramse8pc
0 Replies

5. Shell Programming and Scripting

How to call Java by using shell script

Hi All, I am new to shell script, just wanted you guy to help. How do i call a java program by using shell script, pls give some samle code for it, thank you ver much. (2 Replies)
Discussion started by: aaabbb123123
2 Replies

6. Solaris

How to catch and handle Makefile exceptions

I have a simple makefile which I use to sync libraries files from /source/sybase directory on the source machine where the Makefile resides to /destination/folder on the various destination machines like machine1, machine2, machine3 using solaris utility 'rsync'. So when I run "make -f makefilename... (7 Replies)
Discussion started by: waavman
7 Replies

7. Solaris

Java Exceptions while installing Oracle

Hello. I was trying to installe oracle 10g on solaris t0 x86 and got few exception? Could you please suggest, what might be going wrong? $ ls -l total 32 drwxr-xr-x 9 oracle dba 512 Nov 21 03:50 doc drwxr-xr-x 5 oracle dba 512 Nov 21 03:50 install drwxr-xr-x 2... (5 Replies)
Discussion started by: panchpan
5 Replies

8. Shell Programming and Scripting

how can i call a shell script from pl/sql

I would like to call the shell script from pl/sql and i need to uses the value returned by the shell script in pl/sql procedure. can any one suggest me how can i do that? (3 Replies)
Discussion started by: rajesh.P
3 Replies

9. UNIX for Dummies Questions & Answers

how can a call shell script from pl/sql

I like to call a shell script from pl/sql proceduere and i have to use the shell script return value in that procedure. i am using oracle 9i and cygwin. can any one suggest me how can i do this (0 Replies)
Discussion started by: rajesh.P
0 Replies

10. Shell Programming and Scripting

How to call pl/sql in unix script

sample code as following: test_sql(){ #test#echo test_sql str=`$ORACLE_BIN/sqlplus -s $user/$passwd <<EOM set verify off set heading off set feedback off #--------start pl/sql { DECLARE CURSOR pah_cs IS select id from table where letter = 'abcd';... (6 Replies)
Discussion started by: YoYo
6 Replies
Login or Register to Ask a Question