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

📄 sbnemployeebean.java

📁 EJB 开发 EJB 开发 EJB 开发 EJB 开发 EJB 开发 EJB 开发
💻 JAVA
字号:
package employee;import javax.ejb.*;import javax.naming.*;import java.rmi.*;import java.sql.*;import java.util.*;public class SbnEmployeeBean implements SessionBean{  //设置会话上下文  SessionContext sessionContext;  public void setSessionContext(SessionContext sessionContext) {this.sessionContext = sessionContext; }  public void ejbCreate() throws CreateException {}  public void ejbRemove() {}  public void ejbActivate() {}  public void ejbPassivate() {}  public EmployeeModel insert(EmployeeModel model) throws CreateException,NamingException,RemoteException  {    try    { //利用EjbCommon类得到实体bean的home接口      EbnEmployeeHome home = (EbnEmployeeHome) EjbCommon.getLocalEJBHome( EjbCommon.E_EMPLOYEE_JNDI );      EbnEmployee remote=home.create(model);      return model;    }    catch(CreateException e) { throw e; }    catch(NamingException e) { throw e;}  }  public EmployeeModel update(EmployeeModel model) throws FinderException, NamingException, RemoteException  {    try    {//利用EjbCommon类得到实体bean的home接口      EbnEmployeeHome home = (EbnEmployeeHome) EjbCommon.getLocalEJBHome(EjbCommon.E_EMPLOYEE_JNDI);      EbnEmployee remote = home.findByPrimaryKey(model.getID());      //调用model的取属性函数,将实体bean更新      //remote.setModel(model);  //不能这么写。具体为什么,我也不知道。好像没有道理啊      remote.setName( model.getName() );      remote.setJob( model.getJob() );      remote.setHiredate( model.getHireDate() );      remote.setSal( model.getSal() );      remote.setComm( model.getComm() );      remote.setDeptid( model.getDeptID() );      return model;    }    catch (FinderException e) { throw e;}    catch (NamingException e) { throw e;}  }  public boolean del(String pk) throws RemoveException,FinderException,EJBException,NamingException ,RemoteException  {    try    {//利用EjbCommon类得到实体bean的home接口      EbnEmployeeHome home = (EbnEmployeeHome) EjbCommon.getLocalEJBHome(EjbCommon.E_EMPLOYEE_JNDI);      EbnEmployee remote = home.findByPrimaryKey(pk);      remote.remove();      return true;    }    catch (RemoveException e) {throw e;}    catch (FinderException e) {throw e;}    catch (EJBException e)    {throw e;}    catch (NamingException e) {throw e;}  }  public EmployeeModel findByPk(String pk) throws FinderException, NamingException, RemoteException  {    try    {//利用EjbCommon类得到实体bean的home接口      EbnEmployeeHome home = (EbnEmployeeHome) EjbCommon.getLocalEJBHome(EjbCommon.E_EMPLOYEE_JNDI);      EbnEmployee remote = home.findByPrimaryKey(pk);      EmployeeModel model = new EmployeeModel();      model = remote.getModel();      return model;    }    catch (FinderException e) {throw e;}    catch (NamingException e) {throw e;}  }  public boolean delSome(String[] sid) throws Exception  {    for (int i = 0 ;i<sid.length;i++)    {      try      {        del(sid[i] );      }      catch(Exception ex){throw ex;}    }    return true;  }  public boolean delBatch(String[] sid) throws NamingException, SQLException,RemoteException  {    String[] sID = null;    sID = sid;    String sql = "DELETE FROM tmp_emp WHERE ID='" ; //Note: statmachine is the table name connected with our work    String id = new String();    Connection conn = null;    Statement st = null;    boolean b = false;    try    {      conn = EjbCommon.getLocalConnection();      conn.setAutoCommit(false);      st = conn.createStatement();      for(int i = 1;i <= sID.length ;i++ )      {        id = sID[i-1];        st.execute(sql + id + "'");      }      conn.commit();      b = true;      return b;    }    catch ( NamingException e ) {throw e;}    catch(SQLException e)    {      System.err.print( "delBatch() function error: concerning with database");      try      {        conn.rollback();      }      catch(SQLException sqle) {throw sqle; }      throw e;    }    finally    {      try      {        st.close();        conn.close();      }      catch(SQLException e){ throw e; }    }  }  public java.util.ArrayList queryBySql(String strSql)  throws NamingException, SQLException,RemoteException  {    ArrayList arrayList = null;    EmployeeModel model;    Connection conn = null;    Statement st = null;    ResultSet rs = null;    try    {      conn=EjbCommon.getLocalConnection();      st=conn.createStatement();      rs=st.executeQuery("SELECT * FROM tmp_emp WHERE " + strSql); //Of course, you can change the sql statement according your condition      System.out.println("queryBySql函数的sql=" + "SELECT * FROM tmp_emp WHERE " + strSql);      arrayList = new ArrayList();      while (rs.next())      {        model=new EmployeeModel();        //从数据库里面读取值        model.setID( rs.getString( "ID" ));        model.setName( rs.getString( "NAME" ));        model.setJob( rs.getString( "JOB" ));        model.setHireDate( rs.getDate( "HIREDATE" ));        model.setSal( rs.getBigDecimal( "SAL" ));        model.setComm( rs.getString( "COMM" ));        model.setDeptID( rs.getString( "DEPTID" ));        arrayList.add(model);      }      if ( arrayList.size() <= 0 ){return null;}      else{return arrayList;}    }    catch(SQLException e)    {      System.out.println("queryBySql函数出现了SQL错误:" + e.toString() + ",将返回的ArrayList设置为Null");      throw e;    }    catch(NamingException e1) {throw e1; }    finally    {      System.out.println("queryBySql函数进入finally子块,关闭连接");      try      {        if (rs!=null)  {rs.close();}      }      catch(SQLException e){throw e;}      try      {        if (st!=null)  {st.close();}      }      catch(SQLException e){throw e;}      try      {        if (conn!=null)  conn.close();      }      catch(SQLException e) { throw e;}      //return arrayList;  //说明:不要在finally里面return,否则,捕捉到的错误不会throw出去!!!    }  }}

⌨️ 快捷键说明

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