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

📄 accountejb.java

📁 J2EE之EJB,RMI多个源码
💻 JAVA
字号:
import javax.ejb.*;
import javax.sql.*;
import javax.naming.*;
import java.sql.*;
import java.rmi.*;


public class AccountEJB implements EntityBean
{
  private String id;
  private String date;
  private String vc;
  private String check;
  private double amount;
  private EntityContext context;
  private Connection con;
  private String dbName = "java:comp/env/jdbc/AccountDB";
  public AccountEJB()
  {
     System.out.println("AccountEJB()");
  }
  public String getDate()
  {
    return date;
  }  
  public String getVc()
  {
    return vc;
  }
  public String getCheck()
  {
    return check;
  }
  public double getAmount()
  {
     return amount;
  }
  public void setEntityContext(EntityContext ec)
  {
    System.out.println("setEntityContext()");
     context = ec;
     try{
       makeConnection();
     }
     catch(Exception e)
     {
	System.out.println(e);
     }
  }
  public void makeConnection()throws NamingException,SQLException
  {

    InitialContext ic = new InitialContext();
    DataSource ds = (DataSource)ic.lookup(dbName);
    con = ds.getConnection();
  }
  public void unsetEntityContext()
  {
    System.out.println("unsetEntityContext()");
  }
  public void ejbActivate()
  {
  	  id = (String)context.getPrimaryKey();
     System.out.println("ejbActivate(): ID = " + id);
  }
  public void ejbPassivate()
  {
     id = null;
    System.out.println("ejbPassivate()");
  }
   public void ejbRemove(){}
/*   {
     System.out.println("ejbRemove()");
     try{
        deleteRow(id);
     }
     catch(Exception ex)  
     {
	System.out.println(ex);
     }
   }*/
   public void deleteRow(String id)throws SQLException
   {  
      String deleteStatement = "delete from Account_Holder_Transaction where cAccount_Id=?";
      PreparedStatement st = con.prepareStatement(deleteStatement);
      st.setString(1,id);
      st.executeUpdate();
      st.close();
   }
   public void ejbLoad()
   {
     System.out.println("ejbLoad()");
     try{
       loadRow();
     }
     catch(Exception ex)
     {
	System.out.println(ex);
     }
   }
   public void loadRow()throws SQLException
   {
      String selectStatement = "select dDate_of_transaction,vcParticulars,cCheck_no,mAmount from Account_Holder_Transaction where cAccount_Id=?";
      PreparedStatement st = con.prepareStatement(selectStatement);
      st.setString(1,this.id);
      ResultSet rs = st.executeQuery();
     if (rs.next())
     {
       this.date = rs.getString(1);
       this.vc = rs.getString(2);
       this.check = rs.getString(3);
       this.amount = rs.getDouble(4);
       st.close();   
     }
     else
     {
	st.close();   
        throw new NoSuchEntityException("Row for id " + id + " not found in database");
      }
      
   }
   public void ejbStore(){}
/*    {
       System.out.println("ejbStore()");
      try{
        storeRow();
      }
      catch(Exception ex)
      {
	System.out.println(ex);
       }
    }*/
   public void storeRow()throws SQLException
   {
     String updateStatement = "update Account_Holder_Transaction set dDate_of_Transaction=?,vcParticulars=?,cCheck_no=?,mAmount=? where cAccount_Id=?";
      PreparedStatement st = con.prepareStatement(updateStatement);
      st.setString(1,date);
      st.setString(2,vc);
      st.setString(3,check);
      st.setDouble(4,amount);
      st.setString(5,id);
      int rows = st.executeUpdate();
      st.close();
     if (rows == 0)
     {
       throw new EJBException("Storing row for id" + id + " failed");
     }   
   }
   public String ejbCreate(String id,String date,String vc,String check,double amount)throws RemoteException,CreateException
  {
     System.out.println("ejbCreate()");
     try{
        insertRow(id,date,vc,check,amount);
     }
     catch(Exception ex)
     {
	System.out.println(ex);
     }
     this.id = id;
     this.date = date;
     this.vc = vc;
     this.check = check;
     this.amount = amount;
     return id;
  }
  public void ejbPostCreate(String id,String date,String vc,String check,double amount)
  {
     System.out.println("ejbPostCreate()");
  }
  public void insertRow(String id,String date,String vc,String check,double amount)throws SQLException
  {
     String insertStatement = "insert into Account_Holder_Transaction values(?,?,?,?,?)";
     PreparedStatement st = con.prepareStatement(insertStatement);
    st.setString(1,id);
    st.setString(2,date);
    st.setString(3,vc);
    st.setString(4,check);
    st.setDouble(5,amount);
    st.executeUpdate();
    st.close();   
  }
  public String ejbFindByPrimaryKey(String id)throws FinderException,RemoteException
  {
    System.out.println("ejbFindByPrimaryKey()");
    boolean result = false;
    try{
      result = selectByPrimaryKey(id);
    }
    catch(Exception e)
    {
	System.out.println(e);
    } 
    if (result)
     {
	return id;
     }
     else
     {
        throw new ObjectNotFoundException("Row for id " +  id + " not found");
     }
  }
  public boolean selectByPrimaryKey(String pk)throws SQLException
  {
      String selectStatement = "select cAccount_id from Account_Holder where cAccount_id = ?";
      PreparedStatement st = con.prepareStatement(selectStatement);
      st.setString(1,pk);
      ResultSet rs = st.executeQuery();
      boolean s = rs.next();
      if (!s)
     	System.out.println("the ID not found");
      st.close();
      return s;
  }
}














⌨️ 快捷键说明

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