/*
* BankServer.Java 2006/12/26
*
* 啟動服務器
* 默認監聽端口為8848,也可以帶一個整數參數作為端口號
* 服務器啟動後處於監聽狀態,當有用戶請求服務時就開啟一個服務線程
* 出現異常拋出到控制台
*/
import Java.io.*;
import Java.Net.*;
public class BankServer
{
static int port=8848;
public static void main(String[] args)throws IOException
{
System.out.println("Starting bank system server...");
//讀取端口參數
if(args.length > 0)
port=Integer.parseInt(args[0]);
ServerSocket server=new ServerSocket(port);
System.out.println("Bank server was started");
while(true)
{
//監聽用戶請求
Socket s=server.accept();
//啟動服務線程
new ServerThread(s).start();
}
}
}
/*
* ServerThread.Java 2006/12/26
*
* 服務線程
* 負責讀取客戶端請求,通過該請求創建一個Transaction對象
* 然後調用該對象的response()方法獲得業務處理後的結果信息
* 該信息將被發送至客戶端,若該信息為null,則說明業務處理失敗
* 然後發送失敗的信息至客戶端
*/
import Java.io.*;
import Java.Net.*;
public class ServerThread extends Thread
{
private Socket s;
ServerThread(Socket s)
{
this.s=s;
}
public void run()
{
//客戶請求的業務信息
String request=null;
try{
InputStreamReader isr=new InputStreamReader(s.getInputStream());
BufferedReader br=new BufferedReader(isr);
PrintWriter pw=new PrintWriter(s.getOutputStream(),true);
//獲取客戶請求
request=br.readLine();
Transaction trans=new Transaction(request);
//向客戶端發送業務處理信息
pw.println(trans.response());
}catch(Exception e){
System.out.println("Transaction failse");
}
}
}
/*
* Transation.Java
*/
import Java.io.*;
import Java.util.*;
import Java.sql.*;
import Java.math.*;
public class Transaction
{
//用戶請求的業務信息
private String request;
//用於執行sql語句
Statement statement;
Connection connection;
public Transaction(String request)
{
this.request=request;
//數據庫驅動和url
String driver="com.MySQL.jdbc.Driver";
String url="jdbc:MySQL://localhost/bank?user=root&passWord=zhiqiao";
//讀取數據庫配置文件
try{
FileReader fr=new FileReader("config.data");
BufferedReader br=new BufferedReader(fr);
driver=br.readLine();
url=br.readLine();
fr.close();
}catch(IOException e){
System.out.println(e.toString());
System.out.println("Use the default driver and url");
}
//加載數據庫驅動,連接數據庫
try{
Class.forName(driver);
connection=DriverManager.getConnection(url);
statement=connection.createStatement();
}catch(Exception cnfe){
System.out.println(cnfe.toString());
}
}
private String check()
{
//提取帳號
String account=request.substring(request.indexOf("-")+1,request.lastIndexOf("-"));
//提取密碼
String passWord=request.substring(request.lastIndexOf("-")+1);
//用於返回帳戶信息的字符串
String balance=null;
//查詢語句
String queryStatement="SELECT Balance FROM Account where AccountNo='"+
account+"'and password='"+passWord+"'";
System.out.println("Transacting check...");
try{
//執行查詢語句,獲取結果
ResultSet resultSet=this.statement.executeQuery(queryStatement);
//如果存在該帳號則返回帳號余額
if(resultSet.next())
balance="+Your account balance is: "+resultSet.getBigDecimal("Balance");
else
balance="-Wrong account or passWord";
if(resultSet != null)
resultSet.close();
}catch(SQLException sqle){
System.out.println(sqle.toString());
}
//返回結果 帳號余額
return balance;
}
private String credit()
{
//業務處理結果信息
String result=null;
//提取帳號
String accountNo=request.substring(request.indexOf("-")+1,
request.lastIndexOf("-"));
//提取存入金額
BigDecimal money=new BigDecimal(request.substring(request.lastIndexOf("-")+1));
//更新帳戶信息的SQL語句
String updateStatement="UPDATE Account SET Balance=Balance+"+
money+" where AccountNo='"+accountNo+"'";
//添加交易記錄的SQL語句
String recordStatement="INSERT INTO Record "+
"(AccountNo,TransDate,TransMoney)VALUES("+
accountNo+","+"NOW(),"+money+")";
System.out.println("Transacting credit...");
try{
//更新帳戶信息和交易記錄
statement.executeUpdate(updateStatement);
//若更新了帳戶信息,要在交易記錄中加入記錄
statement.executeUpdate(recordStatement);
result="+Transaction complete";
}catch(SQLException sqle){
System.out.println(sqle.toString());
}
//返回業務處理信息
return result;
}
private String debit()
{
//返回交易信息成功或失敗提示
String result=null;
//提取出 帳號-密碼-取款金額
String str=request.substring(request.indexOf("-")+1);
//帳號
String accountNo=str.substring(0,str.indexOf("-"));
//密碼
String passWord=str.substring(str.indexOf("-")+1,str.lastIndexOf("-"));
//取款金額
BigDecimal money=new BigDecimal(str.substring(str.lastIndexOf("-")+1));
//更新帳戶信息語句
String updateStatement="UPDATE Account SET Balance=Balance-"+money+
" WHERE AccountNo="+accountNo;
//交易記錄語句
String recordStatement="INSERT INTO Record "+
"(AccountNo,TransDate,TransMoney)VALUES("+
accountNo+","+"NOW(),"+money.negate()+")";
//查詢帳戶余額,以判斷是否可以取款
String isEnough="SELECT Balance FROM Account WHERE AccountNo="+accountNo;
System.out.println("Transacting debit...");
try{
//判斷余額是否足以完成本次交易
//可以則更新帳號記錄同時添加交易信息記錄
ResultSet resultSet=statement.executeQuery(isEnough);
if(resultSet.next() &&
money.compareTo(resultSet.getBigDecimal("Balance"))==-1){
statement.executeUpdate(updateStatement);
statement.executeUpdate(recordStatement);
result="+Transaction complete";
}
else
result="-Wrong passWord or haven't enough money";
if(resultSet != null)
resultSet.close();
}catch(SQLException sqle){
System.out.println(sqle.toString());
}
//返回業務處理信息
return result;
}
private String virement()
{
//返回交易信息成功或失敗提示
String result=null;
//提取信息:類型-帳號-密碼-轉帳帳號-轉帳金額
//帳號-密碼-轉帳帳號-轉帳金額
String str=request.substring(request.indexOf("-")+1);
//密碼
String accountNo=str.substring(0,str.indexOf("-"));
//密碼-轉帳帳號-轉帳金額
String str1=str.substring(str.indexOf("-")+1);
//帳號
String passWord=str1.substring(0,str1.indexOf("-"));
//轉帳帳號-轉帳金額
String str2=str1.substring(str1.indexOf("-")+1);
//轉帳帳號
String transNo=str2.substring(0,str2.indexOf("-"));
//轉帳金額
BigDecimal money=new BigDecimal(str2.substring(str2.lastIndexOf("-")+1));
//更新轉出帳戶信息語句
String updateStatement1="UPDATE Account SET Balance=Balance-"+money+
" WHERE AccountNo="+accountNo+" and Password="+passWord;
//更新轉入帳戶信息語句
String updateStatement2="UPDATE Account SET Balance=Balance+"+money+
" WHERE AccountNo="+transNo;
//轉出交易記錄語句
String recordStatement1="INSERT INTO Record "+
"(AccountNo,TransDate,TransMoney,TransNo)VALUES("+
accountNo+","+"NOW(),"+money.negate()+","+transNo+")";
//轉入交易記錄語句
String recordStatement2="INSERT INTO Record "+
"(AccountNo,TransDate,TransMoney,TransNo)VALUES("+
transNo+","+"NOW(),"+money+","+accountNo+")";
//查詢帳戶余額,以判斷是否可以執行轉帳
String isEnough="SELECT Balance FROM Account WHERE AccountNo="+accountNo;
//判斷另一方帳號是否存在
String isExit="SELECT AccountNo FROM Account WHERE AccountNo="+transNo;
System.out.println("Transacting credit...");
try{
ResultSet resultSet=statement.executeQuery(isEnough);
//判斷余額是否足以完成本次交易
if(resultSet.next() &&
money.compareTo(resultSet.getBigDecimal("Balance"))==-1){
resultSet=statement.executeQuery(isExit);
//如果另一方存在
if(resultSet.next()){
statement.executeUpdate(updateStatement2);
statement.executeUpdate(recordStatement2);
statement.executeUpdate(updateStatement1);
statement.executeUpdate(recordStatement1);
result="+Transaction complete";
}
else
result="-Otherside account not exit";
}
else
result="-Wrong passWord or haven't enough money";
if(resultSet != null)
resultSet.close();
}catch(SQLException sqle){
//System.out.println(sqle.toString());
sqle.printStackTrace();
}
return result;
}
//對客戶業務的響應
public String response()
{
//返回業務處理信息
String result=null;
//提取用戶請求的業務類型
String transType=request.substring(0,request.indexOf("-"));
//根據不同的業務請求調用不同的方法
//查詢
if(transType.equals("check"));
result=check();
//存款
if(transType.equals("credit"))
result=credit();
//取款
if(transType.equals("debit"))
result=debit();
//轉帳
if(transType.equals("virement"))
result=virement();
//關閉Statement對象和Connect對象
try{
if(statement != null)
statement.close();
if(connection != null)
connection.close();
}catch(Exception e){
}
if(result==null)
result="-Service is invalid";
return result;
}
}
/*
* Transation.Java
*/
import Java.io.*;
import Java.util.*;
import Java.sql.*;
import Java.math.*;
public class Transaction
{
//用戶請求的業務信息
private String request;
//用於執行sql語句
Statement statement;
Connection connection;
public Transaction(String request)
{
this.request=request;
//數據庫驅動和url
String driver="com.MySQL.jdbc.Driver";
String url="jdbc:MySQL://localhost/bank?user=root&passWord=zhiqiao";
//讀取數據庫配置文件
try{
FileReader fr=new FileReader("config.data");
BufferedReader br=new BufferedReader(fr);
driver=br.readLine();
url=br.readLine();
fr.close();
}catch(IOException e){
System.out.println(e.toString());
System.out.println("Use the default driver and url");
}
//加載數據庫驅動,連接數據庫
try{
Class.forName(driver);
connection=DriverManager.getConnection(url);
statement=connection.createStatement();
}catch(Exception cnfe){
System.out.println(cnfe.toString());
}
}
private String check()
{
//提取帳號
String account=request.substring(request.indexOf("-")+1,request.lastIndexOf("-"));
//提取密碼
String passWord=request.substring(request.lastIndexOf("-")+1);
//用於返回帳戶信息的字符串
String balance=null;
//查詢語句
String queryStatement="SELECT Balance FROM Account where AccountNo='"+
account+"'and password='"+passWord+"'";
System.out.println("Transacting check...");
try{
//執行查詢語句,獲取結果
ResultSet resultSet=this.statement.executeQuery(queryStatement);
//如果存在該帳號則返回帳號余額
if(resultSet.next())
balance="+Your account balance is: "+resultSet.getBigDecimal("Balance");
else
balance="-Wrong account or passWord";
if(resultSet != null)
resultSet.close();
}catch(SQLException sqle){
System.out.println(sqle.toString());
}
//返回結果 帳號余額
return balance;
}
private String credit()
{
//業務處理結果信息
String result=null;
//提取帳號
String accountNo=request.substring(request.indexOf("-")+1,
request.lastIndexOf("-"));
//提取存入金額
BigDecimal money=new BigDecimal(request.substring(request.lastIndexOf("-")+1));
//更新帳戶信息的SQL語句
String updateStatement="UPDATE Account SET Balance=Balance+"+
money+" where AccountNo='"+accountNo+"'";
//添加交易記錄的SQL語句
String recordStatement="INSERT INTO Record "+
"(AccountNo,TransDate,TransMoney)VALUES("+
accountNo+","+"NOW(),"+money+")";
System.out.println("Transacting credit...");
try{
//更新帳戶信息和交易記錄
statement.executeUpdate(updateStatement);
//若更新了帳戶信息,要在交易記錄中加入記錄
statement.executeUpdate(recordStatement);
result="+Transaction complete";
}catch(SQLException sqle){
System.out.println(sqle.toString());
}
//返回業務處理信息
return result;
}
private String debit()
{
//返回交易信息成功或失敗提示
String result=null;
//提取出 帳號-密碼-取款金額
String str=request.substring(request.indexOf("-")+1);
//帳號
String accountNo=str.substring(0,str.indexOf("-"));
//密碼
String passWord=str.substring(str.indexOf("-")+1,str.lastIndexOf("-"));
//取款金額
BigDecimal money=new BigDecimal(str.substring(str.lastIndexOf("-")+1));
//更新帳戶信息語句
String updateStatement="UPDATE Account SET Balance=Balance-"+money+
" WHERE AccountNo="+accountNo;
//交易記錄語句
String recordStatement="INSERT INTO Record "+
"(AccountNo,TransDate,TransMoney)VALUES("+
accountNo+","+"NOW(),"+money.negate()+")";
//查詢帳戶余額,以判斷是否可以取款
String isEnough="SELECT Balance FROM Account WHERE AccountNo="+accountNo;
System.out.println("Transacting debit...");
try{
//判斷余額是否足以完成本次交易
//可以則更新帳號記錄同時添加交易信息記錄
ResultSet resultSet=statement.executeQuery(isEnough);
if(resultSet.next() &&
money.compareTo(resultSet.getBigDecimal("Balance"))==-1){
statement.executeUpdate(updateStatement);
statement.executeUpdate(recordStatement);
result="+Transaction complete";
}
else
result="-Wrong passWord or haven't enough money";
if(resultSet != null)
resultSet.close();
}catch(SQLException sqle){
System.out.println(sqle.toString());
}
//返回業務處理信息
return result;
}
private String virement()
{
//返回交易信息成功或失敗提示
String result=null;
//提取信息:類型-帳號-密碼-轉帳帳號-轉帳金額
//帳號-密碼-轉帳帳號-轉帳金額
String str=request.substring(request.indexOf("-")+1);
//密碼
String accountNo=str.substring(0,str.indexOf("-"));
//密碼-轉帳帳號-轉帳金額
String str1=str.substring(str.indexOf("-")+1);
//帳號
String passWord=str1.substring(0,str1.indexOf("-"));
//轉帳帳號-轉帳金額
String str2=str1.substring(str1.indexOf("-")+1);
//轉帳帳號
String transNo=str2.substring(0,str2.indexOf("-"));
//轉帳金額
BigDecimal money=new BigDecimal(str2.substring(str2.lastIndexOf("-")+1));
//更新轉出帳戶信息語句
String updateStatement1="UPDATE Account SET Balance=Balance-"+money+
" WHERE AccountNo="+accountNo+" and Password="+passWord;
//更新轉入帳戶信息語句
String updateStatement2="UPDATE Account SET Balance=Balance+"+money+
" WHERE AccountNo="+transNo;
//轉出交易記錄語句
String recordStatement1="INSERT INTO Record "+
"(AccountNo,TransDate,TransMoney,TransNo)VALUES("+
accountNo+","+"NOW(),"+money.negate()+","+transNo+")";
//轉入交易記錄語句
String recordStatement2="INSERT INTO Record "+
"(AccountNo,TransDate,TransMoney,TransNo)VALUES("+
transNo+","+"NOW(),"+money+","+accountNo+")";
//查詢帳戶余額,以判斷是否可以執行轉帳
String isEnough="SELECT Balance FROM Account WHERE AccountNo="+accountNo;
//判斷另一方帳號是否存在
String isExit="SELECT AccountNo FROM Account WHERE AccountNo="+transNo;
System.out.println("Transacting credit...");
try{
ResultSet resultSet=statement.executeQuery(isEnough);
//判斷余額是否足以完成本次交易
if(resultSet.next() &&
money.compareTo(resultSet.getBigDecimal("Balance"))==-1){
resultSet=statement.executeQuery(isExit);
//如果另一方存在
if(resultSet.next()){
statement.executeUpdate(updateStatement2);
statement.executeUpdate(recordStatement2);
statement.executeUpdate(updateStatement1);
statement.executeUpdate(recordStatement1);
result="+Transaction complete";
}
else
result="-Otherside account not exit";
}
else
result="-Wrong passWord or haven't enough money";
if(resultSet != null)
resultSet.close();
}catch(SQLException sqle){
//System.out.println(sqle.toString());
sqle.printStackTrace();
}
return result;
}
//對客戶業務的響應
public String response()
{
//返回業務處理信息
String result=null;
//提取用戶請求的業務類型
String transType=request.substring(0,request.indexOf("-"));
//根據不同的業務請求調用不同的方法
//查詢
if(transType.equals("check"));
result=check();
//存款
if(transType.equals("credit"))
result=credit();
//取款
if(transType.equals("debit"))
result=debit();
//轉帳
if(transType.equals("virement"))
result=virement();
//關閉Statement對象和Connect對象
try{
if(statement != null)
statement.close();
if(connection != null)
connection.close();
}catch(Exception e){
}
if(result==null)
result="-Service is invalid";
return result;
}
}
//另外建立一個config.data文件,內容
com.MySQL.jdbc.Driver
jdbc:MySQL://localhost/bank?user=root&passWord=2003610dk