This week's giveaway is in the JDBC forum.
We're giving away four copies of Java Database Connections & Transactions (e-book only) and have Marco Behler on-line!
See this thread for details.
Win a copy of Java Database Connections & Transactions (e-book only) this week in the JDBC forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Inserting BLOB  RSS feed

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hai All,
I wanna insert a BLOB in to oracle DB and my sample code is :
Connection conn = null;
ResultSet result = null;
int columns = 0;
PreparedStatement ps = null;
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
OutputStream out = null;
String sqlInsert = "INSERT INTO TEST Values (?,EMPTY_BLOB())";
String sqlLockRow = "SELECT GENLIST FROM TEST WHERE PROID = ? FOR UPDATE";
String sqlUpdateBlob = "UPDATE TEST SET GENLIST = ? WHERE PROID = ?";
try {
//conn = this.getConnection();
conn = getConn() ;
if (conn != null) {
conn.setAutoCommit(false);
ps = conn.prepareStatement(sqlInsert);
ps.clearParameters();
ps.setInt(1 , proid.intValue());
ps.executeUpdate();
//conn.commit();
System.out.println(" inserted .... ");
conn.commit();
conn.setAutoCommit(false);
//lock new row
ps = conn.prepareStatement(sqlLockRow);
ps.clearParameters();
ps.setInt(1, proid.intValue());
result = ps.executeQuery();
conn.commit();
conn.setAutoCommit(false);
result.next();
// get byte[] from Hashtable
ObjectOutputStream os = new ObjectOutputStream(byteStream);
os.writeObject(aUserData);
os.close();
byte[] hashBytes = byteStream.toByteArray();

//update blob
ps = conn.prepareStatement(sqlUpdateBlob);

oracle.sql.BLOB dbBlob = (oracle.sql.BLOB )result.getBlob(1);
out = ((oracle.sql.BLOB)dbBlob).getBinaryOutputStream();
out.write(hashBytes);
out.flush();
out.close();
ps.setBlob(1,dbBlob);
ps.setInt(2, proid.intValue());
ps.executeUpdate();
conn.commit();
System.out.println(" done ........");
}
}
catch (Exception ex) {
System.out.println(" exception from saveUserHash() : "+ex);
}
system hang when it execute
result = ps.executeQuery();
I don't understand why ? and if any body got any
working code how to insert CLOB or BLOB into oracle that would be grate ...
Looking forward,
Greetings
NPA.
 
Ranch Hand
Posts: 1179
Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is how I do it...

Rene
[ June 09, 2003: Message edited by: Rene Larsen ]
 
I would challenge you to a battle of wits, but I see you are unarmed - shakespear. Unarmed tiny ad:
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!