将一个序列化的对象存放到数据库的方法
2016-01-26 09:49
351 查看
将序列化的对象转成字节数据,将字节数据存放到数据库;
从数据库取出来是以getBlod的方法得到字节数据
public class TypeChangeTool {
private static final String TAG = TypeChangeTool.class.getSimpleName();
public static byte[] toByte(Object object) {
Log.d(TAG, "TypeChangeTool toByte()");
byte[] data = null;
if(object != null){
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
try {
ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
objectOutputStream.writeObject(object);
objectOutputStream.flush();
data = byteArrayOutputStream.toByteArray();
Log.d(TAG, "TypeChangeTool toByte() success");
} catch (IOException e) {
Log.d(TAG, "TypeChangeTool toByte() throw exception="+e.toString());
e.printStackTrace();
}
}
return data;
}
public static Object toObject(byte[] data) {
Log.d(TAG, "TypeChangeTool toUserTags()");
Object object = null;
if(data != null && data.length > 0){
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(data);
try {
ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
object = (Object)objectInputStream.readObject();
byteArrayInputStream.close();
objectInputStream.close();
Log.d(TAG, "TypeChangeTool toUserTags() success");
} catch (Exception e) {
Log.d(TAG, "TypeChangeTool toUserTags() throw exception="+e.toString());
e.printStackTrace();
}
}
return object;
}
}
从数据库取出来是以getBlod的方法得到字节数据
public class TypeChangeTool {
private static final String TAG = TypeChangeTool.class.getSimpleName();
public static byte[] toByte(Object object) {
Log.d(TAG, "TypeChangeTool toByte()");
byte[] data = null;
if(object != null){
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
try {
ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
objectOutputStream.writeObject(object);
objectOutputStream.flush();
data = byteArrayOutputStream.toByteArray();
Log.d(TAG, "TypeChangeTool toByte() success");
} catch (IOException e) {
Log.d(TAG, "TypeChangeTool toByte() throw exception="+e.toString());
e.printStackTrace();
}
}
return data;
}
public static Object toObject(byte[] data) {
Log.d(TAG, "TypeChangeTool toUserTags()");
Object object = null;
if(data != null && data.length > 0){
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(data);
try {
ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
object = (Object)objectInputStream.readObject();
byteArrayInputStream.close();
objectInputStream.close();
Log.d(TAG, "TypeChangeTool toUserTags() success");
} catch (Exception e) {
Log.d(TAG, "TypeChangeTool toUserTags() throw exception="+e.toString());
e.printStackTrace();
}
}
return object;
}
}
相关文章推荐
- InstallShield高级应用--检查是否安装ORACLE或SQL Server
- mysql常用操作
- oracle优化思考-双刃剑
- 解决 ORA-28547 连接服务器失败,可能是Oracle Net 管理错误
- sql字段类型之decimal 和 numeric
- Oracle中的EXISTS与IN
- MySQL的show语句大全
- 应用SqlGeometry无法加载sqlserverspatial.dll
- Linux Oracle服务启动&停止脚本与开机自启动
- sqlserver远程备份到另一台电脑(局域网、定时任务)
- redis中的事务、lua脚本和管道的使用场景
- Mysql的实时同步 - 双机互备
- MySql
- Oracle 最简单的随系统自己主动启动
- SQL Server 函数的使用 Function
- MongoDB Aggregate Methods(2) MonoDB 的 3 种聚合函数
- MONGODB 与sql聚合操作对应图
- oracle常用语法-约束<三>
- Mongodb无法启动:The default storage engine 'wiredTiger' is not available with this build of mongod.
- 扩展阅读- redis 配置参数详解