By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,805 Members | 1,241 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,805 IT Pros & Developers. It's quick & easy.

Executing .sql file in java

P: 9
i am trying to execute a .sql file in java. It works fine when i am giving exit at the end of the .sql file. Is there any solution to run without giving exit at the end of file, please reply me. Thanks in advance.

My java code:

Expand|Select|Wrap|Line Numbers
  1. import;
  2. import;
  3. import;
  4. import;
  5. import;
  7. public class StackFlow {
  9.     private static String script_location = "";
  10.     private static String file_extension = ".sql";
  11.     private static ProcessBuilder processBuilder =null;
  14.     public static void main(String[] args) {
  15.         try {
  16.             File file = new File("D:/script1");
  17.             File [] list_files= file.listFiles(new FileFilter() {
  19.                 public boolean accept(File f) {
  20.                     if (f.getName().toLowerCase().endsWith(file_extension))
  21.                         return true;
  22.                     return false;
  23.                 }
  24.             });
  25.             int count=0;
  26.             for (int i = 0; i<list_files.length;i++){
  27.                 script_location = "@" + list_files[i].getAbsolutePath();//ORACLE
  28.                 processBuilder = new ProcessBuilder("sqlplus","ESB22/ESB22", script_location); //ORACLE
  29.                 //script_location = "-i" + list_files[i].getAbsolutePath();
  30.                 //  processBuilder = new ProcessBuilder("sqlplus", "-Udeep-Pdumbhead-Spc-de-deep\\sqlexpress-de_com",script_location);
  31.                 processBuilder.redirectErrorStream(true);
  32.                 Process process = processBuilder.start();
  33.                 BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
  34.                 String currentLine = null;
  35.                 while ((currentLine = in.readLine()) != null) {
  36.                     System.out.println(" "  + currentLine);
  38.                    // System.out.println("Line----"+count++);
  39.                 }
  40.             }
  41.         } catch (IOException e) {
  42.             e.printStackTrace();
  43.         }catch(Exception ex){
  44.             ex.printStackTrace();
  45.         }
  46.     }
  48. }
Feb 24 '12 #1
Share this Question
Share on Google+
7 Replies

P: 13,264
You are just using Java to kick off a database command so there is no Java code that you can change to make your script work without the exit.
What happens when you run without the exit and why do you want to remove the exit?

P.S Next time you want to post code use code tags so the code appears formatted and all.
Feb 24 '12 #2

P: 9
Hi ,
Thanks for ur reply. When i am trying to run without exit, it execute the first script file only. the other files are not getting executed. i want to execute the all script files in my folder. If u have a solution please reply me.Thanks in advance.
Feb 27 '12 #3

P: 13,264
Since you want to create a new process each time don't use a static ProcessBuilder. Declare and initialize the process builder in the for loop
Expand|Select|Wrap|Line Numbers
  1. ProcessBuilder   processBuilder = new ProcessBuilder("sqlplus","ESB22/ESB22", script_location); 
Feb 27 '12 #4

P: 9
I try as u told. But i got the same problem. But in some scenarios it execute all the script files in the same time without giving exit.
Feb 27 '12 #5

P: 13,264
What do you mean by "But in some scenarios it execute all the script files in the same time without giving exit"?
In what scenarios does it execute all of them?
Also I still don't know why you don't want the exit to be there if it makes your scripts work. Why do you want to remove the exit?
Feb 27 '12 #6

P: 9
For my colleague its working fine without exit. Me too tried for the same code but its not working for me. I am in need to run without exit also want to know the reason .
Feb 27 '12 #7

P: 13,264
EXIT Commits and disconnects. If you don't want to do that in the sql script then you can try doing it from the command by prepending the EXIT using

Expand|Select|Wrap|Line Numbers
  1. echo exit | sqlplus userName/password@connect @scriptname
Feb 27 '12 #8

Post your reply

Sign in to post your reply or Sign up for a free account.