gpt4 book ai didi

java - 如何在数据库中保存序列化对象

转载 作者:行者123 更新时间:2023-11-29 02:23:44 26 4
gpt4 key购买 nike

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);
}
}

这是学生类(class)包含的我的代码字符串标识;字符串名称内部标记我想序列化学生对象并将它们保存到代码中提到的 mysql 数据库中我不断收到类(class)强制转换异常 谁能帮帮我提前致谢

最佳答案

一种方法是将序列化对象存储在文本中,即表的属性作为文本。当你检索时,它和保存时一样,即它的序列化属性不会丢失。

关于java - 如何在数据库中保存序列化对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26901706/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com