File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Oracle/OAS and the fly likes Inserting BLOB Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Products » Oracle/OAS
Reply Bookmark "Inserting BLOB " Watch "Inserting BLOB " New topic
Author

Inserting BLOB

maha raja
Greenhorn

Joined: Nov 12, 2002
Posts: 3
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.
Rene Larsen
Ranch Hand

Joined: Oct 12, 2001
Posts: 1177

This is how I do it...

Rene
[ June 09, 2003: Message edited by: Rene Larsen ]

Regards, Rene Larsen
Dropbox Invite
 
 
subject: Inserting BLOB
 
Threads others viewed
BLOB insertion
setBlob() method fro preparedstatement
How to store & retrieve a video file from database?
ORA-01460: unimplemented or unreasonable conversion requested
Storing blob > 4kb in oracle db through jdbc
IntelliJ Java IDE