程序员人生 网站导航

java 对象序列化存储oracle

栏目:互联网时间:2014-09-16 07:51:29

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){} } } }


 

------分隔线----------------------------
------分隔线----------------------------

最新技术推荐