postgres jdbc 8.4 和 9 之间关于字节数组有什么变化?
我正在运行 Mac OSX 10.6 和 PostgreSQL 9.0。我编写了一个简单的 Java 应用程序,它将图像插入到 bytea 字段中,然后查询同一字段来检查它。
表格:
CREATE TABLE test.test_table
(
id integer NOT NULL,
image bytea,
CONSTRAINT test_table_pkey PRIMARY KEY (id)
);
程序类似于:
//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 " +
}
以下是事实。
使用驱动程序 postgresql-9.0-801.jdbc4.jar 就可以了 完美地工作在 PostgreSQL 8.4 和 PostgreSQL 9
使用驱动程序 8.4-701.jdbc4 有效 仅在 PostgreSQL 8.4 中。
- 使用 带有 PostgreSQL 的驱动程序 8.4-701.jdbc4 9 不行。提取的文件不同。 md5表明数据库中的内容与原始文件相同。因此,我的假设是问题出在提取文件期间。
我可以升级驱动程序,没问题。我关心的是:PostgreSQL 9 不再支持的通信协议内部发生了什么变化?
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 9Using 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 ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
字节数组的编码(服务器发送它们的方式)已从 8.4 更改为 9.0:
请参阅发行说明:
http://www.postgresql.org/docs/9.0 /static/release-9-0.html#AEN99255
以及配置设置的详细说明:
http://www.postgresql.org /docs/9.0/static/runtime-config-client.html#GUC-BYTEA-OUTPUT
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