I'm running Mac OSX 10.6 with PostgreSQL 9.0. I wrote a simple Java application that insert an image in a bytea field and then query the same field to check it.
The table:
CREATE TABLE test.test_table
(
id integer NOT NULL,
image bytea,
CONSTRAINT test_table_pkey PRIMARY KEY (id)
);
The program is something like:
//insert the file
PreparedStatement ps = connection.prepareStatement("INSERT INTO test.test_table( id, image ) VALUES (?, ?);");
byte[] bytesFromFile = readFile("img/test1.bmp");
ps.setInt(1, 1);
ps.setBytes(2, bytesFromFile);
ps.execute();
ps.close();
PreparedStatement stmt = connection.prepareStatement("Select id,image from test.test_table");
ResultSet rs = stmt.executeQuery();
//Get the file from the BD and save it to the FS
while (rs.next()) {
String id = rs.getString(1);
InputStream imageStream = rs.getBinaryStream(2);
String imageName = OUTPUT_DIR + "/" + id + ".bmp";
FileOutputStream f = new FileOutputStream(imageName);
byte buff[] = new byte[1024];
int l;
while ((l = imageStream.read(buff)) > 0) {
f.write(buff, 0, l);
}
f.close();
System.out.println("CREATED : " + imageName);// + " size " +
}
Here are the facts.
Using the driver postgresql-9.0-801.jdbc4.jar it works perfectly both in PostgreSQL 8.4 and with PostgreSQL 9
Using the driver 8.4-701.jdbc4 works only in PostgreSQL 8.4 .
I can upgrade the driver, thats no problem. My concern is: what has changed inside the communication protocol that is no longer supported in PostgreSQL 9 ?
The encoding of byte arrays (the way the server sends them) has been changed from 8.4 to 9.0:
See the release notes:
http://www.postgresql.org/docs/9.0/static/release-9-0.html#AEN99255
and the description of the configuration setting for details:
http://www.postgresql.org/docs/9.0/static/runtime-config-client.html#GUC-BYTEA-OUTPUT
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With