Posted By

arunpjohny on 05/21/11


Tagged

postgres


Versions (?)

Postgres: How to read oid value using JDBC API


 / Published in: Java
 

URL: http://www.postgresql.org/docs/7.4/static/jdbc-binary-data.html

  1. conn.setAutoCommit(false);
  2.  
  3. // Get the Large Object Manager to perform operations with
  4. LargeObjectManager lobj = ((org.postgresql.PGConnection)conn).getLargeObjectAPI();
  5.  
  6. PreparedStatement ps = con.prepareStatement("SELECT imgoid FROM imageslo WHERE imgname = ?");
  7. ps.setString(1, "myimage.gif");
  8. ResultSet rs = ps.executeQuery();
  9. if (rs != null) {
  10. while (rs.next()) {
  11. // Open the large object for reading
  12. int oid = rs.getInt(1);
  13. LargeObject obj = lobj.open(oid, LargeObjectManager.READ);
  14.  
  15. // Read the data
  16. byte buf[] = new byte[obj.size()];
  17. obj.read(buf, 0, obj.size());
  18. // Do something with the data read here
  19.  
  20. // Close the object
  21. obj.close();
  22. }
  23. rs.close();
  24. }
  25. ps.close();
  26.  
  27. conn.commit();

Report this snippet  

You need to login to post a comment.