【Java开源代码栏目提醒】:文章导读:在新的一年中,各位网友都进入紧张的学习或是工作阶段。网学会员整理了Java开源代码-TradeBusiness.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);
}
}
}
}