2

I was trying to import CSV into PostgreSQL using JDBC. I'm using this command:

sql ="\\copy data1_1 from 'C:\\Users\\legolas\\Desktop\\data1.csv' DELIMITERS ',' CSV";

But nothing gets written to the table. I'm not getting any errors. When I use the same command in the psql shell everything works perfectly.

I'm working on windows.

A.H.
  • 63,967
  • 15
  • 92
  • 126
user414209
  • 361
  • 1
  • 5
  • 15
  • How do you execute the sql statement ? Do you get any error code (that does not imply to get an error message on screen) ? – Seki Jan 16 '12 at 15:24

1 Answers1

1

The command you are trying to send is start with \copy. Every "command" starting with \ is NOT an SQL command but something the psql shell handles by itself. Therefore you cannot \copy in JDBC.

You can use the SQL variant copy. But since copy is a PostgreSQL special you have to fiddle with some driver internals. As this question states, the entry point for that is the class CopyManager wich is "documented" here. In another question there is even a working example.

Community
  • 1
  • 1
A.H.
  • 63,967
  • 15
  • 92
  • 126