Executing .sql file in java

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • SakthiPriya28
    New Member
    • Feb 2012
    • 9

    Executing .sql file in java

    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:
    -------------

    Code:
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileFilter;
    import java.io.IOException;
    import java.io.InputStreamReader;
    
    public class StackFlow {
    	
    	private static String script_location = "";
    	private static String file_extension = ".sql";
    	private static ProcessBuilder processBuilder =null;
    	
    
    	public static void main(String[] args) {
    	    try {
    	        File file = new File("D:/script1");
    	        File [] list_files= file.listFiles(new FileFilter() {
    
    	            public boolean accept(File f) {
    	                if (f.getName().toLowerCase().endsWith(file_extension))
    	                    return true;
    	                return false;
    	            }
    	        });
    	        int count=0;
    	        for (int i = 0; i<list_files.length;i++){
    	            script_location = "@" + list_files[i].getAbsolutePath();//ORACLE
    	            processBuilder = new ProcessBuilder("sqlplus","ESB22/ESB22", script_location); //ORACLE
    	            //script_location = "-i" + list_files[i].getAbsolutePath();
    	            //  processBuilder = new ProcessBuilder("sqlplus", "-Udeep-Pdumbhead-Spc-de-deep\\sqlexpress-de_com",script_location);
    	            processBuilder.redirectErrorStream(true);
    	            Process process = processBuilder.start();
    	            BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
    	            String currentLine = null;
    	            while ((currentLine = in.readLine()) != null) {
    	                System.out.println(" "  + currentLine);
    	                
    	               // System.out.println("Line----"+count++);
    	            }
    	        }
    	    } catch (IOException e) {
    	        e.printStackTrace();
    	    }catch(Exception ex){
    	        ex.printStackTrace();
    	    }
    	}
    
    }
    Last edited by Meetee; Feb 24 '12, 08:32 AM. Reason: Use code tags [CODE][/CODE] around your code
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    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.

    Comment

    • SakthiPriya28
      New Member
      • Feb 2012
      • 9

      #3
      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.

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        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
        [CODE=java]ProcessBuilder processBuilder = new ProcessBuilder( "sqlplus","ESB2 2/ESB22", script_location ); [/CODE]

        Comment

        • SakthiPriya28
          New Member
          • Feb 2012
          • 9

          #5
          Hi,
          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.

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            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?

            Comment

            • SakthiPriya28
              New Member
              • Feb 2012
              • 9

              #7
              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 .

              Comment

              • r035198x
                MVP
                • Sep 2006
                • 13225

                #8
                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

                [CODE=java]echo exit | sqlplus userName/password@connec t @scriptname[/CODE]

                Comment

                Working...