File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC and Relational Databases and the fly likes executing .sql file from java program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "executing .sql file from java program" Watch "executing .sql file from java program" New topic

executing .sql file from java program

S Srikanth

Joined: Sep 09, 2002
Posts: 14
I have a .sql file which consists of some DDL Commands.
I need code snippet to execute the .sql file from java?

Shailesh Chandra
Ranch Hand

Joined: Aug 13, 2004
Posts: 1082

you can not execute a sql file straight to your database from jdbc.


Please look at JavaRanch Naming Policy and change you display name.As per policy a display name is first name and last name saperated by a space. you can do the same here


Gravitation cannot be held responsible for people falling in love ~ Albert Einstein
S Srikanth

Joined: Sep 09, 2002
Posts: 14
Is there any alternative to this?

Paul Sturrock

Joined: Apr 14, 2004
Posts: 10336

You can:
  • Read the file in with
  • Parse its contents - you'll probably need java.util.regex for that
  • Create a batch of Statements to execute via JDBC.

  • Sounds like a lot of work. Given this is DDL rather than DML, can't you just execute it via whatever DB client you would normally use. Far easier.

    JavaRanch FAQ HowToAskQuestionsOnJavaRanch
    David O'Meara

    Joined: Mar 06, 2001
    Posts: 13459

    thank you for changing your diaply name, but unfortunately we need: your first name, a space, then your last name.

    Please edit your profile and correct your display name.

    I agree. Here's the link:
    subject: executing .sql file from java program
    It's not a secret anymore!