Thread: COPY problem in JDBC driver
Hi,
I apologise in advance if this has been solved but I haven't found any reference to it online.
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?
Thanks
Michael
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