accountservicehbnimpl.java

来自「用mysql和jdbc servlet 写银行管理系统」· Java 代码 · 共 122 行

JAVA
122
字号
package com.tarena.ebank.biz;

import org.hibernate.Session;

import com.tarena.ebank.persist.AccountDAO;
import com.tarena.ebank.persist.AccountDAOFactory;
import com.tarena.ebank.persist.DataException;
import com.tarena.ebank.persist.HbnUtil;

public class AccountServiceHbnImpl implements AccountService{
AccountDAO dao=AccountDAOFactory.getDAO();
	public void createAccount(Account act) {
		Session s=null;
		try {
		s=HbnUtil.getSessionFactory().openSession();
		s.beginTransaction();
		
			dao.insert(act, s);
			s.getTransaction().commit();
		} catch (DataException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			s.getTransaction().rollback();
		}
		finally{
			s.close();
		}
		
	}

	public void deposite(String actNo, double amount) {
		Session s=null;
		try{
		s=HbnUtil.getSessionFactory().openSession();
		s.beginTransaction();
		Account a=dao.findAccountByActNo(actNo, s);
		a.deposite(amount);
		s.getTransaction().commit();
		}
		catch(Exception e){
			e.printStackTrace();
			s.getTransaction().rollback();
		}finally{
			s.close();
		}
	}

	public void getBal(String actNo) {
		Session s=null;
		try{
		s=HbnUtil.getSessionFactory().openSession();
		s.beginTransaction();
		Account a=dao.findAccountByActNo(actNo, s);
		a.getBal();
		s.getTransaction().commit();
		}
		catch(Exception e){
			e.printStackTrace();
			s.getTransaction().rollback();
		}finally{
			s.close();
		}
		
	}

	public void removeAccount(String actNo) {
		Session s=null;
		try{
		s=HbnUtil.getSessionFactory().openSession();
		s.beginTransaction();
		dao.del(actNo, s);
		s.getTransaction().commit();
		}
		catch(Exception e){
			e.printStackTrace();
			s.getTransaction().rollback();
		}finally{
			s.close();
		}
		
	}

	public void transfer(String from, String to, double amount) {
		Session s=null;
		try{
		s=HbnUtil.getSessionFactory().openSession();
		s.beginTransaction();
		Account a=dao.findAccountByActNo(from, s);
		a.withdraw(amount);
		Account b=dao.findAccountByActNo(to, s);
		b.deposite(amount);
		s.getTransaction().commit();
		}
		catch(Exception e){
			e.printStackTrace();
			s.getTransaction().rollback();
		}finally{
			s.close();
		}
		
	}

	public void withdraw(String actNo, double amount) {
		Session s=null;
		try{
		s=HbnUtil.getSessionFactory().openSession();
		s.beginTransaction();
		Account a=dao.findAccountByActNo(actNo, s);
		a.withdraw(amount);
		s.getTransaction().commit();
		}
		catch(Exception e){
			e.printStackTrace();
			s.getTransaction().rollback();
		}finally{
			s.close();
		}
		
	}

}

⌨️ 快捷键说明

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