带有ant的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来支持,您可以从java.sql.Connection
的PGConnection
接口获得该API.由PgJDBC返回.
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接口与准备好的语句,execute等配合使用并没有多大意义.我们可以在自定义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.
这篇关于带有ant的JDBC COPY的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!