This discussion is archived
9 Replies Latest reply: Nov 24, 2012 4:35 PM by jschellSomeoneStoleMyAlias RSS

How to run SQL script file on Linux using Java ?

Ajay Sharma Newbie
Currently Being Moderated
Hi,

I need to execute .sql file using java. I used following approach for this.
private void runScriptEvent(java.awt.event.ActionEvent evt) {                                
       
        String sqlOutput = "";
        String sqlPromptLines="";
        String currentFunctionName = "";
        
        if(con!=null){
            
            String userName = jTextField4.getText();
            String password = jPasswordField1.getText();
            String databaseName = jTextField3.getText();
            try {
                
                String script_location = "";
                ProcessBuilder processBuilder =null;
                Process process = null;
                //File file = new File("C:/ScriptFile");
                File file = new File("./SQL_Script");
                //File file = new File("E:\\install\\SQL_Script");
                
                if(file.exists()){
                    File [] list_files= file.listFiles(new FileFilter() {
                                    public boolean accept(File f) {
                                    if (f.getName().toLowerCase().endsWith(".sql"))
                                    return true;
                                    return false;
                                    }
                                   });
                int count = 0;
                
                    for (int i = 0; i<list_files.length;i++){
                        script_location = "@" + list_files.getAbsolutePath();//ORACLE
//currentFunctionName = list_files[i].getName();
StringTokenizer st = new StringTokenizer(list_files[i].getName(), ".");
while(st.hasMoreTokens()) {
currentFunctionName = st.nextToken();
String extention= st.nextToken();
System.out.println("Function Name = "+currentFunctionName + "\t Extention = " + extention);
}

processBuilder = new ProcessBuilder("sqlplus",userName+"/"+password+"@"+databaseName, script_location); //ORACLE

processBuilder.redirectErrorStream(true);
process = processBuilder.start();
BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
String currentLine = null;

while ((currentLine = in.readLine()) != null) {

sqlPromptLines = " "+sqlPromptLines + currentLine +"\n";
count ++;
System.out.println(count+" " + currentLine);
if(currentLine.equalsIgnoreCase("Function created.")){
sqlOutput = "\n" sqlOutput currentFunctionName + " " currentLine"\n" ;
break;
}

}// end while

in.close();
process.destroy();
}//end for
}//end if file exists


} catch (IOException e1) {


jTextArea1.setText(e1.getMessage());

}

System.out.println("Script Done");

jTextArea1.append(sqlOutput);
}// end id Connection is not null
}
Above code working appropriate on Windows but not on Linux.

is there any changes needed ?

Regards,
Ajay

Edited by: Ajay Sharma on Nov 21, 2012 6:43 PM                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points