人为错误。下面的代码没有错误
主持人:请删掉。如果可能的话,请将我的 25 个名声发送给 Peter Lawrey,因为他帮助了我很多,甚至阅读了厚厚的日志数据。谢谢!
嗨,我在使用可序列化数据库时遇到问题。
关闭程序后(通过注销按钮),应该存储数据库。这是代码:
FileOutputStream f_out = null;
try {
f_out = new FileOutputStream("database.data");
ObjectOutputStream obj_out = new ObjectOutputStream(f_out);
obj_out.writeObject(database);
System.out.println("Stored!");
} catch (Exception ex) {
Logger.getLogger(BankMainFrameGUI.class.getName()).log(Level.SEVERE, null, ex);
JOptionPane.showMessageDialog(null, ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
} finally {
try {
f_out.close();
} catch (IOException ex) {
Logger.getLogger(BankMainFrameGUI.class.getName()).log(Level.SEVERE, null, ex);
JOptionPane.showMessageDialog(null, ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
}
}
new BankMainFrameGUI();
this.dispose();
然后使用以下代码在登录时恢复数据库:
try {
FileInputStream f_in = new FileInputStream("database.data");
ObjectInputStream obj_in = new ObjectInputStream(f_in);
database = (BankSystem) obj_in.readObject();
System.out.println("FOUND!");
} catch (Exception e) {
System.out.println("New database!");
database = new BankSystem();
}
数据库及其所有内部类已设置为可序列化。但是,重新启动程序后,所有数据都会丢失(客户、帐户、交易)。但是,数据库仍被检测为“找到!” 语句显示在我的编译器的 CLI 中,但不是“新数据库!” 陈述。
我使用了链表和数组列表。
非常感谢!我会升级所有可行的解决方案。
剧透:很多可能不必要的信息
如果需要,提供额外信息:
Main BankSystem() constructor + important variables:
public class BankSystem implements Serializable {
Transaction transactionManager;
Identity identityManager;
Account accountManager;
dayFirstTransaction dayFirstTransactionManager;
public BankSystem() {
transactionManager = new Transaction();
identityManager = new Identity();
accountManager = new Account();
dayFirstTransactionManager = new dayFirstTransaction();
}
...
LinkedList 节点示例:
class Transaction implements Serializable { //Database for TransactionNode
TransactionNode lastTransaction = null;
long lastTransactionId = -1;
//Unchangable location settings defined. Prevents ability to manipulate timestamp by changing computer clock. Acts as a security feature.
//Timestamps are also finalised to ensure security (prevent manipulation)
final TimeZone tz = TimeZone.getTimeZone("Singapore");
final Locale lc = new Locale("en", "SG");
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS EEEE");
final private Calendar cal = Calendar.getInstance(tz, lc);
public long newTransaction(dayFirstTransaction dFTM,
Client identity, AccountNode account, boolean action, long actionAmount,
String location, String comments) throws IOException {
//Setting timings
long timeMilli = cal.getTimeInMillis();
String timeStamp = dateFormat.format(cal.getTime());
TransactionNode previous = lastTransaction;
TransactionNode previousUserTransaction = account.GetLastTransaction();
long transactionId = ++lastTransactionId;
long initialBalance = account.getBalance();
long finalBalance;
if(action) {
finalBalance = initialBalance + actionAmount;
} else {
if(initialBalance < actionAmount) throw new IOException("There is not enough funds present in account " + account.accountId + ". \nAvailable balance: $" + account.getBalance());
finalBalance = initialBalance - actionAmount;
}
lastTransaction = new TransactionNode(previous, previousUserTransaction, timeMilli, timeStamp,
transactionId, identity, account, action, actionAmount,
initialBalance, finalBalance, location, comments);
dFTM.checkTransaction(lastTransaction);
account.setLastTransaction(lastTransaction);
return transactionId;
}
public void newTransaction (dayFirstTransaction dFTM, long timeMilli, String timeStamp,
Client identity, AccountNode account, boolean action, long actionAmount,
long initialBalance, long finalBalance, String location, String comments) throws IOException {
TransactionNode previous = lastTransaction;
TransactionNode previousUserTransaction = account.GetLastTransaction();
long transactionId = ++lastTransactionId;
lastTransaction = new TransactionNode(previous, previousUserTransaction, timeMilli, timeStamp,
transactionId, identity, account, action, actionAmount,
initialBalance, finalBalance, location, comments);
dFTM.checkTransaction(lastTransaction);
account.setLastTransaction(lastTransaction);
}
...
这个链表的节点:
class TransactionNode implements Serializable {
//Node direction pointers
final private TransactionNode previous;
final private TransactionNode previousUserTransaction;
//Declarations
//For action:
final public boolean CREDIT = true;
final public boolean DEBIT = false;
//Variables for each transaction
final public long timeMilli; //Automated. For time comparisons
final public String timeStamp;//Automated. For user reference
final public long transactionId; // Automated at Transaction LinkedList
final public Client identity;
final public AccountNode account;
final public boolean action; //true: Credit. false:Debit
final public long actionAmount;
final public long initialBalance;
final public long finalBalance;
final public String location;
final public String comments;
public TransactionNode(TransactionNode previous, TransactionNode previousUserTransaction, long timeMilli, String timeStamp,
long transactionId, Client identity, AccountNode account, boolean action, long actionAmount,
long initialBalance, long finalBalance, String location, String comments) {
this.previous = previous;
this.previousUserTransaction = previousUserTransaction;
this.timeMilli = timeMilli;
this.timeStamp = timeStamp;
this.transactionId = transactionId;
this.identity = identity;
this.account = account;
this.action = action;
this.actionAmount = actionAmount;
this.initialBalance = initialBalance;
this.finalBalance = finalBalance;
this.location = location;
this.comments = comments;
}
//Getters & setters
public TransactionNode getPreviousTransaction() {
return previous;
}
public TransactionNode getPreviousUserTransaction() {
return previousUserTransaction;
}
}