2016-09-06 1 views
2

JDBCプリペアドステートメントを使用して、データベースからCSVファイルにテーブルの内容を書きたいとします。私が使用していますPSQLクエリは次のとおりです。

COPY(select * from file where uploaded_at > ?) TO '/tmp/file_info.csv' With DELIMITER ',' CSV HEADER "; 

私のコードは次のとおりです。

private void copyData(Connection conn, Date date){ 
String sql = "COPY (select * from file where uploaded_at< ?) TO '/tmp/file_info.csv' With DELIMITER ',' CSV HEADER "; 
PreparedStatement stmt = conn.prepareStatement(sql); 
stmt.setTimestamp(1, new Timestamp(date.getTime())); 
stmt.execute(); 
} 

私はこのクエリを実行すると、私は次のスタックを取得する:

org.postgresql.util.PSQLException: ERROR: could not determine data type of parameter $1 
    at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2103) 
    at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1836) 
    at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:257) 
    at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:512) 
    at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:388) 
    at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:381) 
    at tests.com.paramatrix.pace.archival.file.TestFileArchival.main(TestFileArchival.java:63) 

この例外はあります次のような単純な選択クエリを実行しているときには生成されません。

COPY..TOステートメントでは機能しない理由は何でしょうか?

答えて

2

COPYステートメントをパラメータ化することはできません。文字列補間やPgJDBCのCopyManagerを使用する必要があります。

関連する問題