1

I have a abc.sql file containing procedure and insert statements . How can i run the script(abc.sql file) using java code without using script Runner API.

2

1 Answer 1

1

Use ProcessBuilder. Below sample code, I run select query and print the result in console.

public class RunOracleSql {
    public static void main(String[] args) {
        final String fileExtension = ".sql";
        String script_location = "C:/SQLFileLocation";
        try {
            File file = new File("C:/SQLFileLocation");
            File[] listFiles = file.listFiles(new FileFilter() {

                public boolean accept(File f) {
                    if (f.getName().toLowerCase().endsWith(fileExtension))
                        return true;
                    return false;
                }
            });
            for (int i = 0; i < listFiles.length; i++) {
                script_location = "@" + listFiles[i].getAbsolutePath();// ORACLE
                ProcessBuilder processBuilder = new ProcessBuilder("sqlplus",
                        "username/password@database_name", script_location); // ORACLE

                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);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.