Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Data Bases > Pgsql Sql > Re: export CSV ...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 1 Topic 3408 of 3558
Post > Topic >>

Re: export CSV file through Java JDBC

by public@[EMAIL PROTECTED] (Steve Midgley) Apr 15, 2008 at 10:13 AM

--=====================_2228046==.ALT
Content-Type: text/plain; charset="us-ascii"; format=flowed

At 07:20 AM 4/15/2008, you wrote:
>Date: Mon, 14 Apr 2008 09:41:41 -0400
>From: Emi Lu <emilu@[EMAIL PROTECTED]
>
>To: pgsql-sql@[EMAIL PROTECTED]
>Subject: export CSV file through Java JDBC
>Message-ID: <48035F15.4030302@[EMAIL PROTECTED]
>
>
>Good morning,
>
>Running the following command from command line is ok, but cannot 
>export
>a table into a csv file through java JDBC code.
>
>Please help!
>
>
>JAVA code:
>===================
>     public static void exec(String command)
>     {
>        try{
>           Process p   = Runtime.getRuntime().exec(command);
>           p.waitFor();
>           p.destroy();
>        }catch(Exception e) {
>           System.err.println("exec command Error:  " + 
> e.getMessage());
>        }
>     }
>
>
>
>SQL Command:
>=================
>psql -U username -d dbName -c  "\copy tableName to 'result.csv'  with 
>CSV "
>
>When call exec(commands);
>
>Nothing happens, result.csv was not created at all?
>
>Thanks a lot!

A couple of thoughts. First, you aren't passing the password in, so 
that seems like a problem. Of course, psql won't let you specify a 
password on the command line but last I looked you can set an ENV var 
before running psql: "PGPASSWORD=[your password here]"

Second, you don't specify a server/port, which means your Pg server is 
localhost:5432?

Third, you are not specifying a path to pgsql, so you have to be sure 
that it can be found in the path. Now this can be tricky: your Java 
application may be running in a context DIFFERENT from your command 
prompt. The user/env your Java app is running in will determine what 
path vars are available to it - it may not be able to find psql. Try 
running "which psql > /tmp/which.txt" in your code above and see what 
happens (assuming you're on a box with "which" installed).

Fourth (minor), you don't specify column names in your export which 
could result in variable results depending on the create statement - 
it's better to specify to guarantee the same results every time.

Fifth, try capturing STDERR and STDOUT, so that if psql or command 
shell generate errors you'll know what they are. Maybe Java gives you 
that in e.getMessage or maybe you need to put it in your psql command 
line.

I'm doing exactly the same thing you are doing but in Ruby/ActiveRecord 
so I know this works. It works for me on Windows and Linux, fwiw.

I don't know enough Java to know if the command you are running is the 
standard "shell execute" command in Java. If it's not, that's what you 
want so change your code that way. You just want java to shell out to 
the OS command processor. Be sure when you set your command shell env 
var, that this env var persists long enough so that when you run your 
psql command it's still in effect. For example this psuedo code might 
not work b/c two different child shells are run:

system.exec("export PGPASSWORD=pass1234");
system.exec("psql my command here");

I think you want something more like this psuedo code:

system.set_environment("PGPASSWORD")="pass1234";
system.exec("psql my command here");

I hope this helps,

Steve

--=====================_2228046==.ALT
Content-Type: text/html; charset="us-ascii"

<html>
<body>
At 07:20 AM 4/15/2008, you wrote:<br>
<blockquote type=cite class=cite cite="">Date: Mon, 14 Apr 2008 09:41:41
-0400<br>
From: Emi Lu &lt;emilu@[EMAIL PROTECTED]
>
To: pgsql-sql@[EMAIL PROTECTED]
>
Subject: export CSV file through Java JDBC<br>
Message-ID: &lt;48035F15.4030302@[EMAIL PROTECTED]
><br>
Good morning,<br><br>
Running the following command from command line is ok, but cannot export
<br>
a table into a csv file through java JDBC code.<br><br>
Please help!<br><br>
<br>
JAVA code:<br>
===================<br>
&nbsp;&nbsp;&nbsp; public static void exec(String command)<br>
&nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; try{<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Process
p&nbsp;&nbsp; = Runtime.getRuntime().exec(command);<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; p.waitFor();<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; p.destroy();<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }catch(Exception e) {<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
System.err.println(&quot;exec command Error:&nbsp; &quot; +
e.getMessage());<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; }<br><br>
<br><br>
SQL Command:<br>
=================<br>
psql -U username -d dbName -c&nbsp; &quot;\copy tableName to
'result.csv'&nbsp; with CSV &quot;<br><br>
When call exec(commands);<br><br>
Nothing happens, result.csv was not created at all?<br><br>
Thanks a lot!</blockquote><br>
A couple of thoughts. First, you aren't passing the password in, so that
seems like a problem. Of course, psql won't let you specify a password on
the command line but last I looked you can set an ENV var before running
psql: &quot;<font color="#2A00FF">PGPASSWORD=[your password
here]&quot;<br><br>
</font>Second, you don't specify a server/port, which means your Pg
server is localhost:5432?<br><br>
Third, you are not specifying a path to pgsql, so you have to be sure
that it can be found in the path. Now this can be tricky: your Java
application may be running in a context DIFFERENT from your command
prompt. The user/env your Java app is running in will determine what path
vars are available to it - it may not be able to find psql. Try running
&quot;which psql &gt; /tmp/which.txt&quot; in your code above and see
what happens (assuming you're on a box with &quot;which&quot;
installed).<br><br>
Fourth (minor), you don't specify column names in your export which could
result in variable results depending on the create statement - it's
better to specify to guarantee the same results every time.<br><br>
Fifth, try capturing STDERR and STDOUT, so that if psql or command shell
generate errors you'll know what they are. Maybe Java gives you that in
e.getMessage or maybe you need to put it in your psql command
line.<br><br>
I'm doing exactly the same thing you are doing but in Ruby/ActiveRecord
so I know this works. It works for me on Windows and Linux,
fwiw.<br><br>
I don't know enough Java to know if the command you are running is the
standard &quot;shell execute&quot; command in Java. If it's not, that's
what you want so change your code that way. You just want java to shell
out to the OS command processor. Be sure when you set your command shell
env var, that this env var persists long enough so that when you run your
psql command it's still in effect. For example this psuedo code might not
work b/c two different child shells are run:<br><br>
system.exec(&quot;export
<font color="#2A00FF">PGPASSWORD=pass1234&quot;);<br>
</font>system.exec(&quot;psql my command here&quot;);<br><br>
I think you want something more like this psuedo code:<br><br>
system.set_environment(&quot;PGPASSWORD&quot;)=&quot;pass1234&quot;;<br>
system.exec(&quot;psql my command here&quot;);<br><br>
I hope this helps,<br><br>
Steve<br>
</body>
</html>

--=====================_2228046==.ALT--




 1 Posts in Topic:
Re: export CSV file through Java JDBC
public@[EMAIL PROTECTED]   2008-04-15 10:13:46 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan13V112 Fri Jul 4 22:33:57 CDT 2008.