File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes General Computing and the fly likes Running Sql script file through java programme. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » General Computing
Bookmark "Running Sql script file through java programme." Watch "Running Sql script file through java programme." New topic
Author

Running Sql script file through java programme.

Chaitanya ekre
Greenhorn

Joined: Jan 02, 2013
Posts: 12

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;
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3610
    
  60

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 ).
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Running Sql script file through java programme.