Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to use .sql files using java

 
prasanna pati
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34179
340
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rushikesh Wagh
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Shailesh Chandra
Ranch Hand
Posts: 1082
Java Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Han Dian
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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());
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic