英文:
Retrive serialized JFrame to upload in MySql
问题
I'll provide a brief translation of the key points from your text:
- 如何使用Jython序列化JFrame(假设序列化是将填充的JFrame“打包”到数据库的最佳方式,如果有更好的方法,请告诉我)
- 在Mysql上设置什么类型的数据(假设Mysql是执行此操作的完美DBMS,如果有更好的技术,请告诉我!)以及是否需要进行特殊查询
- 如何反序列化以查看它与离开时完全相同(假设反序列化是检索JFrame的最佳方式)
请注意,Jython和Java之间的对象序列化通常是可能的,但涉及到复杂的Swing组件可能会更加复杂。解决此问题可能需要自定义序列化和反序列化过程。
如有需要,请继续提出您的具体问题或要求。
英文:
I have an object in Jython that extends a Java Swing JFrame. My goal is to serialize it in order to save it on MySql, query the database, deserialize it and review the JFrame exactly as it was before it was deserialized (with all fields filled in).<br><br>
I honestly don't know where to start. I can't imagine serialization - saving to database.
I guess the serialized JFrame could be blob
, longblob
or bit
type on MySql. <br><br>
For now I have done a local experiment using my old and little academic knowledge on the serialization of objects in java, with poor results:
def saveArt(self, e):
v = Vector()
v.add(self) # self = JFrame in question
out = ObjectOutputStream(BufferedOutputStream(FileOutputStream("prova.dat")))
out.writeObject(v)
out.close()
What I did was just put the frame in a Java vector and save it to the file, however I got the following error:
Exception in thread "AWT-EventQueue-0" at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.defaultWriteFields(Unknown Source)
at java.io.ObjectOutputStream.writeSerialData(Unknown Source)
at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.writeObject(Unknown Source)
at java.util.concurrent.ConcurrentHashMap.writeObject(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at java.io.ObjectStreamClass.invokeWriteObject(Unknown Source)
at java.io.ObjectOutputStream.writeSerialData(Unknown Source)
at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.defaultWriteFields(Unknown Source)
at java.io.ObjectOutputStream.writeSerialData(Unknown Source)
at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.defaultWriteFields(Unknown Source)
at java.io.ObjectOutputStream.writeSerialData(Unknown Source)
at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.writeObject(Unknown Source)
at java.util.concurrent.ConcurrentHashMap.writeObject(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at java.io.ObjectStreamClass.invokeWriteObject(Unknown Source)
at java.io.ObjectOutputStream.writeSerialData(Unknown Source)
at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.defaultWriteFields(Unknown Source)
at java.io.ObjectOutputStream.writeSerialData(Unknown Source)
at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.defaultWriteFields(Unknown Source)
at java.io.ObjectOutputStream.writeSerialData(Unknown Source)
at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.defaultWriteFields(Unknown Source)
at java.io.ObjectOutputStream.writeSerialData(Unknown Source)
at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.defaultWriteFields(Unknown Source)
at java.io.ObjectOutputStream.writeSerialData(Unknown Source)
at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.writeObject(Unknown Source)
at javax.swing.event.EventListenerList.writeObject(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at java.io.ObjectStreamClass.invokeWriteObject(Unknown Source)
at java.io.ObjectOutputStream.writeSerialData(Unknown Source)
at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.defaultWriteFields(Unknown Source)
at java.io.ObjectOutputStream.defaultWriteObject(Unknown Source)
at javax.swing.JComponent.writeObject(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at java.io.ObjectStreamClass.invokeWriteObject(Unknown Source)
at java.io.ObjectOutputStream.writeSerialData(Unknown Source)
at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.defaultWriteFields(Unknown Source)
at java.io.ObjectOutputStream.writeSerialData(Unknown Source)
at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.writeObject(Unknown Source)
at java.awt.AWTEventMulticaster.saveInternal(Unknown Source)
at java.awt.AWTEventMulticaster.saveInternal(Unknown Source)
at java.awt.AWTEventMulticaster.saveInternal(Unknown Source)
at java.awt.AWTEventMulticaster.saveInternal(Unknown Source)
at java.awt.AWTEventMulticaster.saveInternal(Unknown Source)
at java.awt.AWTEventMulticaster.saveInternal(Unknown Source)
at java.awt.AWTEventMulticaster.saveInternal(Unknown Source)
at java.awt.AWTEventMulticaster.saveInternal(Unknown Source)
at java.awt.AWTEventMulticaster.save(Unknown Source)
at java.awt.Component.writeObject(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at java.io.ObjectStreamClass.invokeWriteObject(Unknown Source)
at java.io.ObjectOutputStream.writeSerialData(Unknown Source)
at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.writeArray(Unknown Source)
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.access$300(Unknown Source)
at java.io.ObjectOutputStream$PutFieldImpl.writeFields(Unknown Source)
at java.io.ObjectOutputStream.writeFields(Unknown Source)
at java.util.Vector.writeObject(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at java.io.ObjectStreamClass.invokeWriteObject(Unknown Source)
at java.io.ObjectOutputStream.writeSerialData(Unknown Source)
at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.writeObject(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
java.io.NotSerializableException: java.io.NotSerializableException: org.python.core.packagecache.SysPackageManager
This experiment is of little importance, because it is certainly not the final goal. It was just to figure out if I could serialize it locally.<br><br>
The final goal is to save it on the MySql database and extract it exactly as it was saved. <br><br>
So I need to understand 3 things:
- how to serialize JFrame with Jython (assuming serialization is the best way to "package" a filled JFrame for a database, if there's a better way don't hesitate to say so)
- what kind of data to set on Mysql (assuming MySql is the perfect dbms to do this, if there are better technologies, don't hesitate to say it!) and if you need to make a particular query
- how to deserialize it to see it exactly as I left it (assuming deserialization is the best way to retrieve the JFrame)
Thank you so much in advance my saviors (if there are any)
答案1
得分: 3
Sure, here's the translated content:
首先,无论何时,Java序列化都不是一个好主意。Java SE的安全编码准则表示:“注意:对不受信任的数据进行反序列化本质上是危险的,应避免使用。” 不幸的是,该文本不再是红色的。
在这种情况下,问题更加复杂,因为该数据将只是数据库中的一个二进制大对象(blob),而Swing不能保证在不同版本(甚至更新?)之间的串行化兼容性。
既然问题已经解决:从堆栈跟踪中可以看出,问题对象是一个SysPackageManager
,位于ConcurrentHashMap
本身内部,而你可能不希望保留它。
将系统属性sun.io.serialization.extendedDebugInfo
(可能会更改)设置为true
将提供有关对象引用方式的更多信息。
显然JFrame
有点过于复杂了。有更好的方法来存储数据。
英文:
Firstly, Java Serialization is a bad idea at the best of times. The Secure Coding Guidelines for Java SE says "Note: Deserialization of untrusted data is inherently dangerous and should be avoided." Unfortunately that text is no longer in red.
It is even more problematic in this case as that data will just be in a blob in the database, and Swing doesn't guarantee serialisation compatibility between versions (perhaps even updates?).
That out of the way: From the stracktrace, the problem object is a SysPackageManager
somewhere within ConcurrentHashMap
itself within ConcurrentHashMap
itself, which you probably don't want to keep.
Setting the system property sun.io.serialization.extendedDebugInfo
(may change) to true
will give some more information as to how the object is referenced.
Clearly JFrame
is too much. There are better ways to store data.
答案2
得分: 0
Tom已经涵盖了错误的原因以及进一步调试的可能方法。
我将尝试详细回答您提出的3个问题,使用Java代码:
- 如何序列化? - 下面已经回答了
- 用哪种数据类型来创建MySQL列以存储这些数据? - 对于MySQL,我建议使用mediumblob,blob可能太小,longblob可能太大。我也使用了longtext,它可以正常工作而没有任何问题,但要解释为什么选择blob而不是文本字段会有一个非常长的答案。也许这个StackOverflow答案会更清晰:https://stackoverflow.com/questions/5544749/what-column-type-should-be-used-to-store-serialized-data-in-a-mysql-db
- 如何反序列化和使用? - 下面已经回答了
移动到答案:
您可以通过以下几个步骤来实现:
- 创建JFrame
- 将Java对象序列化到MySQL数据库 - 这就像下面这样简单
preparedStatement.setObject(jframe_object_to_save_to_database);
pstmt.executeUpdate();
-
从MySQL数据库反序列化Java对象并将其转换为JFrame对象 - 这并不是那么直接,但也不是那么复杂,我的方法将分为4个步骤,如下所示:
3.a. 仅将列值读取为字节数组
3.b. 创建一个字节数组的ByteArrayInputStream对象
3.c. 从ByteArrayInputStream创建ObjectInputStream
3.d. 使用ObjectInputStream.readObject()方法获取反序列化对象这些步骤在下面的代码片段中已经涵盖了:
byte[] buf = rs.getBytes(1);
ObjectInputStream objectIn = null;
if (buf != null)
objectIn = new ObjectInputStream(new ByteArrayInputStream(buf));
Object deSerializedObject = objectIn.readObject();
- 现在,我们已经准备好了反序列化的对象供我们使用(来自3.d),您可以轻松将其解析为JFrame对象并按需要使用它
Jframe deSerializedObject = (JFrame) objectIn.readObject();
以下是我完整的Java实现代码,这是一个与MySQL数据库一起使用的简单JFrame工作代码。请按照我在代码中作为注释写的步骤1到步骤5进行操作,还请阅读我在deSerializeJavaObjectFromDB方法中提到的注意事项。
// 代码太长,请查看原文
注意1:我没有设置Jython,所以无法提供Python/Jython代码片段。但我正在尝试用纯Java来回答这个问题,我认为您可以很容易地将其与您在Jython中的需求联系起来(因为我使用的类不会变化)。
注意2:我不想辩论序列化的持久性是对还是错,这取决于应用程序的类型、用户类型、项目类型和许多其他情况。您是对您的情况有更好的判断力。
注意3:这篇文章帮助我更好地回答了这个问题 - https://javapapers.com/core-java/serialize-de-serialize-java-object-from-database/
希望这有助于您!如果您有其他问题或需要进一步的帮助,请随时告诉我。
1: https://stackoverflow.com/questions/5544749/what-column-type-should-be-used-to-store-serialized-data-in-a-mysql-db
2: https://javapapers.com/core-java/serialize-de-serialize-java-object-from-database/
英文:
Tom has covered reasons of error and possible ways to debug further
I will try to address the 3 things you asked in detail with java code:
- How to serialize ? - Answered below
- Which datatype to use to create mysql column to store this data ? - I would say mediumblob in case of mysql, blob might to too small and longblob might be too big. I used longtext as well and it works as expected without any issues, but its really long answer to explain why blob and not text fields. May be this stackoverflow answer will throw more light - https://stackoverflow.com/questions/5544749/what-column-type-should-be-used-to-store-serialized-data-in-a-mysql-db
- How to de-serialize and use ? - Answered below
<br>
Moving to answer:
You can achieve this in few steps
-
create JFrame
-
serializing java object to mysql database - this is as simple as
preparedStatement.setObject(jframe_object_to_save_to_database); pstmt.executeUpdate();
-
de-serializing java object from mysql database and converting it into JFrame object - this is not that straight forward, but not that complicate too, my approch will be of 4 steps like below
3.a. Just read your column value as bytes array <br>
3.b. create a ByteArrayInputStream object of the bytes array we got from database <br>
3.c. create ObjectInputStream from ByteArrayInputStream <br>
3.d. use ObjectInputStream.readObject() method to get de-serialized object <br>These steps are covered in the below code snippet
byte[] buf = rs.getBytes(1); ObjectInputStream objectIn = null; if (buf != null) objectIn = new ObjectInputStream(new ByteArrayInputStream(buf)); Object deSerializedObject = objectIn.readObject();
-
Now as we have the de-serialized object ready for our use (from 3.d), you can parse it into a JFrame object easily and use it as you need
Jframe deSerializedObject = (JFrame) objectIn.readObject();
<br>
Below is the complete java implementation that I have, it is a simple JFrame working code with mysql database, please follow Step 1 thru Step 5 which I wrote as comments in the code and also read the Note that I mentioned in deSerializeJavaObjectFromDB method
<br>
<br>
import java.awt.FlowLayout;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class SerializeToDatabase {
private static final String SQL_CREATE_TABLE = "create table if not exists serialized_java_objects (object_name varchar(1000), serialized_object blob)";
private static final String SQL_SERIALIZE_OBJECT = "INSERT INTO serialized_java_objects(object_name, serialized_object) VALUES (?, ?)";
private static final String SQL_DESERIALIZE_OBJECT = "SELECT serialized_object FROM serialized_java_objects limit 1";
public static void createTable(Connection connection) throws SQLException {
connection.createStatement().executeUpdate(SQL_CREATE_TABLE);
}
public static void serializeJavaObjectToDB(Connection connection,
Object objectToSerialize) throws SQLException {
PreparedStatement pstmt = connection
.prepareStatement(SQL_SERIALIZE_OBJECT);
// just setting the class name
pstmt.setString(1, objectToSerialize.getClass().getName());
pstmt.setObject(2, objectToSerialize);
pstmt.executeUpdate();
pstmt.close();
System.out.println("Java object serialized to database. Object: " + objectToSerialize);
}
/**
* To de-serialize a java object from database
*
* @throws SQLException
* @throws IOException
* @throws ClassNotFoundException
*/
public static Object deSerializeJavaObjectFromDB(Connection connection) throws SQLException, IOException,
ClassNotFoundException {
PreparedStatement pstmt = connection.prepareStatement(SQL_DESERIALIZE_OBJECT);
ResultSet rs = pstmt.executeQuery();
rs.next();
//NOTE - below is the most basic way of retrieving data from result set, works perfect for general data
//Object object = rs.getObject(1);
//NOTE - below is the way how we need to implement to retrieve serialized objects from resultset
byte[] buf = rs.getBytes(1);
ObjectInputStream objectIn = null;
if (buf != null)
objectIn = new ObjectInputStream(new ByteArrayInputStream(buf));
Object deSerializedObject = objectIn.readObject();
rs.close();
pstmt.close();
System.out.println("Java object de-serialized from database. Object: "
+ deSerializedObject + " Classname: "
+ deSerializedObject.getClass().getName());
return deSerializedObject;
}
public static Connection getMySqlConnection(String ipAddr, String portNumber, String db, String userName, String password) throws SQLException {
Connection mysqlConn = null;
Properties properties = new Properties();
properties.put("user", userName);
properties.put("password", password);
mysqlConn = DriverManager.getConnection("jdbc:mysql://"+ipAddr+":"+portNumber+"/"+db, properties);
return mysqlConn;
}
/**
* Serialization and de-serialization of java object from mysql
*
* @throws ClassNotFoundException
* @throws SQLException
* @throws IOException
*/
public static void main(String args[]) throws ClassNotFoundException,
SQLException, IOException {
//step 1 - create mysql connection
Connection connection = getMySqlConnection("192.168.1.119", "3306", "xxx", "xxx", "xxx");
//step 2 - create JFrame
JFrame frame = new JFrame("JFrame Example");
JPanel panel = new JPanel();
panel.setLayout(new FlowLayout());
JLabel label = new JLabel("JFrame By Example");
JButton button = new JButton();
button.setText("Button");
panel.add(label);
panel.add(button);
frame.add(panel);
frame.setSize(200, 300);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//uncomment below line if you want to see the JFrame before persisting data
//frame.setVisible(true);
//step 3 - create table
createTable(connection);
//step 4 - serializing java object to mysql database
serializeJavaObjectToDB(connection, frame);
//step 5 - de-serializing java object from mysql database and converting it into JFrame object
JFrame objFromDatabase = (JFrame) deSerializeJavaObjectFromDB(connection);
//setVisible(true) will show the JFrame as is, what ever we have persisted
objFromDatabase.setVisible(true);
//finally close the connection
connection.close();
}
}
Note1: I have no jython setup, so I am unable to provide python/jython code snippet here. But I am trying to answer this in pure java, I think you can relate it easily to your need in jython (as the classes that I've used do not vary)
Note2: I did not want to debate if persistence of serialization is right or wrong, that depends on type of application, type of users, type of project and many other scenarios. You are better judge for your case
Note3: This post helped me to answer this question better - https://javapapers.com/core-java/serialize-de-serialize-java-object-from-database/
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论