Hi all,
I have requirement like connecting to remote unix box from java and running unix scripts.
My java code is as follows
The unix script(rdh_count.sh ) i'm using is as followsCode:import com.jcraft.jsch.*; import com.jcraft.jsch.Channel; import com.jcraft.jsch.JSch.*; import com.jcraft.jsch.Session; import com.jcraft.jsch.UserInfo; import java.io.*; public class testrad {public static void main(String args[]) { String user="uname"; String host="hostname"; String cmd="sh rdh_count.sh r10040"; JSch jsch = new JSch(); try {Session session=jsch.getSession(user,host,22); session.setPassword("psw"); UserInfo usrInfo=new MyUserInfo(); session.setUserInfo(usrInfo); session.connect(); Channel channel=session.openChannel("exec"); ((ChannelExec) channel).setCommand(cmd); channel.setXForwarding(true); InputStream in = channel.getInputStream(); channel.connect(); channel.setInputStream(System.in); byte[] tmp = new byte[1024]; while (true) { while (in.available() > 0) { int i = in.read(tmp, 0, 1024); if (i < 0) break; System.out.print(new String(tmp, 0, i)); } if (channel.isClosed()) { in.close(); break; } try {Thread.sleep(1000);} catch (Exception ee) {} } channel.disconnect(); session.disconnect(); } catch(Exception e) { e.printStackTrace(); System.out.println("Exception"+e); } } public static class MyUserInfo implements UserInfo { public String getPassword() { return "password"; } public String getPassphrase() {return ""; } public boolean promptPassword(String arg0) { return true; } public boolean promptPassphrase(String arg0) { return true; } public boolean promptYesNo(String arg0) { return true; } public void showMessage(String arg0) { } } }
Here i'm able to connect to unix and run unix commands like (ls) but when i invoke the unix script from java , the script is actually invoked but in the script i need to connect to db and run some select statements. This is not working. The result i'm getting isCode:dir=/home/oracle/dba if [ $# -ne 1 ] then echo "Enter retailer ID as parameter" else retailer=$1 . $dir/.CBP_LOAD $retailer echo $password count=`sqlplus <<END $retailer/$password@cbpqc SELECT COUNT(1) FROM tablename@dblink WHERE MARKETKEY IN (SELECT MKTKEY FROM STG_DIM_MARKET); exit; END` echo $count | grep -i "ERROR" if [ $? -eq 0 ] then echo "Error in getting the count" exit else echo "There are $count rows for this retailer" fi fi
psw
There are rows for this retailer.
Iam not able to print the value of count variable in my java code. It is just giving the blank space in the place of printing the count variable.But it is printing the password. so there is a problem in running dml commands in sqlplus using java code.
plz help me guys. It is very urgent.plz.............
Thanks in advance.


Reply With Quote



Bookmarks