This week's book giveaway is in the Programmer Certification forum.
We're giving away four copies of OCP Oracle Certified Professional Java SE 21 Developer Study Guide: Exam 1Z0-830 and have Jeanne Boyarsky & Scott Selikoff on-line!
See this thread for details.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

BLOB insertion

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried to insert a LOB - a 4000 long String converted to bytearray into a BLOB column of Oracle.
code is like this:
conn.setAutoCommit(false);
qry = "select blobby from temp for update";
pstt = conn.prepareStatement(qry);
ResultSet rs = pstt.executeQuery();
rs.next();
oracle.sql.BLOB BL1 =
(oracle.sql.BLOB) rs.getBlob(1);
java.io.OutputStream opStream =
BL1.getBinaryOutputStream();
for(int i=0; i<bytearray.length;i++)
{
opStream.write(bytearray[i]);
}
// I tried this too :
// opStream.write(bytearray);
qry = "insert into temp(blobby)values(?)";
pstt = conn.prepareStatement(qry);
pstt.setBlob(1,BL1);
The String is like : "ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~⌂ ......"
But when BL1 was checked(using getBytes()) the value is like :
" T ☺☺♀ ☺ ☺ ☻☻D ?u ?t ☺ ☺ ←?tr♂?tr♂ ☺♂0? ....."
Whatever the original length the BL1 length is 86
What is the correct way of inerting a LOB into a BLOB column ?
Thanks
 
Ranch Hand
Posts: 1179
Mac OS X Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try this post: https://coderanch.com/t/298572/JDBC/java/Inserting-BLOB-database
Rene
 
Surendran Velath
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rene,
Thanks for the reply.
What I have done in the code is exactly what you had suggested in reply to chinmay.
Do you think that there is something wrong with:
opStream.write(bytearray);
Or is it that the BLOB object has the correct value inside it and I am trying to read it in the wrong way with:
byte[] bb = blobObj.getBytes();
for(int i=0; i<bb.length; i++)
{
System.out.print(bb[i]);
}
Please suggest a way out .....
Surendran
 
Rene Larsen
Ranch Hand
Posts: 1179
Mac OS X Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to close the stream after insert.


Rene
 
Surendran Velath
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rene,
Thanks for your help.
This is the final solution which worked perfectly:
To write Blob:
conn.setAutoCommit(false);
String qry = "";
qry = "insert into temp values(empty_blob())�;
Statement stmt = connection.createStatement();
stmt.execute(qry); // insert a empty blob
stmt.close();
qry = "select blobby from temp for update";
pstt = conn.prepareStatement(qry);
ResultSet rs = pstt.executeQuery(); // write into that empty blob
rs.next();// go to last row if more than one row
Blob b = rs.getBlob(1);
oracle.sql.BLOB BL1 = (oracle.sql.BLOB)b;
java.io.OutputStream opStream = BL1.getBinaryOutputStream();
opStream.write(bytearray);
opStream.close();
opStream = null;
conn.commit();
To read Blob:
oracle.sql.BLOB blobObj = (oracle.sql.BLOB)rs.getBlob(k);
byte[] bb = blobObj.getBytes(1,(int)blobObj.length());
String ss = new String(bb);
System.out.println("\n\nss: "+ss);
[ April 29, 2003: Message edited by: Surendran Velath ]
 
Every snowflake is perfect and unique. And every snowflake contains a very tiny ad.
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic