2012-03-02 77 views
1

我正在使用postgresql託管我的數據庫。在我的數據庫中,我有一個表,其結構如下所示xyz在JDBC中將portgresql表導出爲CSV的問題

id content folder 
1  hello  Inbox 
2  hi   Sent 

我想將此表導出到CSV使用我的Java程序。代碼段低於

Connection connection2 = new ServerDBConnect().getConnection(); 
PreparedStatement statement = connection2.prepareStatement("copy (SELECT * FROM xyz WHERE folder=?) to 'C:/export.csv' delimiter ','"); 
statement.setString(1, FOLDER_SELECTED); //Here, FOLDER_SELECTED=Inbox 
statement.execute(); 

當我執行這個代碼,我越來越SQLException

ERROR: there is no parameter $1 

如果我沒有("copy (SELECT * FROM xyz) to 'C:/export.csv' delimiter ','"))指定的文件夾執行代碼,代碼工作正常。

我在這裏做錯了什麼?如何解決這個問題?

注意:如果我直接在Postgresql SQL控制檯中執行查詢(copy (SELECT * FROM xyz WHERE folder='Inbox' ORDER BY time) to 'G:/export.csv' delimiter ','),我會得到所需的輸出。

請幫

+0

不知道如果這個問題與你有關:http://stackoverflow.com/questions/5726362/does-postgresql-jdbc-driver-has-copy – 2012-03-02 09:35:19

+0

@AndreaPolci:謝謝,但那個鏈接與我無關。我試圖將本地數據庫中的表格內容導出到本地系統 – 2012-03-02 09:42:23

+0

看起來與我相關。鏈接是一個問題,如何使用JDBC中的'copy'。 – skaffman 2012-03-02 11:49:55

回答

2

我終於找到了自己的答案。

查詢零錢給了我希望的結果

該查詢假設是這樣

Connection connection2 = new ServerDBConnect().getConnection(); 
PreparedStatement statement = connection2.prepareStatement("copy (SELECT * FROM xyz WHERE folder='" + FOLDER_SELECTED + "') to 'C:/export.csv' delimiter ','"); 

這是推動我瘋了,但最後還是做了:-)