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 how to use .sql files using java 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 "how to use .sql files using java" Watch "how to use .sql files using java" New topic

how to use .sql files using java

prasanna pati
Ranch Hand

Joined: Jan 29, 2005
Posts: 46
I am starting a new project in which I will use database a lot . Now to begin with , I have to make some .sql files . I want to know what will be in these .sql files ? only sql queries for table creating/accessing or anything else . And MOST IMPORTANT IS : FROM A JAVA PROGRAM HOW I WILL USE THE .SQL FILES .

any sample coding will be highly appreciated . At the bigging of the project I only have the database table's blueprint .


[edited subject to remove shouting, was: HOW TO USE .SQL FILES USING JAVA]
[ June 24, 2006: Message edited by: Jeanne Boyarsky ]
Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 33119

The first thing is to think about what you want in the SQL files. I think it is a good idea to have different files for different things.

For example, you could have a SQL file for the ddl/initial setup. Then you could have a property file for common queries. It is easier to use a property file if you only want to run one query at a time.


Note that all caps is usually viewed as shouting on the Internet - probably not what you were going for.

The Java program would read in the SQL files, parse them to separate statements (probably on the semicolon delimitter) and then pass them along to the database using JDBC.

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
Rushikesh Wagh
Ranch Hand

Joined: Jun 18, 2005
Posts: 58
Dear Prasanna,
I have created A .bat file (Or say .sh file for unix)
which consists following sample code..

plus80 /nolog @./Queries/GenReport.SQL SCOTT/TIGER@MYDB

And I Called this batch/sh file from Java Program like..

Process p = Runtime.getRuntime().exec("abc.bat");

It Works Great as I have to use this for diffrent users. Just create batch file thats it..

If any one found better solution this please tell.

Best Regards,
Rushikesh Wagh
SCJP 1.4 (2005)
SCJP 1.6 (2010)
Shailesh Chandra
Ranch Hand

Joined: Aug 13, 2004
Posts: 1082

I would not actually recommend to execute .sql file from java using runtime process.

because by this way we can execute a batch file , but we can not handle the Sql Exceptions occurred during execution of .sql file.


Gravitation cannot be held responsible for people falling in love ~ Albert Einstein
Han Dian

Joined: Jun 26, 2006
Posts: 2
you can use IO to read the *.sql files and execute
example code:
BufferedReader in = new BufferedReader(new FileReader("./pro_sql/MSS_PM_createOnePoTable_Proc.sql"));
String str;
StringBuffer sb = new StringBuffer();
while ((str = in.readLine()) != null) {
sb.append(str+"\n ");
I agree. Here's the link:
subject: how to use .sql files using java
It's not a secret anymore!