I am trying to run sql commands from java using ProcessBuilder. My problem si that i cant use the same cmd from different functions. For example, i want to run an sql file when i press a button. After that, i want to maintain the connection to sql plus and commit or rollback the transaction by pressing other button. How to keep the process from a button to other?
My first button code:
public class MyExec {
public static ProcessBuilder builder;
public static Process p;
public static BufferedReader bri;
public static BufferedReader bre;
public static BufferedWriter p_stdin;
public static void executeScript(String scriptName, String alias, String path, TextArea txtArea) throws IOException {
//obtaining sql script from a share folder
NtlmPasswordAuthentication userCred = new NtlmPasswordAuthentication("domain",
"user", "pass");
SmbFile smbFile=new SmbFile("path" + scriptName, userCred);
File file = new File("D://" + scriptName);
try (InputStream in = smbFile.getInputStream()) {
Files.copy(smbFile.getInputStream(), file.toPath());
}
//init shell
builder = new ProcessBuilder( "cmd" );
builder.redirectErrorStream(true);
try {
p = builder.start();
} catch (IOException e) {
System.out.println(e);
}
//get stdin of shell
p_stdin =new BufferedWriter(new OutputStreamWriter(p.getOutputStream()));
//executing commands in cmd
try {
p_stdin.write("sqlplus sys/sys@" + alias +" as sysdba");
p_stdin.newLine();
p_stdin.flush();
p_stdin.write("@"+file.toPath());
p_stdin.newLine();
p_stdin.flush();
} catch (IOException e) {
System.out.println(e);
}
// write stdout of shell (=output of all commands)
String line;
bri = new BufferedReader(new InputStreamReader(p.getInputStream()));
bre = new BufferedReader(new InputStreamReader(p.getErrorStream()));
while ((line = bri.readLine()) != null) {
txtArea.appendText(line + "\n");
System.out.println(line + "\n");
}
while ((line = bre.readLine()) != null) {
txtArea.appendText(line + "\n");
System.out.println(line + "\n");
}
System.out.println("Done.");
}
}
Second button code:
Thread t1 = new Thread(new Runnable() {
public void run() {
//commit the sql script which was run with the first button
try {
MyExec.p_stdin.write("commit");
MyExec.p_stdin.newLine();
MyExec.p_stdin.flush();
MyExec.p_stdin.write("exit");
MyExec.p_stdin.newLine();
MyExec.p_stdin.flush();
//output the result of commiting sql script
String line;
BufferedReader bri = new BufferedReader(new InputStreamReader(MyExec.p.getInputStream()));
BufferedReader bre = new BufferedReader (new InputStreamReader(MyExec.p.getErrorStream()));
while ((line = MyExec.bri.readLine()) != null) {
txtArea.appendText(line + "\n");
System.out.println(line + "\n");
}
MyExec.bri.close();
while ((line = MyExec.bre.readLine()) != null) {
txtArea.appendText(line + "\n");
System.out.println(line + "\n");
}
MyExec.bri.close();
System.out.println("Done.");
} catch (IOException e1) {
e1.printStackTrace();
}
}
});
//event on Commit button, running the thread above
public void commit(ActionEvent e) {
t1.start();
}
QueryRunnerwith methods like Connect, BeginTran, Commit/RollbackTran, Execute. Ensure your button listeners use the same instance of the above class.