Re: COPY problem in JDBC driver

From: "Albe Laurenz" <laurenz(dot)albe(at)wien(dot)gv(dot)at>
To: "Michael Epstein *EXTERN*" <michael(dot)epstein(at)mac(dot)com>, <pgsql-jdbc(at)postgresql(dot)org>
Subject: Re: COPY problem in JDBC driver
Date: 2010-02-24 08:47:22
Message-ID: D960CB61B694CF459DCFB4B0128514C20393819B@exadv11.host.magwien.gv.at
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-jdbc

Michael Epstein wrote:
> I am using the COPY implementation in the latest
> (postgresql-8.4-701.jdbc4) driver with Java VM 1.6.0 (Mac OSX)
>
> My code is the following (just trying to insert a string from
> a file into a single column):
>
> public static void main(String[] args) {
> try {
> Class.forName("org.postgresql.Driver");
> Connection connection =
> DriverManager.getConnection("jdbc:postgresql://127.0.0.1:5432/
> xseq_zip","postgres", "postgres");
> PreparedStatement statement =
> connection.prepareStatement("DELETE FROM jdbctest");
> statement.execute();
> System.out.println("Erased Db");
> System.out.println(connection.toString());
>
>
> PGConnection pConn = (PGConnection)connection;
> CopyManager copyManager = pConn.getCopyAPI();
> String columns = "(name)";
> copyManager.copyIn("COPY jdbctest " + columns + " FROM '" +
> "/Users/foobar/Documents/bulk.txt" + '");
> }
>
> catch (Exception ex){
> ex.printStackTrace();
> }
> }
>
> The code inserts the value from the file into the db but
> throws the following exception:
>
>
> org.postgresql.util.PSQLException: Received CommandComplete 'COPY 1' without an active copy operation
> at org.postgresql.core.v3.QueryExecutorImpl.processCopyResults(QueryExecutorImpl.java:918)
> at org.postgresql.core.v3.QueryExecutorImpl.startCopy(QueryExecutorImpl.java:713)
> at org.postgresql.copy.CopyManager.copyIn(CopyManager.java:52)
> at tests.JDBCTest.main(JDBCTest.java:33)
>
> Has anyone noticed this before? Am I doing something wrong?

Your COPY statement is an SQL statement that will not need the CopyManager.
It will read the data from a file on the server.
Just run it like a normal statement.
You'll need superuser privileges though.

I didn't look at it, but I guess that CopyManager is only necessary
if you want to load from or export to files on the client side,
which amounts to a COPY ... TO/FROM STDIN in SQL.

Yours,
Laurenz Albe

In response to

Browse pgsql-jdbc by date

  From Date Subject
Next Message niromon 2010-02-24 17:00:12 How to change the date in a postgres database via JDBC
Previous Message Maciek Sakrejda 2010-02-24 07:12:14 synchronization problems with COPY on connection failure