java 對象序列化儲存oracle

來源:互聯網
上載者:User

標籤:java對象序列化儲存   serializable   oracle   oracle儲存java對象   

java 對象序列化儲存oracle:

 

import java.io.BufferedInputStream;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.InputStream;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.io.OutputStream;import java.sql.Connection;import java.sql.ResultSet;import java.sql.Statement;import oracle.sql.BLOB;/** *  * handle serial object with oracle dbStore<br/> * eg: create table TEST_OBJECTSTORE ( CLASSNAME VARCHAR2(256), CONTENT BLOB ) * @author Administrator *  */public class ObjectSerialStore {private String tableName;private String classNameColumn;private String serialObjColumn;/** * construct *  * @param tableName * @param classNameColumn * @param serialObjColumn */public ObjectSerialStore(String tableName, String classNameColumn,String serialObjColumn) {this.tableName = tableName;this.classNameColumn = classNameColumn;this.serialObjColumn = serialObjColumn;}/** * store the serial Object *  * @param dbConn  close after use * @param className serialObj.getClass().getName() or OBJ.class.getName() * @param serialObj */public final void storeSerialObject(Connection dbConn, String className,Object serialObj) {Statement stmt = null;ResultSet rs = null;try {ByteArrayOutputStream byteArray = new ByteArrayOutputStream();ObjectOutputStream objOuts = new ObjectOutputStream(byteArray);objOuts.writeObject(serialObj);final byte[] objBytes = byteArray.toByteArray();dbConn.setAutoCommit(false);stmt = dbConn.createStatement();stmt.executeUpdate("insert into " + this.tableName + " ("+ this.classNameColumn + ", " + this.serialObjColumn+ ") values ('" + className + "', empty_blob())");rs = stmt.executeQuery("select " + this.serialObjColumn + " from "+ this.tableName + " where " + this.classNameColumn + "='"+ className + "' for update");if (rs.next()) {BLOB blob = (BLOB) rs.getBlob(this.serialObjColumn);@SuppressWarnings("deprecation")OutputStream outStream = blob.getBinaryOutputStream();outStream.write(objBytes, 0, objBytes.length);outStream.flush();outStream.close();}dbConn.commit();byteArray.close();objOuts.close();} catch (Exception e) {System.out.println("The error when serial obj:"+e.getMessage());} finally {close(rs,stmt,dbConn);}}/** * update the serial Object * @param dbConn close after use * @param className serialObj.getClass().getName() or OBJ.class.getName() * @param serialObj */public final void updateSerialObject(Connection dbConn, String className,Object serialObj){Statement stmt = null;ResultSet rs = null;try {ByteArrayOutputStream byteArray = new ByteArrayOutputStream();ObjectOutputStream objOuts = new ObjectOutputStream(byteArray);objOuts.writeObject(serialObj);final byte[] objBytes = byteArray.toByteArray();dbConn.setAutoCommit(false);stmt = dbConn.createStatement();stmt.executeUpdate("update "+this.tableName+" set "+this.serialObjColumn+"=empty_blob() where "+this.classNameColumn+"='"+className+"'");rs = stmt.executeQuery("select " + this.serialObjColumn + " from "+ this.tableName + " where " + this.classNameColumn + "='"+ className + "' for update nowait");if (rs.next()) {BLOB blob = (BLOB) rs.getBlob(this.serialObjColumn);@SuppressWarnings("deprecation")OutputStream outStream = blob.getBinaryOutputStream();outStream.write(objBytes, 0, objBytes.length);outStream.flush();outStream.close();}dbConn.commit();byteArray.close();objOuts.close();} catch (Exception e) {System.out.println("The error when update serial obj:"+e.getMessage());} finally {close(rs,stmt,dbConn);}}/** * get the serial Object from db *  * @param dbConn close after use * @param className serialObj.getClass().getName() or OBJ.class.getName() * @return */public final Object getSerialObject(Connection dbConn, String className) {Statement stmt = null;ResultSet rs = null;Object returnObj = null;try{stmt = dbConn.createStatement();rs = stmt.executeQuery("select "+this.serialObjColumn+" from "+this.tableName+" where "+this.classNameColumn+"='"+className+"'");BLOB blob = null;if(rs.next()){blob = (BLOB) rs.getBlob(this.serialObjColumn);}InputStream is = blob.getBinaryStream();BufferedInputStream bufferIs = new BufferedInputStream(is);byte[] byteArrays = new byte[blob.getBufferSize()];while(-1 != bufferIs.read(byteArrays, 0, byteArrays.length));ObjectInputStream objInput = new ObjectInputStream(new ByteArrayInputStream(byteArrays));returnObj = objInput.readObject();is.close();bufferIs.close();objInput.close();}catch(Exception e){System.out.println("The error when deserial obj:"+e.getMessage());}finally{close(rs,stmt,dbConn);}return returnObj;}private void close(ResultSet rs,Statement stmt,Connection conn){if(rs != null){try{rs.close();}catch(Exception e){}}if(stmt != null){try{stmt.close();}catch(Exception e){}}if(conn != null){try{conn.close();}catch(Exception e){}}}}


 

java 對象序列化儲存oracle

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.