ejb 3初探侦


 ejb3.0  web,ejb  M(   ),    eclipse+myeclipse  (  jbuilder ejb      )


1.   ejb   jar     


   jar


[jboss    ]\client\commons-logging.jar


[jboss    ]\client\concurrent.jar


[jboss    ]\client\ejb3-persistence.jar


[jboss    ]\client\hibernate-annotations.jar


[jboss    ]\client\hibernate-client.jar


[jboss    ]\client\javassist.jar


[jboss    ]\client\jboss-annotations-ejb3.jar


[jboss    ]\client\jboss-aop-jdk50-client.jar


[jboss    ]\client\jboss-aspect-jdk50-client.jar


[jboss    ]\client\jboss-common-client.jar


[jboss    ]\client\jboss-ejb3-client.jar


[jboss    ]\client\jboss-ejb3x.jar


[jboss    ]\client\jboss-j2ee.jar


[jboss    ]\client\jboss-remoting.jar


[jboss    ]\client\jbosssx-client.jar


[jboss    ]\client\jboss-transaction-client.jar


[jboss    ]\client\jnp-client.jar


[jboss    ]\client\trove.jar


[jboss    ]\client\jbossws-client.j


[jboss    ]\client\jboss-jaxws.jar


[jboss    ]\client\xmlsec.jar


    !


     stateless session bean(     bean)


ejb3          !


2.      (   )


 :


com.sk.DAO //  


package com.sk.DAO;


 Login  


import com.sk.domain.User;
public interface Login {


public User login(String username,String password);


}


3.      (        )


 :


com.sk.DAOImp //  


  LoginBean     Login  (ejb3  :               +Bean   )


package com.sk.DAOImp;
import java.sql.CallableStatement;


import java.sql.Connection;


import java.sql.ResultSet;


import java.sql.SQLException;
import javax.ejb.Remote;


import javax.ejb.Stateless;
import com.sk.DAO.Login;


import com.sk.domain.User;


import com.sk.tools.DBOper;
@Stateless //   Bean    Bean(      Bean   ,   Server   )


@Remote({Login.class}) //    (@remote)   ,  (@local)     EJB                ApplicationServer 


public class LoginBean implements Login {
public User login(String username, String password) {


// TODO Auto-generated method stub


try {


Connection conn= DBOper.getConnection();


CallableStatement cs=conn.prepareCall("{call up_users_login(?,?)}");


cs.setString(1, username);


cs.setString(2, password);


ResultSet rs= cs.executeQuery();


while(rs.next()){


User user=new User(rs.getString("username"),rs.getString("password"),rs.getString("description"),rs.getBoolean("isadmin"));


return user;


//User         ,(  web  )


}


} catch (SQLException e) {


// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
  ,ejb3    ,    
                
public class EJBFactory {
 public static Object getEJB(String jndipath) {
 try {
 Properties props = new Properties();
//
 props.setProperty("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
 props.setProperty("java.naming.provider.url", "localhost:1099");
 props.setProperty("java.naming.factory.url.pkgs", "org.jboss.naming:org.jnp.interfaces");

 InitialContext ctx = new InitialContext(props);
 return ctx.lookup(jndipath);
 } catch (NamingException e) {
 e.printStackTrace();
 }
 return null;
 }
}
         :
Login loginAdmin= (Login)EJBFactory.getEJB("LoginBean/remote");