在执行COPY..TO语句时无法确定参数$ 1的数据类型

时间:2016-09-06 06:17:10

标签: postgresql jdbc

我想使用JDBC预编译语句将数据库中的表内容写入CSV文件。我正在使用的PSQL查询是:

$("#ReportToEmployeeName").select2({
        placeholder: "Select Report To",
        ajax: {
            url: 'myurl ',
            dataType: 'json',
            delay: 250,
            data: function (params) {
                console.log(params);
                return {
                    term: params.term, // search term
                    page: params.page //Undefined error here
                    //page: 1 // do worked, but then pagination will not work
                };
            },
            processResults: function(data, params) {
                params.page = params.page || 1;

                return {
                    results: data.Results,
                    pagination: {
                        more: (params.page * pageSize) < data.Total
                    }
                };
            },
            cache: true
        },
        //escapeMarkup: function (markup) { return markup; }, // let our custom formatter work
        minimumInputLength: 1,
        allowClear: true,
        //templateResult: formatRepo, // omitted for brevity, see the source of this page
        //templateSelection: formatRepoSelection // omitted for brevity, see the source of this page
    });

我的代码如下:

public <T extends ClassA, P extends ClassB> T setSomething(T c, P ap) {     
            if (c instanceof ChildA1){
                if(ap instanceof ChildB1){  
                //getters and setters here
            c.setValue(ap.getIntValue());
             c.setId(ap.getId());
   }
   }
else if (c instanceof ChildA2){
            if(ap instanceof CHildB2){
//getters and setters here
       c.setValue(ap.getIntValue());
       c.setId(ap.getId());
}
}

运行此查询时,我得到以下堆栈:

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();
}

它不能与COPY..TO声明合作的原因是什么?

1 个答案:

答案 0 :(得分:2)

您无法参数化COPY语句。你必须使用字符串插值或PgJDBC的CopyManager