Postgresql中有2种数据类型可以存储大对象:bytea和oid两种。而利用bytea操作时候也有2种不同的方法
方法一:在JAVA中利用byte[]读取bytea类型(image为bytea类型)
byte[] imgBytes = rs.getBytes("image");
Image image = Toolkit.getDefaultToolkit().createImage(imgBytes);
方法二:在JAVA中利用InputStream读取bytea类型(image为bytea类型)
InputStream stream = rs.getBinaryStream("image");
Image image = ImageIO.read(stream);
方法三:利用oid类型(picture 为oid类型)
conn.setAutoCommit(false); LargeObjectManager lobj = ((org.postgresql.PGConnection) conn) .getLargeObjectAPI(); PreparedStatement ps = conn .prepareStatement("SELECT picture FROM resume WHERE /"NAME/"=?"); ps.setString(1,"myimage.gif"); ResultSet rs = ps.executeQuery(); if (rs != null) { while (rs.next()) { // 打开大对象读 int oid = rs.getInt(1); LargeObject obj = lobj.open(oid,LargeObjectManager.READ); // 读取数据 byte buf[] = new byte[obj.size()]; obj.read(buf,obj.size()); obj.close(); } rs.close(); } ps.close(); conn.commit();