Posted By

arunpjohny on 05/21/11


Tagged

postgres


Versions (?)

Postgres: How to insert 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. // Create a new large object
  7. int oid = lobj.create(LargeObjectManager.READ | LargeObjectManager.WRITE);
  8.  
  9. // Open the large object for writing
  10. LargeObject obj = lobj.open(oid, LargeObjectManager.WRITE);
  11.  
  12. // Now open the file
  13. File file = new File("myimage.gif");
  14.  
  15. // Copy the data from the file to the large object
  16. byte buf[] = new byte[2048];
  17. int s, tl = 0;
  18. while ((s = fis.read(buf, 0, 2048)) > 0) {
  19. obj.write(buf, 0, s);
  20. tl += s;
  21. }
  22.  
  23. // Close the large object
  24. obj.close();
  25.  
  26.  
  27. //later
  28. ps.setInt(<index>, oid);
  29.  
  30.  
  31.  
  32. conn.commit();
  33. conn.close();

Report this snippet  

You need to login to post a comment.