wood burning stoves*
The moose likes JDBC 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


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "how to use .sql files using java" Watch "how to use .sql files using java" New topic
Author

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 .

HELP

[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
Marshal

Joined: May 26, 2003
Posts: 30789
    
157

Prasanna,
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.

MOST IMPORTANT IS : FROM A JAVA PROGRAM HOW I WILL USE THE .SQL FILES .

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.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, 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: 1081

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.

Shailesh


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

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 ");
}
in.close();
stmt.executeUpdate(sb.toString());
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to use .sql files using java