Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am new to Oracle DB and am using Oracle 11g, I want to execute Oracle sql script through java code. my SQL script may contain SQL statements(DDL or DML) or PL/SQL blocks, so I don't want to parse the script in my java code, but would prefer executing the complete script in one go. Hope my question is clear enough, if not let me know and I can clarify.

Would iBatis ScriptRunner work in my scenario?

share|improve this question
    
Solution mentioned stackoverflow.com/a/3055008/948268 works for works for insert, create table. But for script having create or replace trigger it fails with java.sql.SQLSyntaxErrorException: ORA-00900: invalid SQL statement. Any clue why? –  Kuldeep Jain Dec 20 '13 at 11:26
    
Could not find any better solution, so I used this: stackoverflow.com/a/20786458/948268 –  Kuldeep Jain Jan 9 '14 at 13:18

2 Answers 2

I haven't tried it myself but does this work for you? It is the code for a PostgresDB but you may be able to use the Oracle SQL Command Line, too. Here is the link where I found this solution.

import java.io.*;
public class CmdExec {

    public static void main(String argv[]) {
        try {
            String line;
            Process p = Runtime.getRuntime().exec
                    ("psql -U username -d dbname -h serverhost -f scripfile.sql");
            BufferedReader input =
                new BufferedReader
                    (new InputStreamReader(p.getInputStream()));
            while ((line = input.readLine()) != null) {
                System.out.println(line);
            }
            input.close();
        } catch (Exception err) {
            err.printStackTrace();
        }
    }
}
share|improve this answer
    
What would be the command for Oracle db? –  Kuldeep Jain Dec 20 '13 at 9:34
    
try to replace psql -U username -d dbname -h hserverhost -f with sqlplus hr/my_hr_password@host_computer_name –  mvieghofer Dec 20 '13 at 9:39
    
also this post here suggests using the ProcessBuilder. –  mvieghofer Dec 20 '13 at 9:39
    
I want to avoid using SQLplus or any other client to do it. –  Kuldeep Jain Dec 24 '13 at 8:23

I'm assuming that you want to run some "onetime scripts" to create a initial db setup. If this is the case then the solution provided by mvieghofer could work but it assumes that you have the psql tool installed and available in the system path. So this impose some requirements on your development system.

Better and more portable solution would be to indeed use the ibatis scriptrunner in conjunction with Resource. You could write a simple commandline tools taking the script location as an input. Downside of this approach is that your code will have a dependency on Ibatis (large framework dependency to use only one feature)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.