java 如何在数据库中保存序列化对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26901706/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to save a serialized object in a database
提问by user3363357
package test;
//
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
class ssbn extends Student{
static final String WRITE_OBJECT_SQL = "INSERT INTO java_objects(name, object_value) VALUES (?, ?)";
static final String READ_OBJECT_SQL = "SELECT object_value FROM java_objects WHERE id = ?";
public static Connection getConnection() throws Exception {
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost/test";
String username = "root";
String password = "";
Class.forName(driver);
Connection conn = DriverManager.getConnection(url, username, password);
return conn;
}
public static long writeJavaObject(Connection conn, Object object) throws Exception {
String className = object.getClass().getName();
PreparedStatement pstmt = conn.prepareStatement(WRITE_OBJECT_SQL, Statement.RETURN_GENERATED_KEYS);
// set input parameters
pstmt.setString(1, className);
pstmt.setObject(2, object);
pstmt.executeUpdate();
// get the generated key for the id
ResultSet rs = pstmt.getGeneratedKeys();
int id = -1;
if (rs.next()) {
id = rs.getInt(1);
}
rs.close();
pstmt.close();
System.out.println("writeJavaObject: done serializing: " + className);
return id;
}
public static Object readJavaObject(Connection conn, long id) throws Exception {
PreparedStatement pstmt = conn.prepareStatement(READ_OBJECT_SQL);
pstmt.setLong(1, id);
ResultSet rs = pstmt.executeQuery();
rs.next();
Object object = rs.getObject(1);
String className = object.getClass().getName();
rs.close();
pstmt.close();
System.out.println("readJavaObject: done de-serializing: " + className);
return object;
}
public static void main(String args[])throws Exception {
Connection conn = null;
try {
conn = getConnection();
System.out.println("conn=" + conn);
conn.setAutoCommit(false);
List<Object> list = new ArrayList<Object>();
list.add("This is a short string.");
list.add(new Integer(1234));
list.add(new Date());
// long objectID = writeJavaObject(conn, list);
// conn.commit();
// System.out.println("Serialized objectID => " + objectID);
// List listFromDatabase = (List) readJavaObject(conn, objectID);
// System.out.println("[After De-Serialization] list=" + listFromDatabase);
Student st = new Student("001","aaa",95);
long objID = writeJavaObject(conn, st);
conn.commit();
System.out.println("SERIALIZED : " + objID);
//class cast exception
Student ss = (Student)readJavaObject(conn, objID);
System.out.println("DESERIALIZED : " + ss);
} catch (Exception e) {
e.printStackTrace();
} finally {
conn.close();
}
}
public ssbn(String id, String name, int marks) {
super(id, name, marks);
}
}
This is my code the class student contains String id; String name int marks and i want to serialize student objects and save them in to the mysql database mentioned in the code i keep getting a class cast exception can anyone help me thanks in advance
这是我的代码,学生包含字符串 id;字符串名称 int 标记,我想序列化学生对象并将它们保存到代码中提到的 mysql 数据库中,我不断收到类转换异常,任何人都可以帮助我,提前致谢
采纳答案by Sunil Goli
One way would be store serialized object in text, ie attribute of the table as text. When you retrieve, it is same as it was while saving, ie its serialization property doesn't get lost.
一种方法是将序列化对象存储在文本中,即将表的属性存储为文本。当您检索时,它与保存时相同,即其序列化属性不会丢失。
回答by Sunil Goli
Do you need to store relations between the objects or just reference the objects by id?
您需要存储对象之间的关系还是仅通过 id 引用对象?