【发布时间】:2011-01-28 12:35:13
【问题描述】:
我正在运行带有 PostgreSQL 9.0 的 Mac OSX 10.6。我编写了一个简单的 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 不再支持的通信协议内部发生了什么变化?
【问题讨论】:
标签: java postgresql jdbc