The following code will do what you want once you have got hold of your file as a file.
conn.setAutoCommit(false);
String prepare = "insert into news_xml values(" + id + ", empty_blob())";
String cmd = "SELECT * FROM news_xml where id=" + id + " for update";
Statement stmt = conn.createStatement( ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
stmt.executeUpdate(prepare);
ResultSet rset = stmt.executeQuery(cmd);
rset.next();
BLOB blob = (BLOB)((OracleResultSet)rset).getBlob(2);
FileInputStream instream = new FileInputStream(f);
OutputStream outstream = blob.getBinaryOutputStream();
int size = blob.getBufferSize();
byte[] buffer = new byte[size];
int length = -1;
while ((length = instream.read(buffer)) != -1) outstream.write(buffer, 0, length);
instream.close();
outstream.close();
conn.commit();要在jsp中显示把
while ((length = instream.read(buffer)) != -1)
out.print(buffer);
conn.setAutoCommit(false);
String prepare = "insert into news_xml values(" + id + ", empty_blob())";
String cmd = "SELECT * FROM news_xml where id=" + id + " for update";
Statement stmt = conn.createStatement( ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
stmt.executeUpdate(prepare);
ResultSet rset = stmt.executeQuery(cmd);
rset.next();
BLOB blob = (BLOB)((OracleResultSet)rset).getBlob(2);
FileInputStream instream = new FileInputStream(f);
OutputStream outstream = blob.getBinaryOutputStream();
int size = blob.getBufferSize();
byte[] buffer = new byte[size];
int length = -1;
while ((length = instream.read(buffer)) != -1) outstream.write(buffer, 0, length);
instream.close();
outstream.close();
conn.commit();要在jsp中显示把
while ((length = instream.read(buffer)) != -1)
out.print(buffer);
出现错误:
The method oracle.jdbc.driver.OracleBlob getBlob(int) declared in class oracle.jdbc.driver.OracleResultSet cannot override the method of the same signature declared in interface java.sql.ResultSet. They must have the same return type.
package org.apache.jsp;Why?
Connection connection = ds.getConnection();
connection.setReadOnly(false);
Statement statement = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
ResultSet results = statement.executeQuery(SQLString);
results.setFetchDirection(ResultSet.FETCH_UNKNOWN);if(results !=null)
{
while (results.next())
{
Blob blob = results.getBlob("Blob_Field_Name");
FileInputStream instream = new FileInputStream("F:\\someImage.jpg");
int length = instream.available();
byte[] buf = new byte[length];
instream.read(buf);
results.updateBytes("Blob_Field_Name", buf);
results.updateRow();
instream.close();
}
}