• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Executing .sql file in Java

 
Shrikant Raut
Greenhorn
Posts: 7
Java ME Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I am having a .sql file containing queries to create and populate tables, what is the way to execute that .sql file using java.

 
Les Morgan
Rancher
Posts: 710
17
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe that the .sql file is a text file with an extension that is bound to your SQL tool. If that is your case, then all you need do is read the file, put the sql statement into a variable, then build a statement from that variable, then execute it. Pretty much the standard stuff except the reading the text of the sql statement in before creating the statement.
 
Campbell Ritchie
Sheriff
Posts: 51344
86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the file really contains SQL, you would do well to go through the Jva™ Tutorials, which has a nice introduction to databases.
 
Shrikant Raut
Greenhorn
Posts: 7
Java ME Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey,

"Campbell Ritchie" and "Les Morgan" thank you for the reply, but the .sql file is a sql dump file that contains queries for building tables as you said. I am trying to execute those queries directly from that file, so is there any other method or java syntax to do so.

Any help will really be appretiable.
 
Les Morgan
Rancher
Posts: 710
17
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd look into Runtime exec() to execute your SQL product using your .sql file as a parameter for your SQL product. I've not had to do that in a long time, so I will leave it to you to look up the syntax and appropriateness for your need. There are several examples on the web on how to do it. If you are using Windows, you may be fortunate enough to be able to call the .sql in the exec() and have the binding pick up the default program to execute for it, but that I would not count on.

Shrikant Raut wrote:Hey,

"Campbell Ritchie" and "Les Morgan" thank you for the reply, but the .sql file is a sql dump file that contains queries for building tables as you said. I am trying to execute those queries directly from that file, so is there any other method or java syntax to do so.

Any help will really be appretiable.
 
Rob Spoor
Sheriff
Pie
Posts: 20745
68
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Les Morgan wrote:If you are using Windows, you may be fortunate enough to be able to call the .sql in the exec() and have the binding pick up the default program to execute for it, but that I would not count on.

On Windows, you will need to run the file as argument to rundll32.exe: rundll32.exe url.dll,FileProtocolHandler <file>. Better yet is to use java.awt.Desktop and its open method (after first checking that the Desktop is available and the open action is available).
 
Al Zed
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Read the file consider the terminator you ar using (either ; or I don't know) and execute each statement using your driver.
Take care of triggers which end with "END ;"
If it's just simple DDL like tables, indexes and so on, it should be very fast.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic