📄 account.java
字号:
import java.sql.*;
public class Account extends People {
private double balance;
private DBOperator dbOperator;
public Account(String name, String address, String password,
String account, String pin, double balance) {
super(name, address, password, account, pin);
this.balance = balance;
dbOperator = new DBOperator();
// addAccount();
}
public Account(String account, String pin) {
this.SetAccount(account);
this.Setpin(pin);
balance = 0;
dbOperator = new DBOperator();
// addAccount();
}
// public void setBalance(double amount) throws SQLException {
// Connection conn = dbOperator.getConnection();
// try {
// PreparedStatement stat = conn
// .prepareStatement("UPDATE Accounts"
// + " SET Balance = ?"
// + " WHERE Num = ?");
// stat.setDouble(1, amount);
// stat.setString(2, this.GetAccount());
//
// balance = amount;
// } finally {
// conn.close();
// }
// }
public double getBalance() throws SQLException {
Connection conn = dbOperator.getConnection();
try {
double balance = -1;
PreparedStatement stat = conn
.prepareStatement("SELECT Balance FROM Accounts WHERE Num = ?");
stat.setString(1, this.GetAccount());
ResultSet result = stat.executeQuery();
if (result.next())
balance = result.getDouble(1);
return balance;
} finally {
conn.close();
}
}
public boolean withdraw(double amount) throws SQLException {
Connection conn = dbOperator.getConnection();
try {
if( balance<amount)
return false;
balance-=amount;
PreparedStatement stat = conn.prepareStatement("UPDATE Accounts"
+ " SET Balance = Balance - ?"
+ " WHERE Num = ?");
stat.setDouble(1, amount);
stat.setString(2, this.GetAccount());
stat.executeUpdate();
addOperator("withdraw",amount);
return true;
} finally {
conn.close();
}
}
public boolean changepin(String newpin) throws SQLException {
Connection conn = dbOperator.getConnection();
try {
PreparedStatement stat = conn.prepareStatement("SELECT * FROM Accounts WHERE Num = ?");
stat.setString(1, this.GetAccount());
// stat.executeUpdate();
ResultSet result = stat.executeQuery();
if (result.next() && this.Getpin().equals(result.getString("pin"))){
stat = conn.prepareStatement("UPDATE Accounts"
+ " SET pin = ?"
+ " WHERE Num = ?");
stat.setString(1, newpin);
stat.setString(2, this.GetAccount());
stat.executeUpdate();
conn.close();
addOperator("changepin",0);
return true;
}
conn.close();
return false;
} finally {
conn.close();
}
}
public boolean deposit(double amount) throws SQLException {
Connection conn = dbOperator.getConnection();
try {
PreparedStatement stat = conn.prepareStatement("UPDATE Accounts"
+ " SET Balance = Balance + ?"
+ " WHERE Num = ?");
stat.setDouble(1, amount);
stat.setString(2, this.GetAccount());
stat.executeUpdate();
balance+=amount;
addOperator("deposit",amount);
return true;
} finally {
conn.close();
}
}
public void addAccount(){
try{
Connection conn = dbOperator.getConnection();
PreparedStatement stat = conn.prepareStatement("SELECT * FROM Accounts WHERE Num = ?");
stat.setString(1, this.GetAccount());
ResultSet result = stat.executeQuery();
if(result.next()){
System.out.println("The Account has already exist!");
}
else{
String query = "insert into Accounts(Num, Balance,Guest,Address,pin) values ( ?, ?, ?, ?, ?)";
stat =conn.prepareStatement(query);
stat.setString(1, this.GetAccount());
stat.setDouble(2, balance);
stat.setString(3, this.GetName());
stat.setString(4, this.GetAddress());
stat.setString(5, this.Getpin());
stat.executeUpdate();
}
conn.close();
addOperator("add",0);
}
catch(SQLException ex){
System.out.println(ex);
}
}
public synchronized void delete() {
try {
// Delete the record from the database
Connection conn = dbOperator.getConnection();
PreparedStatement stat = conn.prepareStatement("SELECT * FROM Accounts WHERE Num = ?");
stat.setString(1, this.GetAccount());
ResultSet result = stat.executeQuery();
addOperator("delete",0);
result.deleteRow();
conn.close();
// refreshResultSet();
// Remove the row in the table
// tableModel.removeRow(
// listSelectionModel.getLeadSelectionIndex());
}
catch (SQLException ex) {
// jlblStatus.setText(ex.toString());
System.out.println(ex);
}
}
protected void addOperator(String operator,double money){
try{
Connection conn = dbOperator.getConnection();
PreparedStatement stat = conn.prepareStatement("insert into Operator values(?,?,?)");//???????????????????/
stat.setString(1, this.GetAccount());
stat.setString(2, operator);
stat.setDouble(3, money);
stat.executeUpdate();
conn.close();
}
catch(SQLException ex){
System.out.println(ex);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -