What has changed between postgres jdbc 8.4 and 9 regarding bytearrays?
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 开发者_C百科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 .
- Using the driver 8.4-701.jdbc4 with PostgreSQL 9 doesn't work. The extracted file is different. An md5 shows that the content in the database is equals to the original file. Therefore, my assumption is that the problem is during the extraction of the file.
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
精彩评论