我正在尝试使用java语言将我的对象存储在MySQL数据库中。我试图将对象转换为字节,所以我可以将它存储到LONGBLOB中。但我面临的错误是“NotSerializable Exception”。
我的班级我想要疼痛的对象:
public class Books implements Serializable {
private int isbn;
private String bookName;
private String author;
private String edition;
private int rowNo;
private int colNo;
private String shelfNo;
private String img;
private InputStream imag;
validation v = new Validation();
Database database;
public Books() { database = new Database(); }
.
.
setters & getters...
调用数据库方法在数据库中插入对象的方法:
String className = this.getClass().getName();
database.insertBookRecord(this.getIsbn(), this, className);
this
是我想要存储的当前类的对象。
以下是insertBookRecord
方法。
public void insertBookRecord(int isbn, Books book, String name) {
String query = "INSERT INTO Test VALUES (?, ?, ?)";
byte[] data = null;
//book = new Books();
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(book);
oos.flush();
oos.close();
baos.close();
data = baos.toByteArray();
}
catch(IOException ex) {
JOptionPane.showMessageDialog(null, ex.getMessage());
}
try {
//conn.setAutoCommit(false);
state = conn.prepareStatement(query);
state.setInt(1, isbn);
state.setString(2, name);
state.setObject(3, data);
state.executeUpdate();
//conn.commit();
}
catch(SQLException ex) {
JOptionPane.showMessageDialog(null, ex.getMessage());
}
finally {
close(3);
}
}
当我到达oos.writeObject(book);
语句时,它会在例外情况下停止并显示包裹& JOptionPane中的类名。
我的图书对象包含我在文本字段中输入的所有数据字段。但是我无法写它./将它转换为Serializable格式。
有什么建议吗?
堆栈追踪:
java.io.NotSerializableException: com.my.classes.Database
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 com.my.classes.Database.insertBookRecord(Database.java:123)
at com.my.classes.Books.insertBookRecord(Books.java:107)
at com.my.jlms.ManageBooks$2.actionPerformed(ManageBooks.java:308)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
答案 0 :(得分:1)
您遇到异常,因为您正在尝试序列化InputStream
private InputStream imag;
https://gyazo.com/a1f4eb91c7d6d8a3730bfb3ca610cde6不是Serializable
通过声明transient
:
private transient InputStream imag;
答案 1 :(得分:0)
你的imag是InputStream,它不是Serializable的实现。它应该是一个byte []或String链接到你的imag位置。
编辑:您应该将业务与模型对象分开。将insertBook方法和数据库对象从Book类移到另一个类。
答案 2 :(得分:0)
所以问题是InputStream以及我试图Seralize对象,即V&数据库。
我刚刚添加了例如ValueError: could not convert string to float: $46.5
的瞬态,然后在构造函数中声明了private transient Database database;
。因此我能够将它序列化。
感谢@Ralf,@ Joseph,@ Diyarbakir和@ 3kings帮助我。