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

📄 statefulaccountbean.java

📁 介绍了j2ee开发常用的学习知识,如servlet,javamail,EJB等知识,并有项目源码.
💻 JAVA
字号:
/*
 * StatefulAccountBean.java
 *
 * Created on 2007年11月19日, 下午5:05
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package com.ejb;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;
import javax.ejb.PostActivate;
import javax.ejb.PrePassivate;
import javax.ejb.Remove;
import javax.ejb.Stateful;
import javax.sql.DataSource;

/**
 *
 * @author hyl
 */
@Stateful
public class StatefulAccountBean implements com.ejb.StatefulAccountRemote, com.ejb.StatefulAccountLocal {

    @Resource(name = "sample")
    private DataSource sample;
    
    private Connection connection;
    private double account=-1.0;
    /** Creates a new instance of StatefulAccountBean */
    public StatefulAccountBean() {
    }
    @PostConstruct
    @PostActivate
    public void openConnection() {
        try {
            connection = sample.getConnection();
        } catch (SQLException sqle) {
            sqle.printStackTrace();
        }
    }
    @PrePassivate
    @PreDestroy
    public void cleanup() {
        try {
            connection.close();
            connection = null;
        } catch (SQLException sqle) {
            sqle.printStackTrace();
        }
    }
    public void depoist(double amount)throws  Exception {
        //TODO implement depoist
        if(amount<0)throw new Exception("Invalid amount");
        account+=amount;
        
    }
    
    public void withdraw(double amount) throws  Exception{
        //TODO implement withdraw
      
        if(amount<0)throw new Exception("Invalid amount");
        if(account< amount)throw new Exception("not engouch money");
        account-=amount;
        
    }
    
    public double getBalance(String id) {
        //TODO implement getBalance
        if(account<0){
            if(connection!=null){
                String sql="Select amount from account where id= "+id;
                try{
                    Statement s=connection.createStatement();
                    ResultSet re= s.executeQuery(sql);
                    if(re.next())account=re.getInt("amount");
                }catch(Exception e){
                    System.out.println(e.toString());
                }
            }
            
        }
        return account;
    }
    
    @Remove
    public void updateAccount(String id) {
           if(connection!=null){
                String sql="update account set amount =? where id= "+id;
                try{
                    PreparedStatement s=connection.prepareStatement(sql);
                    s.setDouble(1,account);
                    int re= s.executeUpdate();
                   
                }catch(Exception e){
                    System.out.println(e.toString());
                }
            }
    }
    
}

⌨️ 快捷键说明

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