Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

executing scripts

 
shilpa reddy
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,
I am trying to execute a SQL script to create some tables from my java app.
I am trying to use Runtime.
String[] cmd = new String[2];
cmd[0] ="sqlplus.exe" ;
cmd[1] = "abc.sql" ;
Process proc = Runtime.getRuntime().exec(cmd);
This spawns the SQLPlus window, but how do I run the script ?
Can this be done or do need to hardcode the create statements in my code ?
Thanks in advance.
Shilpa
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can the script be fed as a parameter to the command?
 
shilpa reddy
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How would you do this ?
Obliged if you would put down some code.
Thanks,
Shilpa
 
Peter Tran
Bartender
Posts: 783
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can feed the script in as a command line parameter. You also need to feed in the password and user id. Look up the SQL*PLUS command line parameter and it should tell you. I think it's a roundabout way to do things via JAVA. Why don't you run the script outside JAVA rather than forcing it inside the VM and executing an exec() which makes your code platform DEPENDENT.
-Peter
 
shilpa reddy
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
The situation is to create a set of tables which the user can start off,for instance by clicking a button.
My take was to execute a set of .sql files sequentially which would contain the scripts to to this.
How would I do this outside the VM ?
And where can I look for the command line for SQL*Plus ?
Thanks,
Shilpa.
 
Peter Tran
Bartender
Posts: 783
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A better approach is to do it all via JDBC. You can pass in the SQL commands via a properties file or XML configuration file. XML is probably a better mechanism in the long run, since it gives you the most flexibility.
SQL*PLUS commands can be found somewhere on the ORACLE website under documentation.
Executing a script outside the VM is not a recommend practice, since it ties you to a particular platform. What would happen if you tried to execute SQL*PLUS and it wasn't pathed correctly? Then you wouldn't have any tables created.
I would put in the work and make it all work inside your application.
my 2c.
-Peter
 
shilpa reddy
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Peter.
 
Ambrose Tati
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Peter,
Could you give me more info on how to use properties file and XML
configuration file?
Thanks
Ambrose
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic