Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

  1. Right Click under the Project Explorer and Create a EJB project as shown.





  2. On the next screen give the project name as StatelessSessionEJB.





  3. Right Click on ejbModule and create a new Interface RegisterBeanRemote





  4. On the New Java Interface window give the package name as statelessejb and Interface name as RegisterBeanRemote





  5. Populate the RegisterBeanRemote interface with the following code.
    Code Block
    titleRegisterBeanRemote.java
    borderStylesolid
    package statelessejb;
    import javax.ejb.Remote;
    @Remote
    public interface RegisterBeanRemote 
    {
    public void register(String FirstName, String LastName, String Sex, String UserName, String Password);
    public boolean verify(String username, String password);
    }
    
  6. Right Click on StatelessSessionEJB project and create a new class RegisterBean as shown in the figure





  7. Populate the class RegisterBean with the following data
    Code Block
    titleRegisterBean.java
    borderStylesolid
    package statelessejb;
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import javax.annotation.PostConstruct;
    import javax.annotation.PreDestroy;
    import javax.annotation.Resource;
    import javax.ejb.Stateless;
    import javax.sql.DataSource;
    @Stateless
    public class RegisterBean implements RegisterBeanRemote{
    	@Resource(name="jdbc/userds")
    	private DataSource dbsource;
    	private Connection dbconnect;
    	@PostConstruct
    	public void initialize()
    	{
    		try{
    			dbconnect= dbsource.getConnection();
    		}
    		catch(Exception e)
    		{
    			e.printStackTrace();
    		}
    	}
    	public void register(String FirstName, String LastName, String Sex, String UserName, String Password)
    	{
    		try
    		{
    			String insert="INSERT INTO USERINFO (" + "FIRSTNAME," + "LASTNAME," + "SEX," + "USERNAME," + "PASSWORD)" + " VALUES(?,?,?,?,?)";
    			PreparedStatement ps=dbconnect.prepareStatement(insert);
    			ps.setString(1, FirstName);
    			ps.setString(2, LastName);
    			ps.setString(3, Sex);
    			ps.setString(4, UserName);
    			ps.setString(5, Password);
    			int rs =ps.executeUpdate();
    		}
    		catch(Exception e)
    		{
    			e.printStackTrace();
    		}
    	}
    	public boolean verify(String username, String password)
    	{
    		boolean ret=false;
    		String select="SELECT * FROM USERINFO";
    		try{
    		PreparedStatement ps=dbconnect.prepareStatement(select);
    		ResultSet rs= ps.executeQuery();
    		while(rs.next())
    		{
    			String str=rs.getString("USERNAME");
    			String str1=rs.getString("PASSWORD");
    			if (str.compareTo(username)==0)
    			{
    			if(str1.compareTo(password)==0)
    				ret=true;
    			else
    			    ret=false;
    			}
    			else
    				ret=false;
    		}
    		}
    		catch(Exception e)
    		{
    			e.printStackTrace();
    		}
    		return ret;	
    	}
    	@PreDestroy
    	public void destroy(){
    		try
    		{
    		dbconnect.close();
    		dbconnect=null;
    		}
    		catch(Exception e)
    		{
    			e.printStackTrace();
    		}
    
    	}
    }
    
    
Warning
titleWarning

Due to some limitations in Geronimo Eclipse Plugin the generated deployment plan(openejb-jar.xml) does not have the correct namespace. Replace the existing namespace as shown in the figure with the following
<openejb-jar xmlns="http://www.openejb.org/xml/ns/openejb-jar-2.2" xmlns:nam="http://geronimo.apache.org/xml/ns/naming-1.2" xmlns:pkgen="http://www.openejb.org/xml/ns/pkgen-2.0" xmlns:sec="http://geronimo.apache.org/xml/ns/security-1.2" xmlns:sys="http://geronimo.apache.org/xml/ns/deployment-1.2">

Image Added

Next we will try to understand the code

This completes the development of EJB project.

...

  1. In the EJB Project. Under META-INF, Edit openejb-jar.xml and add the following
    Code Block
    titledatasource dependency
    borderStylesolid
    <sys:dependencies>
                <sys:dependency>
                    <sys:groupId>console.dbpool</sys:groupId>
                    <sys:artifactId>jdbc%2Fuserds</sys:artifactId>
                </sys:dependency>        
    </sys:dependencies>
    
    Info
    titleWhere did the above dependencies come from??

    To make the datasource visible to EJB we need to add a dependency to the EJB deployment plan that is openejb-jar.xml. The above element can be obtained automatically from Geronimo Database Pool wizard. Select usage against the database pool jdbc/userds

  2. In the WEB Project. Under WEB-INF, Edit geronimo-web.xml and add the following
    Code Block
    titleEJB dependency
    borderStylesolid
    <sys:dependencies>
           <sys:dependency>
                <sys:groupId>default</sys:groupId>
          		<sys:artifactId>StatelessSessionEJB</sys:artifactId>
          		<sys:version>1.0</sys:version>
          		<sys:type>car</sys:type>
           </sys:dependency>        
    </sys:dependencies>
    

Deploy and Run