JDBC COPY with ant

后端 未结 1 1836
遇见更好的自我
遇见更好的自我 2021-01-25 06:09

I have a project with Spring, Hibernate and PostgreSQL and have to use ANT to create schema with data:

        

        
相关标签:
1条回答
  • 2021-01-25 06:49

    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.

    Unfortunately, you can't use that from a plain SQL file where you mix COPY operations in with other commands.

    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.

    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.

    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.

    So - for now, by far the easiest option is to just exec the psql command to do what you want.

    0 讨论(0)
提交回复
热议问题