• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Running Sql script file through java programme.

 
Greenhorn
Posts: 13
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I want to run a sql script file (.sql) file from my application written in java.
Here is what I've done ----
I get the file content in the form of String.
I eliminate the comments from the String.
Commands are separated by / in script file so i split the string with / and then execute each command line by line.

This solution works for the script file i have now but i doubt that it will work for every file.
So my question is is there any other way for this ??? or any suggestions on this process.

Thanks in Advance;
 
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It depends on how you define "sql script file".

For example, Oracle's dialect of sql script file has several extensions that are handled by the script interpreter, not by database (such as spooling, defining variables and so on). Oracle furthermore has several command separators (the forward slash and a semicolon) that must be handled correctly, and even has commands that allow these separators to be overridden. In short, to be able to run any sql script file that can be processed by Oracle's Sql*plus, you'd have to fully replicate all these capabilities. That would be a lot of work.

Other databases and/or sql clients can have their own extensions.

There are generally two ways to go:

1) Define your own "sql script file": make it clear which commands are supported and which aren't. Implement your application to conform to the specification. Then you're able to tell yourself which files will work and which won't. (Of course you can use an existing specification, for example Oracle's I've already mentioned, but it will probably be a lot of work.)

2) Let the hard work of parsing and executing the script be done by a call to the SQL client, in a way similar to running Windows' BAT file. Most SQL clients should support passing the connection information and a name of the SQL file to execute on the command line.

Then there is a third option: let your users use the SQL client directly (mentioning just for completeness ).
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic