java中将数据对象序列化后保存到数据库中,并反序列化取出显示

    /**

     * 向数据库插入一条战斗回放纪录

     * @param matchMovie 比赛回放纪录

     */

    public int insertPlayBackGame(MatchMovie matchmove) {

       byte[] buf = null;

       try {

           buf = SerializeUtil.serializeObject(matchmove);

       } catch (IOException e) {

           e.printStackTrace();

       }

       Connection conn = null;

       CallableStatement cstmt = null;

       try {

           conn = DBHelper.getConnection();

           cstmt = conn.prepareCall("{call insertPlayBackGame(?,?,?,?,?)}");

           cstmt.setInt(1, matchmove.getOrgId1());

           cstmt.setInt(2, matchmove.getOrgId2());

           cstmt.setBytes(3, buf);

           cstmt.setByte(4, (byte) matchmove.getMatchType());

           cstmt.registerOutParameter(5, Types.INTEGER);

           cstmt.executeUpdate();

           if (cstmt.getInt(5) != 0)

              return cstmt.getInt(5);

       } catch (SQLException e) {

           logger.error("DatabaseMgr: insertPlayBackGame");

           e.printStackTrace();

           return 0;

       } finally {

           DBHelper.close(conn, cstmt, null);

       }

       return 0;

    }

 

    /**

     * 显示某条回放纪录

     * @param backGameId 比赛回放纪录ID

     * @return 当前回放纪录的完整信息

     */

    public MatchMovie getPlayBackGame(int backGameId) {

       Connection conn = null;

       CallableStatement cstmt = null;

       ResultSet rs = null;

       MatchMovie matchMovie = null;

       byte[] buf = null;

       boolean isFristShow = false;

       int movieId = 0;

       try {

           conn = DBHelper.getConnection();

           cstmt = conn.prepareCall("{call getPlayBackGame(?)}");

           cstmt.setInt(1, backGameId);

           rs = cstmt.executeQuery();

           while (rs.next()) {

              buf = rs.getBytes("process");

              isFristShow = rs.getBoolean("receiveDraw");

              movieId = rs.getInt("pbgId");

           }

           try {

              matchMovie = (MatchMovie) SerializeUtil.deserializeObject(buf);

              matchMovie.setReceiveDraw(isFristShow);

              matchMovie.setMovieId(movieId);

           } catch (IOException e1) {

              e1.printStackTrace();

           } catch (ClassNotFoundException e) {

              e.printStackTrace();

           }

           return matchMovie;

       } catch (SQLException e) {

           logger.error("DatabaseMgr: getPlayBackGame");

           e.printStackTrace();

       } finally {

           DBHelper.close(conn, cstmt, rs);

       }

       return null;

    }

 

 

==============

 

public final class SerializeUtil {

   

    /** 序列化对象

     * @throws IOException */

    public static byte[] serializeObject(Object object) throws IOException{

       ByteArrayOutputStream saos=new ByteArrayOutputStream();

       ObjectOutputStream oos=new ObjectOutputStream(saos);

       oos.writeObject(object);

       oos.flush();

       return saos.toByteArray();

    }

   

    /** 反序列化对象

     * @throws IOException

     * @throws ClassNotFoundException */

    public static Object deserializeObject(byte[]buf) throws IOException, ClassNotFoundException{

       Object object=null;

       ByteArrayInputStream sais=new ByteArrayInputStream(buf);

       ObjectInputStream ois = new ObjectInputStream(sais);

       object=(MatchMovie) ois.readObject();

       return object;

    }

}

 

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值