zoukankan      html  css  js  c++  java
  • java序列化对象 插入、查询、更新到数据库

    java序列化对象 插入、查询、更新到数据库 :

    实现代码例如以下:

    import java.io.ByteArrayInputStream;
    import java.io.ByteArrayOutputStream;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.sql.Blob;
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.Statement;
    
    /**
     *
     * 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;
    	}
    
    	public final void storeSerialObject(Connection dbConn,String className,Object serialObj){
    		PreparedStatement pstmt = null;
    		try {
    			ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
    			ObjectOutputStream objOuts = new ObjectOutputStream(byteArray);
    			objOuts.writeObject(serialObj);
    
    			final byte[] objBytes = byteArray.toByteArray();
    
    			pstmt = dbConn.prepareStatement("insert into "+ this.tableName +" ("
    					+this.classNameColumn+", "+this.serialObjColumn
    					+") values (?,?

    )"); pstmt.setString(1, className); ByteArrayInputStream bis = new ByteArrayInputStream(objBytes); pstmt.setBinaryStream(2, bis,objBytes.length); pstmt.execute(); } catch (Exception e) { System.out.println("The error when serial obj:"+e.getMessage()); } finally { close(null,pstmt,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){ PreparedStatement pstmt = null; ResultSet rs = null; try { ByteArrayOutputStream out = new ByteArrayOutputStream(); ObjectOutputStream objOuts = new ObjectOutputStream(out); objOuts.writeObject(serialObj); final byte[] objBytes = out.toByteArray(); pstmt = dbConn.prepareStatement("update "+this.tableName+" set "+this.serialObjColumn+"=? where "+this.classNameColumn+"=?"); ByteArrayInputStream input = new ByteArrayInputStream(objBytes); pstmt.setBinaryStream(1, input, objBytes.length); pstmt.setString(2, className); pstmt.execute(); input.close(); out.close(); objOuts.close(); } catch (Exception e) { System.out.println("The error when update serial obj:"+e.getMessage()); } finally { close(rs,pstmt,dbConn); } } 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 = rs.getBlob(this.serialObjColumn); } byte[] getBytes = blob.getBytes(1, (int)blob.length()); ObjectInputStream objInput = new ObjectInputStream(new ByteArrayInputStream(getBytes)); returnObj = objInput.readObject(); 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序列化对象 插入、查询、更新到数据库 。



     

  • 相关阅读:
    MYSQL—— 基础入门,增、删、改、查(基础篇)
    作为测试你不可不知的iOS与Android差异点!
    MYSQL—— Insert的几种用法!
    mysql 使用Navicat Lite如何打开‘查询编辑器’,使用sql语句对表进行操作!
    mysql基础总结
    C#中的DllImport
    StructLayout特性
    c++中指针类型在c#中怎么对应?
    c++/java/c# 几种编程语言的指针、引用比较
    如何在C#中模拟C++的联合(Union)?[C#, C++] How To Simulate C++ Union In C#?
  • 原文地址:https://www.cnblogs.com/zfyouxi/p/5172940.html
Copyright © 2011-2022 走看看