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 = L;
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();
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());
}
}
結果:
![](https://img.laitimes.com/img/9ZDMuAjOiMmIsIjOiQnIsICM38CXlZHbvN3cpR2Lc1TPB10QGtWUCpEMJ9CXsxWam9CXwADNvwVZ6l2c052bm9CXUJDT1wkNhVzLcRnbvZ2LcdHeHJWNoNDTwYVbiVHNHpleO1GTulzRilWO5x0LcRHelR3LcJzLctmch1mclRXY39jNycTNwkTNwIDOxMDM4EDMy8CX0Vmbu4GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.jpg)
- 實作将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();
stu.setBirthday(new Date());
String sql = " insert into stu(student) values(?)";
PreparedStatement statement = connection.prepareStatement(sql);
statement.setObject(, 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();
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