I want to connect to Oracle as SYS
from SQL*Plus in Java. But I am not able to connect.
But I am able to connect as user named SCOTT
. My code snippet i
You're passing all the connection information as a single value; equivalent to this from a command line:
sqlplus "sys as sysdba/tiger@<connect_string>"
which would get the same response of printing the SQL*Plus logon help. You also have your password in the wrong place but it isn't getting that far. From a command line this would work:
sqlplus "sys/tiger" "as" "sysdba@<connect_string>"
so you need to pass 5 arguments to ProcessBuilder
, something like:
String sqlCmd = "sqlplus";
String arg1 = "sys/tiger";
String arg2 = "as";
String arg3 = "sysdba@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(Host=hostname)(Port=PORT ID))(CONNECT_DATA=(SID=SID)))";
String arg4 = fileName;
...
ProcessBuilder pb = new ProcessBuilder(sqlCmd, arg1, arg2, arg3, arg4);
This will still only work if your environment is configured to allow remote connection as sysdba
. Doing anything as sys
should be very rare, and having a script you want to run as sys
seem unusual enough for a Java wrapper to seem like overkill - and makes it seem like you might connect as sys
routinely, which is not a good idea - but maybe this is just a learning exercise.
Think your arg1 should be as below:
String arg1 = "scott as sysdba/<syspwd>@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(Host=hostname)(Port=PORT ID))(CONNECT_DATA=(SID=SID)))";
You can easily call sqlplus from java like below.
String stringCommand =
"sqlplus " + dbUser + "/" + dbPassword + "@(description=(address=(protocol=TCP)" +
"(host=" + dbHost + ")(port=" + dbPort + "))(connect_data=(service_name=" + dbName + "))) " +
"@" + sqlScriptFile + "";
int exitCode;
Runtime rt = Runtime.getRuntime();
Process process = null;
try {
process = rt.exec(stringCommand);
exitCode = process.waitFor();
} finally {
process.destroy();
}
I found the answer by hit and try and its the connection string.
If one wants to connect as sysdba/sysoper the connection string should be like:
public static void test_script () {
String fileName = "@t.sql";
String sqlPath = "D:\\";
String sqlCmd = "sqlplus";
// IP_address,portid and sid are variables to be entered and t.sql is the file to be read .It contains show user command
String arg3 = "sys/oracle123@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(Host=IP_address)(Port=portid))(CONNECT_DATA=(SID=sid))) as sysdba";
String arg4= fileName;
try {
String line;
ProcessBuilder pb = new ProcessBuilder(sqlCmd,arg3,arg4);
Map<String, String> env = pb.environment();
env.put("VAR3", arg3);
env.put("VAR4", arg4);
pb.directory(new File(sqlPath));
pb.redirectErrorStream(true);
Process p = pb.start();
BufferedReader bri = new BufferedReader(new InputStreamReader(p.getInputStream()));
BufferedReader bre = new BufferedReader(new InputStreamReader(p.getErrorStream()));
while ((line = bri.readLine()) != null) {
System.out.println(line);
}
bri.close();
while ((line = bre.readLine()) != null) {
System.out.println(line);
}
bre.close();
System.out.println("\n\n\n");
System.out.println("Done.");
} catch (Exception err) {
err.printStackTrace();
}
}