This week's book giveaway is in the Android forum.
We're giving away four copies of Head First Android and have Dawn & David Griffiths on-line!
See this thread for details.
The moose likes JDBC and Relational Databases and the fly likes can i execute sql script directly by java? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Head First Android this week in the Android forum!
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "can i execute sql script directly by java?" Watch "can i execute sql script directly by java?" New topic

can i execute sql script directly by java?

Sylven Yip
Ranch Hand

Joined: Aug 30, 2007
Posts: 42
hi everyone.
now I have a sql script file generate by MySQL.
is it necessary to read the sql script file(*.sql) by I/O and analyse by my program and then execute them?
thanks for help~
Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 32319

No. You can use Runtime.exec and have it run the sql script at a command line. This is the same as using your database's command line tool to run a script.

[OCA 8 book] [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
Mladen Grabowsky

Joined: Sep 19, 2007
Posts: 29
You could use iBatis' ScriptRunner, it was made for that purpose:
Sylven Yip
Ranch Hand

Joined: Aug 30, 2007
Posts: 42
Anil Bansal

Joined: Jun 18, 2009
Posts: 4

Hi All,
I also wanted to execute an sql script through java but had one more requirement. I wanted to ensure that the password that i am using to connect to the database as well as the arguments to the script are not visible through ps commands.
Can you suggest me a way in which i can redirect the password as well as the arguments through a file using java.
something like:
runtime.exec(sqlplus system@db @some.sql <tmp)
where tmp is the temporary file and has the password to connect to the db as well as the arguments required by the sql file.
This is not working in java through runtime command but the same kind of thing works in a shell script.

Is there any other better way of achieving the same.

Jan Cumps

Joined: Dec 20, 2006
Posts: 2541

Hi Anil,
can you start a new topic?
This one is 2 years old.

OCUP UML fundamental and ITIL foundation
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42956
No need to start a new topic; it already exists: executing an sql through java
I agree. Here's the link:
subject: can i execute sql script directly by java?
It's not a secret anymore!