private static void writeBLOB( Statement myStatement, String fileName ) throws SQLException, IOException { // step 1: initialize the LOB column to set the LOB locator myStatement.executeUpdate( "INSERT INTO blob_content(file_name, blob_column) " + "VALUES ('" + fileName + "', EMPTY_BLOB())" ); // step 2: retrieve the row containing the LOB locator ResultSet blobResultSet = myStatement.executeQuery( "SELECT blob_column " + "FROM blob_content " + "WHERE file_name = '" + fileName + "' " + "FOR UPDATE" ); blobResultSet.next(); // step 3: create a LOB object and read the LOB locator BLOB myBlob = ((OracleResultSet) blobResultSet).getBLOB("blob_column"); // step 4: get the buffer size of the LOB from the LOB object int bufferSize = myBlob.getBufferSize(); // step 5: create a buffer to hold a block of data from the file byte [] byteBuffer = new byte[bufferSize]; // step 6: create a file object File myFile = new File(fileName); // step 7: create a file input stream object to read // the file contents FileInputStream myFileInputStream = new FileInputStream(myFile); // step 8: create an input stream object and call the appropriate // LOB object output stream function OutputStream myOutputStream = myBlob.getBinaryOutputStream(); // step 9: while the end of the file has not been reached, // read a block from the file into the buffer, and write the // buffer contents to the LOB object via the output stream int bytesRead; while ((bytesRead = myFileInputStream.read(byteBuffer)) != -1) { // write the buffer contents to the output stream // using the write() method myOutputStream.write(byteBuffer); } // end of while // step 10: close the stream objects myFileInputStream.close(); myOutputStream.close(); System.out.println("Wrote content from file " + fileName + " to BLOB"); } // end of writeBLOB()
后面加上for update之后捕获异常:java.sql.SQLException: ORA-01002: 读取违反顺序
//如果id号唯一,修改时先把原来的数据删除,然后在进行插入
JDBC.executeUpdate("delete from test_upload where id='" + id + "'");
//将数据插入到数据库中
String strSql =
"insert into test_upload(id,filename,content,filesize,filestyle,fileheigth,filewidth) values('" +
id + "','" + fileName + "',empty_blob(),'"+fileSize+"','"+fileStyle+"','"+imageHeight+"','"+imageWidth+"')";
JDBC.executeUpdate(strSql); Result res = JDBC.executeQuery(
"select content from test_upload where id='" + id + "' for update ");
if (res.rowNum() > 0) {
//将图片写入到数据库中
oracle.sql.BLOB blob = (oracle.sql.BLOB) res.getObject(0, 0);
OutputStream outStream = blob.getBinaryOutputStream();
inStream.read(bytes);
outStream.write(bytes);
outStream.flush();
outStream.close();
}
inStream.close();
[email protected]
Statement myStatement,
String fileName
) throws SQLException, IOException { // step 1: initialize the LOB column to set the LOB locator
myStatement.executeUpdate(
"INSERT INTO blob_content(file_name, blob_column) " +
"VALUES ('" + fileName + "', EMPTY_BLOB())"
); // step 2: retrieve the row containing the LOB locator
ResultSet blobResultSet = myStatement.executeQuery(
"SELECT blob_column " +
"FROM blob_content " +
"WHERE file_name = '" + fileName + "' " +
"FOR UPDATE"
);
blobResultSet.next(); // step 3: create a LOB object and read the LOB locator
BLOB myBlob =
((OracleResultSet) blobResultSet).getBLOB("blob_column"); // step 4: get the buffer size of the LOB from the LOB object
int bufferSize = myBlob.getBufferSize(); // step 5: create a buffer to hold a block of data from the file
byte [] byteBuffer = new byte[bufferSize]; // step 6: create a file object
File myFile = new File(fileName); // step 7: create a file input stream object to read
// the file contents
FileInputStream myFileInputStream = new FileInputStream(myFile); // step 8: create an input stream object and call the appropriate
// LOB object output stream function
OutputStream myOutputStream = myBlob.getBinaryOutputStream(); // step 9: while the end of the file has not been reached,
// read a block from the file into the buffer, and write the
// buffer contents to the LOB object via the output stream
int bytesRead; while ((bytesRead = myFileInputStream.read(byteBuffer)) != -1) { // write the buffer contents to the output stream
// using the write() method
myOutputStream.write(byteBuffer); } // end of while // step 10: close the stream objects
myFileInputStream.close();
myOutputStream.close(); System.out.println("Wrote content from file " +
fileName + " to BLOB"); } // end of writeBLOB()