...
- Right Click under the Project Explorer and Create a EJB project as shown.
- On the next screen give the project name as StatelessSessionEJB.
- Right Click on ejbModule and create a new Interface RegisterBeanRemote
- On the New Java Interface window give the package name as statelessejb and Interface name as RegisterBeanRemote
- Populate the RegisterBeanRemote interface with the following code.
Code Block title RegisterBeanRemote.java borderStyle solid 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); }
- Right Click on StatelessSessionEJB project and create a new class RegisterBean as shown in the figure
- Populate the class RegisterBean with the following data
Code Block title RegisterBean.java borderStyle solid 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 | ||
---|---|---|
| ||
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 |
Next we will try to understand the code
This completes the development of EJB project.
...
- In the EJB Project. Under META-INF, Edit openejb-jar.xml and add the following
Code Block title datasource dependency borderStyle solid <sys:dependencies> <sys:dependency> <sys:groupId>console.dbpool</sys:groupId> <sys:artifactId>jdbc%2Fuserds</sys:artifactId> </sys:dependency> </sys:dependencies>
Info title Where 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
- In the WEB Project. Under WEB-INF, Edit geronimo-web.xml and add the following
Code Block title EJB dependency borderStyle solid <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>