我创建了一个银行系统,用户只能存款(现在)。我的程序使用MS Access数据库。我加载了数据库;一切都运作良好。除了,当用户存款时,数据库正在点击"存款"按钮,用户必须注销并重新登录以查看更新的余额(这是在另一个框架中称为"帐户详细信息)。
有没有办法在点击按钮时刷新数据库,以便在整个程序中刷新?我也在看阵列。我可以将数据库存储在数组中并在“帐户详细信息”窗口中将其回调吗?如果是这样,我该怎么做?
我正在加载数据库的LoadData窗口
public static Account accounts[] = new Account[2];
public static Customer people[] = new Customer[2];
public static CreditCard credit[] = new CreditCard[2];
public static Mortgage mortgage[] = new Mortgage[2];
public static Transaction trans[] = new Transaction[100];
LoadData()
{
int i = 0;
try
{
//Connect to the database
Connection conn = DriverManager.getConnection("jdbc:ucanaccess:///Volumes/GREEN GRASS/JSProject/prjTIMS_JS/Bank11.mdb");
Statement st = conn.createStatement();
System.out.println("Connected database successfully.");
//Retrieve data from Salesperson table
String sqlAccount = "SELECT * FROM tblAccount";
ResultSet rsAccount = st.executeQuery(sqlAccount);
//Loop through the results to load the data into the Salesperson array
while (rsAccount.next())
{
//Retrieve by column name
int cid = rsAccount.getInt("custID");
int can = rsAccount.getInt("checkingAcctNum");
int san = rsAccount.getInt("savingsAcctNum");
int soan = rsAccount.getInt("soarAcctNum");
String ccan = rsAccount.getString("creditCardAcctNum");
int man = rsAccount.getInt("mortgageAcctNum");
int clan = rsAccount.getInt("carLoanAcctNum");
double cab = rsAccount.getDouble("checkingAcctBal");
double sab = rsAccount.getDouble("savingsAcctBal");
double soab = rsAccount.getDouble("soarAcctBal");
//Load record into the Salesperson array
accounts[i] = new Account(cid,can,san,soan,ccan,man,clan,cab,sab,soab);
i++;
}
rsAccount.close();
System.out.println("Loaded tblAccount table.");
//Retrieve data from the Customer table
String sqlCustomer = "SELECT * FROM tblCustomer";
ResultSet rsCustomer = st.executeQuery(sqlCustomer);
i = 0;
//Loop through the results to load the data into the Customer array
while (rsCustomer.next())
{
//Retrieve by column name
int cid = rsCustomer.getInt("custID");
String ti = rsCustomer.getString("title");
String fn = rsCustomer.getString("fName");
String ln = rsCustomer.getString("lName");
String un = rsCustomer.getString("username");
String pw = rsCustomer.getString("password");
int sn = rsCustomer.getInt("SSN");
String uid = rsCustomer.getString("uniqueID");
String sk = rsCustomer.getString("sightkey");
int hn = rsCustomer.getInt("houseNum");
String stn = rsCustomer.getString("streetName");
String ci = rsCustomer.getString("city");
String sta = rsCustomer.getString("state");
int zc = rsCustomer.getInt("zipcode");
String pn = rsCustomer.getString("phoneNum");
String em = rsCustomer.getString("email");
//Load record into the Customer array
people[i] = new Customer(cid,ti,fn,ln,un,pw,sn,uid,sk,hn,stn,ci,sta,zc,pn,em);
i++;
}
rsCustomer.close();
System.out.println("Loaded tblCustomer table.");
//Retrieve data from the Car table
String sqlCreditCard = "SELECT * FROM tblCreditCard";
ResultSet rsCreditCard = st.executeQuery(sqlCreditCard);
i = 0;
//Loop through the results to load the data into the Customer array
while (rsCreditCard.next())
{
//Retrieve by column name
int cid = rsCreditCard.getInt("custID");
String ld = rsCreditCard.getString("loanDate");
float air = rsCreditCard.getFloat("annualInterestRate");
double la = rsCreditCard.getDouble("loanAmount");
double bal = rsCreditCard.getDouble("balance");
int lt = rsCreditCard.getInt("loanTerm");
double cmp = rsCreditCard.getDouble("ccMonthlyPayment");
//Load record into the Car array
credit[i] = new CreditCard(cid,ld,air,la,bal,lt,cmp);
i++;
}
//Close the result sets and the connection
rsCreditCard.close();
//Retrieve data from the Car table
String sqlMortgage = "SELECT * FROM tblMortgage";
ResultSet rsMortgage = st.executeQuery(sqlMortgage);
i = 0;
//Loop through the results to load the data into the Customer array
while (rsMortgage.next())
{
//Retrieve by column name
int cid = rsMortgage.getInt("custID");
String mld = rsMortgage.getString("mloanDate");
float hv = rsMortgage.getFloat("homeValue");
double mla = rsMortgage.getDouble("mortLoanAmount");
double mair = rsMortgage.getDouble("mortAnnualInterestRate");
int mlt = rsMortgage.getInt("mortLoanTerm");
double mmp = rsMortgage.getDouble("mortMonthlyPayment");
double tp = rsMortgage.getDouble("totalPayments");
//Load record into the Car array
mortgage[i] = new Mortgage(cid,mld,hv,mla,mair,mlt,mmp,tp);
i++;
}
//Close the result sets and the connection
rsMortgage.close();
System.out.println("Loaded tblMortgage table.");
//Retrieve data from the Car table
String sqlTransaction = "SELECT * FROM tblTransaction";
ResultSet rsTransaction = st.executeQuery(sqlTransaction);
i = 0;
//Loop through the results to load the data into the Customer array
while (rsTransaction.next())
{
//Retrieve by column name
int tc = rsTransaction.getInt("transCount");
int cid = rsTransaction.getInt("custID");
int tn = rsTransaction.getInt("transNum");
String tt = rsTransaction.getString("transType");
String td = rsTransaction.getString("transDate");
//Load record into the Car array
trans[i] = new Transaction(tc,cid,tn,tt,td);
i++;
}
//Close the result sets and the connection
rsTransaction.close();
System.out.println("Loaded tblTransactions table.");
conn.close();
}
catch (Exception e)
{
System.out.println("can't get" +e);
}
}
帐户详情窗口(我知道必须在此窗口中执行某些操作才能反映在“存款”窗口中更新的余额,只是不确定如何)
public AccountDetailsWindow(int customerindex) throws IOException{
//THIS SHOWS THE BALANCES AND ACCOUNT NUMBERS
//I KNOW I NEED TO INSERT SOMETHING HERE TO REFLECT THE NEW BALANCE CHANGES, IF ANY
JLabel lblCbal = new JLabel("$"+LoadData.accounts[customerindex].getCheckingAcctBal());
lblCbal.setFont(new Font("Helvetica", Font.PLAIN, 11));
JLabel lblSbal = new JLabel("$"+LoadData.accounts[customerindex].getSavingsAcctBal());
lblSbal.setFont(new Font("Helvetica", Font.PLAIN, 11));
JLabel lblSobal = new JLabel("$"+LoadData.accounts[customerindex].getSoarAcctBal());
lblSobal.setFont(new Font("Helvetica", Font.PLAIN, 11));
JLabel lblCCbal = new JLabel("$"+LoadData.credit[customerindex].getBalance());
lblCCbal.setFont(new Font("Helvetica", Font.PLAIN, 11));
JLabel lblMbal = new JLabel("$"+LoadData.mortgage[customerindex].getTotalPayments());
lblMbal.setFont(new Font("Helvetica", Font.PLAIN, 11));
JLabel lblCacct = new JLabel("#"+LoadData.accounts[customerindex].getCheckingAcctNum());
lblCacct.setForeground(new Color(245, 135, 79));
lblCacct.setFont(new Font("Helvetica", Font.PLAIN, 11));
JLabel lblSacct = new JLabel("#"+LoadData.accounts[customerindex].getSavingsAcctNum());
lblSacct.setForeground(new Color(245, 135, 79));
lblSacct.setFont(new Font("Helvetica", Font.PLAIN, 11));
JLabel lblSoacct = new JLabel("#"+LoadData.accounts[customerindex].getSoarAcctNum());
lblSoacct.setForeground(new Color(245, 135, 79));
lblSoacct.setFont(new Font("Helvetica", Font.PLAIN, 11));
JLabel lblCCacct = new JLabel("#"+LoadData.accounts[customerindex].getCreditCardAcctNum());
lblCCacct.setForeground(new Color(245, 135, 79));
lblCCacct.setFont(new Font("Helvetica", Font.PLAIN, 11));
JLabel lblMacct = new JLabel("#"+LoadData.accounts[customerindex].getMortgageAcctNum());
lblMacct.setForeground(new Color(245, 135, 79));
lblMacct.setFont(new Font("Helvetica", Font.PLAIN, 11));
DEPOSIT窗口,其中包含作为存款更新余额的方法。
public static int i = 0;
double DTSoar, DTChecking, DTSavings, newCB, newSB, newSoB;
double checkAmount;
int transNum = 4;
String transType = "Deposit";
// Get current date time
java.util.Date date = new java.util.Date();
Timestamp ts = new Timestamp(date.getTime());
DepositCheckWindow(int customerindex) throws IOException, ParseException{
//Create Make Deposit button
JButton depositButton = new JButton ("Make Deposit");
depositButton.setFont(new Font("Helvetica", Font.BOLD, 12));
depositButton.setOpaque(false);
depositButton.setPreferredSize(new Dimension(85, 22));
depositButton.setForeground(new Color(251, 125, 24));
BufferedImage buttonIcon6 = ImageIO.read(new File("images/depositbtn.png"));
depositButton = new JButton(new ImageIcon(buttonIcon6));
depositButton.setBorderPainted(false);
depositButton.setFocusPainted(false);
depositButton.setContentAreaFilled(false);
// Add Listener to Make Deposit button
depositButton.addActionListener (new ActionListener () {
public void actionPerformed(ActionEvent e){
// Retrieve the value from the text field
checkAmount = (Double) txtAmt.getValue();
//checkAmount = Double.parseDouble(txtAmt.getText());
//DEPOSIT TO CHECKING ACCOUNT
if (toList.getSelectedItem().toString().equals("Checking Account")){
DTChecking += checkAmount;
JOptionPane.showMessageDialog(null,"You have successfully deposited " + checkAmount+" to your Checking Account.");
}
//DEPOSIT TO SAVINGS ACCOUNT
else if (toList.getSelectedItem().toString().equals("Savings Account")){
DTSavings += checkAmount;
JOptionPane.showMessageDialog(null,"You have successfully deposited $" + checkAmount+" to your Savings Account.");
}
//DEPOSIT TO SOAR ACCOUNT
else if (toList.getSelectedItem().toString().equals("Soar Account")){
DTSoar += checkAmount;
JOptionPane.showMessageDialog(null,"You have successfully deposited $" + checkAmount+" to your Soar Account.");
}
try {
calculateBalances(customerindex);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
txtAmt.setText("");
}});
//Calculate new account balances
void calculateBalances(int customerindex) throws IOException
{
//Calculate newCB - new Savings Account Balance
newCB = LoadData.accounts[customerindex].getCheckingAcctBal() + DTChecking;
//Calculate newSB - new Savings Account Balance
newSB = LoadData.accounts[customerindex].getSavingsAcctBal() + DTSavings;
//Calculate new SoB - new Soar Account Balance
newSoB = LoadData.accounts[customerindex].getSoarAcctBal() + DTSoar;
//Call methods
depositFunds(customerindex);
saveTransaction();
}
//Update account balances
boolean depositFunds(int customerindex){
int i = 0;
try
{
//Connect to the database
Connection conn = DriverManager.getConnection("jdbc:ucanaccess:///Volumes/GREEN GRASS/JSProject/prjTIMS_JS/Bank11.mdb");
Statement stmt = conn.createStatement();
System.out.println("Connected database successfully ...");
String upProfile = "UPDATE tblAccount SET checkingAcctBal = '" +newCB+ "', savingsAcctBal = '" +newSB+ "', soarAcctBal = '" +newSoB+ "' WHERE custID = '" +LoadData.accounts[i].getCustID()+ "'";
stmt.execute(upProfile);
conn.close();
JOptionPane.showMessageDialog(null, "Accounts table updated successfully");
}
catch( Exception e ){
System.out.println("Can't retrieve from database connection: "+e);
}
return true;
}
//Write to the transaction table
void saveTransaction(){
try
{
int transCount = 0;
//Connect to the database
Connection conn = DriverManager.getConnection("jdbc:ucanaccess:///Volumes/GREEN GRASS/JSProject/prjTIMS_JS/Bank11.mdb");
Statement stmt = conn.createStatement();
System.out.println("Connected database successfully ...");
String inTransaction = "insert into tblTransaction (transCount,"+ "custID,"+ "transNum," + "transType, "+ "transDate) "+ "values('"+transCount+"','"+ LoadData.accounts[i].getCustID()+"','"+transNum+"','"+transType+"','"+ts+"')";
transCount++;
stmt.execute(inTransaction);
conn.close();
JOptionPane.showMessageDialog(null, "Transaction table values inserted successfully");
}
catch( Exception e ){
System.out.println("Can't retrieve from database connection: "+e);
}
}
}