2

I have to execute an sql statement through SQLPLUS in JAVA without using the file As we do it in command line in sqlplus. I need to make connection and execute the sql statement in the same single line in Java

echo "show user;" | sqlplus USERNAME/PASSWORD as sysdba

I need to find the connection string for that. How to do the same thing in Java??

Every suggestion is welcomed.

saurabhk
  • 140
  • 1
  • 4
  • 14
  • Its a different question totally. In this question my requirement is to create connection and run SQL statement in the same line rather than using a file in its syntax. – saurabhk Apr 14 '14 at 12:06
  • 1
    I don't see any difference. In both cases you (try to) run `sqlplus` from within Java. The connection is always done by `sqlplus`, never from within your Java program. –  Apr 14 '14 at 12:09

1 Answers1

1

There's an article here that describes better ways of calling the Runtime.getRuntime().exec()

The linked example may be a bit dated, but It still works.

The following code is based off this

import java.io.*;

public class doscmd
{
    public static void main(String args[])
    {
        try
        {
            String[] cmd = new String[3];


            cmd[0] = "cmd.exe" ;
            cmd[1] = "/C" ;
            cmd[2] = args[0];

            Process p=Runtime.getRuntime().exec(cmd);

            BufferedReader reader=new BufferedReader(
                new InputStreamReader(p.getInputStream())
            );

            BufferedReader error=new BufferedReader(
                new InputStreamReader(p.getErrorStream())
            );

            String line;
            while((line = reader.readLine()) != null)
            {
                System.out.println(line);
            }

            String errLine;
            while((errLine = error.readLine()) != null)
            {
                System.out.println(errLine);
            }
        }
        catch(IOException e1) {e1.printStackTrace();}


        System.out.println("Done");
    }
}


Since I don't have sysdba access, I did try:

java doscmd "@echo show user; | sqlplus -L USERNAME/PASSWORD@DATABASE"

and it worked.

You should be able to do:

java doscmd "@echo show user; | sqlplus -L USERNAME/PASSWORD@DATABASE as sysdba"

I got

ERROR:
ORA-01031: insufficient privileges

Which leads me to believe it would work as well.

Community
  • 1
  • 1
Mike
  • 3,186
  • 3
  • 26
  • 32