将对象存储在 Java 的数据库中
Storing object in database in Java
我正在尝试使用 java 语言将我的对象存储在 MySQL 数据库中。我正在尝试将对象转换为字节,以便将其存储到 LONGBLOB 中。但我面临错误,即 "NotSerializable Exception".
我的Class我想痛谁的对象:
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
是我要存储的当前 class 的对象。
以下是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 中显示包和 Classname。
我的图书对象具有我在文本字段中输入的所有数据字段。但是我写不出来。/把它转换成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.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.access0(Unknown Source)
at java.awt.EventQueue.run(Unknown Source)
at java.awt.EventQueue.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.run(Unknown Source)
at java.awt.EventQueue.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)
您遇到异常是因为您正在尝试序列化 InputStream
private InputStream imag;
InputStream 不是 Serializable
您可以在序列化时通过声明省略此字段 transient
:
private transient InputStream imag;
您的图像是未实现序列化的 InputStream。它应该是一个 byte[] 或 String link 到您的图片位置。
已编辑:您应该将业务与模型对象分开。将 insertBook 方法和数据库对象从 Book class 移动到另一个 class.
所以问题是 InputStream 以及我试图序列化对象,即 V 和数据库。
我只是添加了瞬态,例如 private transient Database database;
,然后在构造函数中声明了 database = new Database();
。因此我能够序列化它。
感谢@Ralf、@Joseph、@Diyarbakir 和@3kings 帮助我。
我正在尝试使用 java 语言将我的对象存储在 MySQL 数据库中。我正在尝试将对象转换为字节,以便将其存储到 LONGBLOB 中。但我面临错误,即 "NotSerializable Exception".
我的Class我想痛谁的对象:
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
是我要存储的当前 class 的对象。
以下是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 中显示包和 Classname。
我的图书对象具有我在文本字段中输入的所有数据字段。但是我写不出来。/把它转换成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.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.access0(Unknown Source)
at java.awt.EventQueue.run(Unknown Source)
at java.awt.EventQueue.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.run(Unknown Source)
at java.awt.EventQueue.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)
您遇到异常是因为您正在尝试序列化 InputStream
private InputStream imag;
InputStream 不是 Serializable
您可以在序列化时通过声明省略此字段 transient
:
private transient InputStream imag;
您的图像是未实现序列化的 InputStream。它应该是一个 byte[] 或 String link 到您的图片位置。
已编辑:您应该将业务与模型对象分开。将 insertBook 方法和数据库对象从 Book class 移动到另一个 class.
所以问题是 InputStream 以及我试图序列化对象,即 V 和数据库。
我只是添加了瞬态,例如 private transient Database database;
,然后在构造函数中声明了 database = new Database();
。因此我能够序列化它。
感谢@Ralf、@Joseph、@Diyarbakir 和@3kings 帮助我。