/**
* 向数据库插入一条战斗回放纪录
* @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;
}
}