Dan*_*cco 7 java postgresql jdbc
我正在使用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)
);
Run Code Online (Sandbox Code Playgroud)
该计划类似于:
//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 " +
}
Run Code Online (Sandbox Code Playgroud)
这是事实.
使用驱动程序postgresql-9.0-801.jdbc4.jar它在PostgreSQL 8.4和PostgreSQL 9中都能很好地工作
使用驱动程序8.4-701.jdbc4仅适用于PostgreSQL 8.4.
我可以升级驱动程序,这没问题.我担心的是:PostgreSQL 9中不再支持的通信协议内部发生了哪些变化?
a_h*_*ame 10
字节数组的编码(服务器发送它们的方式)已从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
| 归档时间: |
|
| 查看次数: |
4720 次 |
| 最近记录: |