Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Executing .sql file in Java  RSS feed

 
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.

 
Rancher
Posts: 786
19
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.
 
Marshal
Posts: 60098
188
  • 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: 786
19
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.

 
Sheriff
Posts: 21452
96
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).
 
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!