使用蚂蚁进行 JDBC COPY [英] JDBC COPY with ant
问题描述
我有一个包含 Spring、Hibernate 和 PostgreSQL 的项目,必须使用 ANT 来创建包含数据的模式:
I have a project with Spring, Hibernate and PostgreSQL and have to use ANT to create schema with data:
<sql driver="org.postgresql.Driver"
classpath="src/main/webapp/WEB-INF/lib/postgresql-9.1-901.jdbc4.jar"
url="jdbc:postgresql://localhost:5433/postgres"
userid="postgres"
password="pw123"
autocommit="true"
src="src/main/sql/dbbackup.sql">
</sql>
但我收到此错误:
C:\Users\<user>\<workspace>\<Project>\antdb.xml:22: org.postgresql.util.PSQLException: ERROR: COPY from stdin failed: The JDBC driver currently does not support COPY operations.
不知道我们是否可以在这里使用 postgresql.copy 类?
Don't know if somehow we could use postgresql.copy class here?
推荐答案
PgJDBC 不直接支持 COPY
,但它通过 CopyManager
API 支持,你可以从PgJDBC 返回的java.sql.Connection
的PGConnection
接口.
PgJDBC doesn't support COPY
directly, but it does via the CopyManager
API you can get from the PGConnection
interface of the java.sql.Connection
returned by PgJDBC.
不幸的是,您不能在将 COPY
操作与其他命令混合在一起的纯 SQL 文件中使用它.
Unfortunately, you can't use that from a plain SQL file where you mix COPY
operations in with other commands.
就个人而言,我会使用 Ant <exec>
任务使用 psql
来运行 .sql
文件.这样您就可以在 SQL 文件中内嵌COPY
数据.
Personally, I'd shell out to psql
to run .sql
files using the Ant <exec>
task. That way you can include COPY
data in-line in your SQL files.
启用 PgJDBC 来处理 COPY
会很好,但这并不容易.它实际上是 PostgreSQL 中一种不同的协议模式,对于它使用带有准备好的语句、执行等的常用 JDBC 接口没有多大意义.我们可以在自定义的 PGconnection
上提供一个 execSQLScript
但这对你没有多大帮助,因为像 Ant 的 <sql>
任务不会使用它.您必须编写自定义任务.
It'd be nice to enable PgJDBC to handle COPY
, but it's not easy. It's effectively a different protocol mode in PostgreSQL, and it doesn't make much sense to use the usual JDBC interfaces with prepared statements, execute, etc, for it. We could provide an execSQLScript
on the custom PGconnection
but that wouldn't help you out much because things like Ant's <sql>
task wouldn't use it. You'd have to write a custom task.
相反,PgJDBC 不得不对客户端撒谎——当它在 COPY
命令后进入 COPY
模式时,它必须忽略 JDBC 规范而不是真正做它应该做的以响应 JDBC 语句的执行.这很可能会破坏各种事情.
Instead, PgJDBC would have to pretty much lie to clients - when it entered COPY
mode after a COPY
command, it'd have to ignore the JDBC spec and not really do what it was supposed to in response to JDBC statement executes. This would be likely to break all sorts of things.
所以 - 到目前为止,最简单的选择是执行 psql
命令来做你想做的事.
So - for now, by far the easiest option is to just exec the psql
command to do what you want.
这篇关于使用蚂蚁进行 JDBC COPY的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!