473,406 Members | 2,698 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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

Expand|Select|Wrap|Line Numbers
  1. import java.io.BufferedReader;
  2. import java.io.File;
  3. import java.io.FileFilter;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6.  
  7. public class StackFlow {
  8.  
  9.     private static String script_location = "";
  10.     private static String file_extension = ".sql";
  11.     private static ProcessBuilder processBuilder =null;
  12.  
  13.  
  14.     public static void main(String[] args) {
  15.         try {
  16.             File file = new File("D:/script1");
  17.             File [] list_files= file.listFiles(new FileFilter() {
  18.  
  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);
  37.  
  38.                    // System.out.println("Line----"+count++);
  39.                 }
  40.             }
  41.         } catch (IOException e) {
  42.             e.printStackTrace();
  43.         }catch(Exception ex){
  44.             ex.printStackTrace();
  45.         }
  46.     }
  47.  
  48. }
Feb 24 '12 #1
7 14367
r035198x
13,262 8TB
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
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
r035198x
13,262 8TB
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
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.
Feb 27 '12 #5
r035198x
13,262 8TB
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
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
r035198x
13,262 8TB
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

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

Similar topics

6
by: vishal | last post by:
hi how can i get the file name from which the current executing file is called. i m making a scrript which will be called from many other scripts. this script performs some calculation and then...
2
by: Your Friend | last post by:
Hello All, I'm relatively new to Python programming but have been working on this problem for a little bit now ... I initially began writing UNIX scripts in Python and thought it was the...
5
by: claus.hirth | last post by:
If I create the function HELLO in schema S01 as follows, @ CREATE FUNCTION S01.HELLO() RETURNS VARCHAR(32) EXTERNAL NAME 'UDFSRVXYZ!sayHelloWorld' LANGUAGE JAVA PARAMETER STYLE DB2GENERAL NO...
2
by: vasilip | last post by:
I am trying to use the XMLUPDATE stored procedure. Installed fine, and seems to work great if I execute the statement from a sql query using the controll center.. Doesn't work from java however. I...
0
by: dmitri | last post by:
I've written a small program in Java that reads data from an Excel sheet does some processing and writes the results to a database. It would be extremely convenient to be able to launch the JAR file...
2
by: XSV | last post by:
I need my application to run a file that will be in the same folder as it ( i dont wanna just use shell ("C:\Launcher.exe i want the users to be able to install anywhere). Ive tried so many different...
1
by: XSV | last post by:
I need my application to run a file that will be in the same folder as it ( i dont wanna just use shell ("C:\Launcher.exe i want the users to be able to install anywhere). Ive tried so many different...
1
by: banging | last post by:
Hi there, I have a question regarding locking of tables so that when two or more people try to write or update the mysql tables, it locks up. Basically I only want one person to write to the...
7
by: mark | last post by:
I am writing a program that will be placed in the startup folder so that on login it executes, it also needs to delete itself so it creates a batch file which cleans up. After that it restarts the...
6
by: mjahabarsadiq | last post by:
Hi, I have written a java code to execute ant targets via Servlet. I have given the code below. import org.apache.tools.ant.*; import org.apache.catalina.ant.*; . . . .
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.