wood burning stoves 2.0*
The moose likes Java in General and the fly likes problem calling isql or osql from java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "problem calling isql or osql from java" Watch "problem calling isql or osql from java" New topic
Author

problem calling isql or osql from java

anurag priya
Ranch Hand

Joined: Dec 19, 2000
Posts: 72
hi ! if any one can tell me how to Call isql or osql (MSSQL SERVER) from java and get the exitvalue for loging !
i am trying this code but the code hangs when i run !i got this code from javaworld link :
http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html
===========================================================================

import java.util.*;
import java.io.*;

class StreamGobbler extends Thread
{
InputStream is;
String type;

StreamGobbler(InputStream is, String type)
{
this.is = is;
this.type = type;
}

public void run()
{
try
{
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line=null;
while ( (line = br.readLine()) != null)
System.out.println(type + ">" + line);
} catch (IOException ioe)
{
ioe.printStackTrace();
}
}
}

public class WindowsExec
{
public static void main(String args[])
{
if (args.length < 1)
{
System.out.println("USAGE: java GoodWindowsExec <cmd>");
System.exit(1);
}

try
{
String osName = System.getProperty("os.name" );
String[] cmd = new String[3];

if( osName.equals( "Windows 2000" ) )
{
cmd[0] = "cmd.exe" ;
cmd[1] = "/C" ;
cmd[2] = args[0];
}
else if( osName.equals( "Windows 95" ) )
{
cmd[0] = "command.com" ;
cmd[1] = "/C" ;
cmd[2] = args[0];
}

Runtime rt = Runtime.getRuntime();
System.out.println("Execing " + cmd[0] + " " + cmd[1]
+ " " + cmd[2]);
Process proc = rt.exec(cmd);
// for error message
StreamGobbler errorGobbler = new
StreamGobbler(proc.getErrorStream(), "ERROR");

// for output
StreamGobbler outputGobbler = new
StreamGobbler(proc.getInputStream(), "OUTPUT");

// kick them off
errorGobbler.start();
outputGobbler.start();

// exit value
int exitVal = proc.waitFor();
System.out.println("ExitValue: " + exitVal);
} catch (Throwable t)
{
t.printStackTrace();
}
}
}//end of class
===========================================================================
The code compiles and runs well for other dos commands like this

$ java WindowsExec "dir *.java" ---> this works fine

but when i try to run it for isql or osql like this the code hangs

$ java GoodWindowsExec "isql.exe /Usa /Psa /i test.sql " where /U = username /P =Password and /i = input sql file

can anyone please help !


Anurag priya
SCJP 2(83%); Brain Bench Certified for: Masters level EJB2.0(97%) - Masters level J2EE (83%) - BEA-WebLogic Application Server8.1 (86% )
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

what's about editing your post and inserting some code-tags?

[ September 25, 2004: Message edited by: Stefan Wagner ]

http://home.arcor.de/hirnstrom/bewerbung
 
Don't get me started about those stupid light bulbs.
 
subject: problem calling isql or osql from java