Postgres: How to read oid value using JDBC API


/ Published in: Java
Save to your folder(s)



Copy this code and paste it in your 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();

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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.