my dog learned polymorphism*
The moose likes JDBC and the fly likes how to execute .sql file using java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "how to execute .sql file using java" Watch "how to execute .sql file using java" New topic
Author

how to execute .sql file using java

kiran manohar
Ranch Hand

Joined: Sep 28, 2003
Posts: 49
hi all,
i am using sql server and oracle. Through java program i want to execute
.sql script file which has its own set of commands. i want to know how to
execute that the sql file.
Thanks in advance
Kiran.
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
I see two ways of doing this.
1) Execute the .sql file using Oracle's command line client (if there is one, that is) and the Runtime.exec() method.
2) Execute the individual statements within the .sql file by reading the .sql file and executing the pieces of SQL using java.sql.Statement. You'll have to do a bit parsing here (for example, semi-colons should be omitted and COMMIT calls should not be executed using Statement).


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
Jamie Robertson
Ranch Hand

Joined: Jul 09, 2001
Posts: 1879

Originally posted by Lasse Koskela:
I see two ways of doing this.
1) Execute the .sql file using Oracle's command line client (if there is one, that is) and the Runtime.exec() method.
2) Execute the individual statements within the .sql file by reading the .sql file and executing the pieces of SQL using java.sql.Statement. You'll have to do a bit parsing here (for example, semi-colons should be omitted and COMMIT calls should not be executed using Statement).

I agree with Lasse that you should parse the .SQL file. Just beware of any semi-colons in your data that might cause it to think that it is a delimiter, instead of data. But that should be fairly easy to ensure, since everything is self contained ( not dynamic data ) within the file. The easiest way might be to read the file in to a String and use the String.split(";") method to break up the individual statements. Then loop the resulting String array, executing each individual sql string.
Jamie
Jamie Robertson
Ranch Hand

Joined: Jul 09, 2001
Posts: 1879

Originally posted by Lasse Koskela:
I see two ways of doing this.
1) Execute the .sql file using Oracle's command line client (if there is one, that is) and the Runtime.exec() method.
2) Execute the individual statements within the .sql file by reading the .sql file and executing the pieces of SQL using java.sql.Statement. You'll have to do a bit parsing here (for example, semi-colons should be omitted and COMMIT calls should not be executed using Statement).

I agree with Lasse that you should parse the .SQL file. Just beware of any semi-colons in your data that might cause it to think that it is a delimiter, instead of data. But that should be fairly easy to ensure, since everything is self contained ( not dynamic data ) within the file. The easiest way might be to read the file in to a String and use the String.split(";") method to break up the individual statements. Then loop the resulting String array, executing each individual sql string.
Jamie
kiran manohar
Ranch Hand

Joined: Sep 28, 2003
Posts: 49
hi,
Thanks for responding my question.
i will try.
Regards
kiran
Francois Le Droff
Greenhorn

Joined: Dec 03, 2004
Posts: 3
you should also have a look at:
http://sqljc.sourceforge.net/


sqljc is a java program to connect through JDBC to any database engine (with JDBC support) to make simple queries and to execute sql files.

sqljc can execute commands defined by user. These commands are aliases to sqljc internal commands. User can define these aliases in the configuration file. An internal parser provides a simple mechanism to substitute variables in the command; those variables are passed at command invocation time (at execution time).

sqljc can execute SQL-ANSI sql files.


Cheers!
Fran´┐Żois

www.droff.com


Fran´┐Żois<br /><a href="http://www.droff.com" target="_blank" rel="nofollow">http://www.droff.com</a>
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to execute .sql file using java
 
Similar Threads
how to use .sql files using java
Running .sql File in java
create tables using java code by fetching from .sql file
how to convert .csv files into sql server files using java
Execute .sql file using java