我能够将自定义jframe序列化到数据库中,并且我想反序列化它。
不幸的是,在所谓的反序列化我有一个错误,我不能解决。
import sys
from java.io import *
from java.lang import Class
from java.sql import *
from methods import *
class MyFrame(JFrame):
def __init__(self):
self._title = 'Frame di prova'
self.setTitle(self._title)
c = self.getContentPane()
self._insertN = JTextField(20)
nameLabelPane, nameFieldPane = incidentPanels(
JLabel("Nome"), [self._insertN])
firstBlock = (nameLabelPane, nameFieldPane)
firstPane = gridPane(firstBlock)
if self._title.startswith('Modifica'):
saveBtn = JButton('Modifica')
else:
saveBtn = JButton('Salva')
globalPane = JPanel()
gridBagger(globalPane, (firstPane, saveBtn))
c.add(JScrollPane(globalPane))
self.pack()
SQL_CREATE_TABLE = "create table if not exists serialized_java_objects (object_name varchar(1000), serialized_object blob)"
SQL_SERIALIZE_OBJECT = "INSERT INTO serialized_java_objects(object_name, serialized_object) VALUES (?, ?)"
SQL_DESERIALIZE_OBJECT = "SELECT serialized_object FROM serialized_java_objects limit 1"
def createTable(connection):
connection.createStatement().executeUpdate(SQL_CREATE_TABLE)
def serializeJavaObjectToDB(connection, objectToSerialize):
pstmt = connection.prepareStatement(SQL_SERIALIZE_OBJECT)
pstmt.setString(1, 'Frame di prova')
pstmt.setObject(2, objectToSerialize)
pstmt.executeUpdate()
pstmt.close()
def deSerializeJavaObjectFromDB(connection):
pstmt = connection.prepareStatement(SQL_DESERIALIZE_OBJECT)
rs = pstmt.executeQuery()
rs.next()
buf = rs.getBytes(1)
objectIn = None
if (buf != None):
objectIn = ObjectInputStream(ByteArrayInputStream(buf))
deSerializedObject = objectIn.readObject()
rs.close()
pstmt.close()
return deSerializedObject
def getMySqlConnection():
mysqlConn = None
mysqlLog_server = "localhost"
mysqlLog_username = "root"
mysqlLog_password = ""
mysqlLog_database = "tuttle"
properties = Properties()
properties.put("user", mysqlLog_username)
properties.put("password", mysqlLog_password)
url = "jdbc:mysql://" + mysqlLog_server + "/" + mysqlLog_database + \
"?user=" + mysqlLog_username + "&password=" + mysqlLog_password
mysqlConn = DriverManager.getConnection(url)
return mysqlConn
def main(args):
frame = MyFrame()
Class.forName("com.mysql.jdbc.Driver")
connection = getMySqlConnection()
createTable(connection)
serializeJavaObjectToDB(connection, frame)
objFromDatabase = deSerializeJavaObjectFromDB(connection)
objFromDatabase.setVisible(True)
if __name__ == '__main__':
main(sys.argv)
错误在线路上 objFromDatabase = deSerializeJavaObjectFromDB(connection)
,尤其是 deSerializedObject = objectIn.readObject()
具体如下:
Traceback (most recent call last):
File "provaSerializzazione.py", line 119, in <module>
main(sys.argv)
File "provaSerializzazione.py", line 113, in main
objFromDatabase = deSerializeJavaObjectFromDB(connection)
File "provaSerializzazione.py", line 74, in deSerializeJavaObjectFromDB
deSerializedObject = objectIn.readObject()
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at java.io.ObjectInputStream.resolveClass(Unknown Source)
at java.io.ObjectInputStream.readNonProxyDesc(Unknown Source)
at java.io.ObjectInputStream.readClassDesc(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at java.io.ObjectInputStream.readObject(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.lang.ClassNotFoundException: java.lang.ClassNotFoundException: org.python.proxies.__main__$MyFrame$3
好奇的事实是,如果你换线 frame = MyFrame()
与 frame = JFrame()
函数序列化正确,没有错误。
反序列化函数似乎不接受python类(而python类又扩展了jframe)。我应该做些什么来反序列化我的 MyFrame
用jython语言上课?提前谢谢!
1条答案
按热度按时间kiayqfof1#
你需要使用
org.python.util.PythonObjectInputStream
而不是java.io.ObjectInputStream
.