使用postgresql存储并读出 oid 类型图片应该如何设计数据库表和 程序?求给个例子
解决方案 »
- EXT的portal中怎么动态增加一列?
- spring到底是怎么生成bean实例的
- 求一个连接sql2005+s2sh的小项目,只要有登录或注册功能就OK了
- ArrayList get(i) 赋值问题 没有改变数据
- 跪求java使用ajax实现分页的方法!
- 一个像QQ一样的聊天工具
- 存储数据到数据库时出错
- hibernate删除错误--Illegal attempt to associate a collection with two open sessions
- Hibernatead问题 关于HQL 查询
- 初学使用Hibernate,报错“org/dom4j/DocumentException”始终无法解决,网上也没有类似解决方案!
- extjs 学习方法
- 都进来看看java问题
CREATE TABLE imageslo (imgname text, imgoid oid);To insert an image, you would use:// All LargeObject API calls must be within a transaction block
conn.setAutoCommit(false);// Get the Large Object Manager to perform operations with
LargeObjectManager lobj = ((org.postgresql.PGConnection)conn).getLargeObjectAPI();// Create a new large object
int oid = lobj.create(LargeObjectManager.READ | LargeObjectManager.WRITE);// Open the large object for writing
LargeObject obj = lobj.open(oid, LargeObjectManager.WRITE);// Now open the file
File file = new File("myimage.gif");
FileInputStream fis = new FileInputStream(file);// Copy the data from the file to the large object
byte buf[] = new byte[2048];
int s, tl = 0;
while ((s = fis.read(buf, 0, 2048)) > 0) {
obj.write(buf, 0, s);
tl += s;
}// Close the large object
obj.close();// Now insert the row into imageslo
PreparedStatement ps = conn.prepareStatement("INSERT INTO imageslo VALUES (?, ?)");
ps.setString(1, file.getName());
ps.setInt(2, oid);
ps.executeUpdate();
ps.close();
fis.close();// Finally, commit the transaction.
conn.commit();
// All LargeObject API calls must be within a transaction block
conn.setAutoCommit(false);
// Get the Large Object Manager to perform operations with
LargeObjectManager lobj = ((org.postgresql.PGConnection)conn).getLargeObjectAPI(); PreparedStatement ps = conn.prepareStatement("SELECT imgoid FROM imageslo WHERE imgname = ?");
ps.setString(1, "myimage.gif");
ResultSet rs = ps.executeQuery();
while (rs.next()) {
// Open the large object for reading
int oid = rs.getInt(1);
LargeObject obj = lobj.open(oid, LargeObjectManager.READ);
// Read the data
byte buf[] = new byte[obj.size()];
obj.read(buf, 0, obj.size());
// Do something with the data read here
// Close the object
obj.close();
}
rs.close();
ps.close();
// Finally, commit the transaction.
conn.commit();