Java对象序列化到文件或数据库
由于前段时间在学习Java web方面的知识,涉及到了session对象的钝化与活化,对于将Java对象如何序列化到数据库中一直没有解决。今天看到一篇文章,得到了启发,并做了改进,得出本文
一、准备一个Java类用于序列化与反序列化,该类必须实现Serializable接口
package ser_test;
import java.io.Serializable;
import java.util.Date;
public class Student implements Serializable{
private static final long serialVersionUID = 1L;
private String name;
private Integer gender;
private Date birthday;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getGender() {
return gender;
}
public void setGender(Integer gender) {
this.gender = gender;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}
二、实现将Java对象序列化到文件及从文件读取数据进行反序列化
package ser_test;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Date;
public class ObjectStoreToFile {
public static void main(String[] args) throws Exception{
// storeObjToFile();
readObjFromFile();
}
/**
* 将Java对象序列化到文件
* @throws IOException
*/
public static void storeObjToFile() throws IOException {
Student stu = new Student();
stu.setName("Jimi");
stu.setGender(1);
stu.setBirthday(new Date());
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("obj.txt"));
oos.writeObject(stu);
}
/**
* 从文件读取数据进行反序列化为Java对象
* @throws IOException
* @throws ClassNotFoundException
*/
public static void readObjFromFile() throws IOException, ClassNotFoundException {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("obj.txt"));
Student stu = (Student) ois.readObject();
System.out.println(stu.getName() + "--" +stu.getGender() + "--" + stu.getBirthday());
}
}
结果:
- 实现将Java对象序列化到数据库及从数据库获取数据进行反序列化
数据库表
package ser_test;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
public class ObjectStoreToDb {
public static void main(String[] args) {
String driverClass = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/jeeweb?useunicode=true&charset=utf-8";
String user = "root";
String pwd = "password";
try {
Class.forName(driverClass);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {
Connection connection = DriverManager.getConnection(url, user, pwd);
// storeObjToDb(connection);
readObjFromDb(connection);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 将Java对象保存到数据库
* @param connection
* @throws SQLException
*/
public static void storeObjToDb(Connection connection) throws SQLException {
Student stu = new Student();
stu.setName("Jimi");
stu.setGender(1);
stu.setBirthday(new Date());
String sql = " insert into stu(student) values(?)";
PreparedStatement statement = connection.prepareStatement(sql);
statement.setObject(1, stu);
statement.executeUpdate();
}
//读取从数据库获取序列化后的二进制数据,反序列化为对应的Java对象
public static void readObjFromDb(Connection connection) throws SQLException,
IOException, ClassNotFoundException {
String sql = "select student from stu";
PreparedStatement statement = connection.prepareStatement(sql);
ResultSet resultSet = statement.executeQuery();
while(resultSet.next()) {
Blob blob = resultSet.getBlob(1);
InputStream is = blob.getBinaryStream();
ObjectInputStream ois = new ObjectInputStream(is);
Student stu = (Student) ois.readObject();
System.out.println(stu.getName() + "--" +stu.getGender() + "--" + stu.getBirthday());
}
}
}
结果:
注意:在看的博客中,从数据库获取序列化的数据后,需要进行好几次IO流的转换,但是blob.getBinaryStream();的返回值本来就是InputStream类型,而ObjectInputStream的构造函数刚好需要的也是一个InputStream类型的参数,所以直接进行使用即可
原文地址:http://blog.csdn.net/jeryjeryjery/article/details/70670103