Unix Technical Forum

How do we execute this command through java?

This is a discussion on How do we execute this command through java? within the DB2 forums, part of the Database Server Software category; --> technocrat wrote: > String line; > Process p = Runtime.getRuntime().exec("db2cmd /c /w /i db2 -tvf > /home/filename.txt >output"); > ...


Go Back   Unix Technical Forum > Database Server Software > DB2

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #11 (permalink)  
Old 02-27-2008, 06:51 AM
Gregor =?ISO-8859-2?Q?Kova=E8?=
 
Posts: n/a
Default Re: How do we execute this command through java?

technocrat wrote:

> String line;
> Process p = Runtime.getRuntime().exec("db2cmd /c /w /i db2 -tvf
> /home/filename.txt >output");
>
> BufferedReader input = new BufferedReader
> (new InputStreamReader(p.getInputStream()));
> while ((line = input.readLine()) != null) {
> System.out.println(line);
> }
> input.close();
> System.out.println("Output Print done");
>
>
> When I run this...I get an error saying...db2cmd not found...
> SQL4302N Procedure or user-defined function "CONTROL.INITTIERONE",
> specific
> name "SQL060424133114600" aborted with an exception "db2cmd: not
> found".
> SQLSTATE=38501
> What might be going wrong???

I have it working this way:
I've put
if [ -f /home/db2inst1/sqllib/db2profile ]; then
. /home/db2inst1/sqllib/db2profile
fi

into my .bash_profile.
Then run your program with:
Process p = Runtime.getRuntime().exec("/bin/sh -c db2cmd -c -w -i
db2 -t -f /home/filename.txt >output");

Best regards,
Kovi
--
-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
| Gregor Kovac | Gregor.Kovac@mikropis.si |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| In A World Without Fences Who Needs Gates? |
| Experience Linux. |
-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #12 (permalink)  
Old 02-27-2008, 06:51 AM
technocrat
 
Posts: n/a
Default Re: How do we execute this command through java?

Well Dave the thing I was planning to do was
I have a txt file which has all the insert commands to different tables
in a database. I can hardcode those insert statements as a simple sql
insert stattements using jdbc but there are almost 30-40 of them and
might change in future...so I was thinking of writing a JAVA stored
procedure which would call the "db2 -tvf/home./...filename" command to
read from that filename and populate the database...

This is the code...but if there is a better way to do the above
problem, kindly let me know
/**
* JDBC Stored Procedure CONTROL.InitTierOne
*/
package PKG60424012112828;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.sql.*; // JDBC classes

public class InitTierOne
{
public static void initTierOne () throws SQLException, Exception
{
// Get connection to the database
Connection con =
DriverManager.getConnection("jdbc:default:connecti on");
PreparedStatement stmt = null;
boolean bFlag;
String sql;
String line;
Process p = Runtime.getRuntime().exec("db2profile /c /w /i db2
-tvf ecd2061.txt >ecd2061");

BufferedReader input = new BufferedReader
(new InputStreamReader(p.getInputStream()));
while ((line = input.readLine()) != null) {
System.out.println(line);
}
input.close();
System.out.println("Output Print done");

}
}

And the text file from wehich it reads is as below

connect to DB
insert into table tablename (values);
commit;
connect reset;
quit

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #13 (permalink)  
Old 02-27-2008, 06:51 AM
Knut Stolze
 
Posts: n/a
Default Re: How do we execute this command through java?

technocrat wrote:

> Well Dave the thing I was planning to do was
> I have a txt file which has all the insert commands to different tables
> in a database. I can hardcode those insert statements as a simple sql
> insert stattements using jdbc but there are almost 30-40 of them and
> might change in future...so I was thinking of writing a JAVA stored
> procedure which would call the "db2 -tvf/home./...filename" command to
> read from that filename and populate the database...


In this case, I'd recommend that you write some (Java) code that reads the
file and executes the statements therein directly through JDBC. The thing
is that the spawning of another process inside an external routine is a not
supported configuration.

--
Knut Stolze
DB2 Information Integration Development
IBM Germany
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #14 (permalink)  
Old 02-27-2008, 06:52 AM
Dave Hughes
 
Posts: n/a
Default Re: How do we execute this command through java?

technocrat wrote:

> Well Dave the thing I was planning to do was
> I have a txt file which has all the insert commands to different
> tables in a database. I can hardcode those insert statements as a
> simple sql insert stattements using jdbc but there are almost 30-40
> of them and might change in future...so I was thinking of writing a
> JAVA stored procedure which would call the "db2
> -tvf/home./...filename" command to read from that filename and
> populate the database...
>
> This is the code...but if there is a better way to do the above
> problem, kindly let me know
> /**
> * JDBC Stored Procedure CONTROL.InitTierOne
> */
> package PKG60424012112828;
>
> import java.io.BufferedReader;
> import java.io.InputStreamReader;
> import java.sql.*; // JDBC classes
>
> public class InitTierOne
> {
> public static void initTierOne () throws SQLException, Exception
> {
> // Get connection to the database
> Connection con =
> DriverManager.getConnection("jdbc:default:connecti on");
> PreparedStatement stmt = null;
> boolean bFlag;
> String sql;
> String line;
> Process p = Runtime.getRuntime().exec("db2profile /c /w /i db2
> -tvf ecd2061.txt >ecd2061");
>
> BufferedReader input = new BufferedReader
> (new InputStreamReader(p.getInputStream()));
> while ((line = input.readLine()) != null) {
> System.out.println(line);
> }
> input.close();
> System.out.println("Output Print done");
>
> }
> }
>
> And the text file from wehich it reads is as below
>
> connect to DB
> insert into table tablename (values);
> commit;
> connect reset;
> quit


If all you're doing is inserting data into a variety of different
tables I'd suggest dispensing with SQL in the text file altogether:
store the data and the target table in some format (whatever you like,
be it CSV, XML, or whatever) and have your external routine read the
data file and run the necessary INSERT statements (saves all that
messing around with spawning external processes and the associated
portability problems).

One thing that still confuses me ... why does this have to be in an
external routine / stored procedure?

I'm guessing you want the SQL script to sit on the server (for security
reasons or some such?) and have the ability to execute a simple CALL
statement from a client, causing the server to execute the script. I'm
just wondering why using the CLP via SSH or something similar isn't
acceptable (assuming your server is UNIX/Linux)?

For example:

$ ssh dave@myserver "db2 -tvf myscript.sql"

seems just as easy (if not easier) than:

$ db2 CONNECT TO MYDB USER dave USING mypassword
$ db2 CALL MYPROC
$ db2 CONNECT RESET


Dave.
--

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #15 (permalink)  
Old 02-27-2008, 06:53 AM
technocrat
 
Posts: n/a
Default Re: How do we execute this command through java?

It really doesnt need to be in a stored procedure, the only reason I
have it there..is that I have a streod procedure to init the tier 3
tables (which I wrote earlier) so to init the tier one tables now I
started by default as a stor ed procedure thinking if i can execute
just db2 -tvf/filename command from some srt of a technique the problem
would be solved, but seems to be much more difficult than that. I like
the technique you mentioned...but I would rather do the inserts
manually by calling the command from CLP (earlier I was thinking of
automating this process which lead to all this mess)

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 09:01 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com