...
- 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
Next we will try to understand the codeCode 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"; System.out.println(select); try{ PreparedStatement ps=dbconnect.prepareStatement(select); ResultSet rs= ps.executeQuery(); while(rs.next()) { String str=(rs.getString("USERNAME")).trim(); String str1=(rs.getString("PASSWORD")).trim(); 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(); } } }
...