⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tradebusiness.java

📁 个人网上银行系统
💻 JAVA
字号:
/*
 * Created on 2005-7-25
 *
 */
package banksystem.business;

/**
 * @author 曲本盛
 *
 * TODO Struts 项目实践
 */
import java.sql.*;
import javax.sql.*;
import banksystem.Constants;
import banksystem.GetCurrentTime;
import banksystem.PO.Trade;
import banksystem.exception.*;
public class TradeBusiness {
    /**
	 * @param dataSource 数据源.
	 * @param trade 交易信息.
	 */
	public void trade(DataSource dataSource,Trade trade)throws TranferAccountNotExistException,BalanceNotEnoughException,AccountFreezedException,SQLException{
		//该方法将会抛出在SQL服务器中定义的特定错误消息,由Delegate接收并处理为特定的Exception类
		Connection con = null;
		PreparedStatement stat = null;
		try{
			con = dataSource.getConnection();
			stat = con.prepareStatement(Constants.SQL_TRADE_INSERT);
			stat.setString(1,"");
			stat.setString(2,trade.getAccountID());
			stat.setString(3,trade.getTransferAccountID());
			stat.setString(4,trade.getType());
			stat.setDouble(5,trade.getMoney().doubleValue());
			stat.setString(6,GetCurrentTime.getCurrentTime());
			int rows = stat.executeUpdate();
			if(rows==0){
				throw new SQLException(Constants.INSERT_FAILED);
			}
		}
		catch(SQLException e){
			//按接收异常的错误号重抛特定异常
			//System.out.println(e.getErrorCode());
			//此处采取两种方案一种是根据错误号重抛特定异常,二是将SQL的错误信息直接交给界面
			if(e.getErrorCode()==Constants.ERRORCODE_ACCOUNT_NOT_EXIST){
				throw new TranferAccountNotExistException(Constants.ERRORS_TRANFER_NOT_EXIST);
			}
			else if(e.getErrorCode()==Constants.ERRORCODE_BALANCE_NOT_ENOUGH){
				throw new BalanceNotEnoughException(Constants.ERRORS_BALANCE_NOT_ENOUGH);
			}
			else if(e.getErrorCode()==Constants.ERRORCODE_ACCOUNT_FREEZED){
				throw new AccountFreezedException(Constants.ERRORS_ACCOUNT_FREEZED);
			}
			else{
				throw new SQLException(e.getMessage());
			}
		}
		finally{
			try{
				
				if(con!=null){
					con.close();
				}
				if(stat!=null){
					stat.close();
				}
			}
			catch(Exception e){
				throw new SQLException(Constants.DATABASE_CLOSE_FAILED);
			}
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -