posted 21 years ago
does that database support blob. what i mean is are you going to store that file in the db as a blob ? if yes then the code goes this way:
MultipartParser mp = null;
Connection con=//Write the code to get java.sql.Connection object
Statement st=null;
try {
mp = new MultipartParser(request, 10*1024*1024); // 10MB
}
catch (IOException ex) {
throw new UploadException("Error initializing multi-part parser",ex);
}
st = con.createStatement();
st.executeUpdate("insert into " + tableName + " (" +
primaryKeyFieldName
+ "," + blobFieldName + ") values (" + primaryFieldValue
+ ",EMPTY_BLOB() ) ");
/*the table where you blob data will recide. Insert an empty blob. The function EMPTY_BLOB() is provided by the Oracle Database itself. Check out if u have this feature in ur db
*/
rs = st.executeQuery("select " + blobFieldName + " from "
+ tableName + " where "
+ primaryKeyFieldName + " = "
+ primaryFieldValue + " FOR UPDATE ");
if (!rs.next()) {
throw new BlobInsertException("Unable to insert record with PrimaryKey " + primaryFieldValue);
}
Object o = rs.getBlob(1);
OutputStream os = ((weblogic.jdbc.common.OracleBlob) o).getBinaryOutputStream();
byte buffer[] = new byte[8192];
int size = 0;
int nread = 0;
//here input stream is from the the file
while ((nread = inputStream.read(buffer)) != -1) {
os.write(buffer, 0, nread);
size += nread;
}
os.flush();
os.close();
inputStream.close();
}
catch (SQLException ex) {
throw new BlobInsertException("Some database problem",ex);
}
catch (IOException ex) {
throw new BlobInsertException("Some problem in data streams",ex);
}
finally {
try {
if(rs != null) rs.close();
if(st != null) st.close();
}
catch (Exception ex) { }
}
let me know if u face any problems