Java Object Serialization Storage Oracle

Source: Internet
Author: User
Tags getmessage object serialization serialization

Java Object Serialization Storage 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 (Classna ME VARCHAR2 (), CONTENT BLOB) * @author Administrator * */public class Objectserialstore {private String Tablename;pri Vate 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 Ob Jectoutputstream (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.cl Ass.getname () * @param serialobj */public final void Updateserialobject (Connection dbconn, String classname,object serial OBJ) {Statement stmt = null; ResultSet rs = null;try {Bytearrayoutputstream ByteArray = new Bytearrayoutputstream (); ObjectOutputStream objouts = new Ob Jectoutputstream (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); @Suppre Sswarnings ("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 () O  R 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.ser ialobjcolumn+ "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 Object Serialization Storage Oracle

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.