Here goes the code, sorry for the mess. It uses a table in mysql database
called userstable2 with username, forename, surname, email, password all
varchar and username is the primary key.
Thanks...
| package com.ejb;
|
| import java.io.Serializable;
| import java.rmi.RemoteException;
| import java.sql.Connection;
| import java.sql.PreparedStatement;
| import java.sql.ResultSet;
| import java.sql.SQLException;
|
| import javax.ejb.CreateException;
| import javax.ejb.DuplicateKeyException;
| import javax.ejb.EJBException;
| import javax.ejb.EntityBean;
| import javax.ejb.EntityContext;
| import javax.ejb.FinderException;
| import javax.ejb.NoSuchEntityException;
| import javax.ejb.ObjectNotFoundException;
| import javax.ejb.RemoveException;
|
| public class UserBean implements Serializable, EntityBean {
|
| private static final String USERS_TABLE_NAME = "userstable2";
|
| private static final long serialVersionUID = 1L;
|
| private String userName;
|
| private String forename;
|
| private String surname;
|
| private String email;
|
| private String password;
|
| private Role role = new Role();
|
| private EntityContext context = null;
|
| public UserBean() {
| }
|
| public UserBean(String userName, String forename, String surname,
| String email, String password) {
| this.userName = userName;
| this.forename = forename;
| this.surname = surname;
| this.email = email;
| this.password = password;
| }
|
| /**
| * @return Returns the email.
| */
| public String getEmail() {
| return email;
| }
|
| /**
| * @param email
| * The email to set.
| */
| public void setEmail(String email) {
| this.email = email;
| }
|
| /**
| * @return Returns the forename.
| */
| public String getForename() {
| return forename;
| }
|
| /**
| * @param forename
| * The forename to set.
| */
| public void setForename(String forename) {
| this.forename = forename;
| }
|
| /**
| * @return Returns the password.
| */
| public String getPassword() {
| return password;
| }
|
| /**
| * @param password
| * The password to set.
| */
| public void setPassword(String password) {
| this.password = password;
| }
|
| /**
| * @return Returns the surname.
| */
| public String getSurname() {
| return surname;
| }
|
| /**
| * @param surname
| * The surname to set.
| */
| public void setSurname(String surname) {
| this.surname = surname;
| }
|
| /**
| * @return Returns the userName.
| */
| public String getUserName() {
| return userName;
| }
|
| /**
| * @param userName
| * The userName to set.
| */
| public void setUserName(String userName) {
| this.userName = userName;
| }
|
| /**
| * Add the role to this user.
| *
| * @param role
| */
| public void addRole(String roleName) {
| role.addRole(roleName);
| }
|
| /**
| * User's roles include this role?
| *
| * @param role
| * @return
| */
| public boolean isUserInRole(String roleName) {
| return role.isInRole(roleName);
| }
|
| /**
| * Get the priority role name.
| *
| * @return
| */
| public String getRoleName() {
| return role.getRoleName();
| }
|
| public String ejbCreate(String userName, String forename, String
surname,
| String email, String password) throws CreateException {
|
| // get the connection for the db
| Connection conn = UserDBUtil.getConnection();
|
| PreparedStatement stmtSelect = null;
| PreparedStatement stmtInsert = null;
| ResultSet rs = null;
|
| try {
| // check for existing user
| StringBuffer sbSelect = new StringBuffer();
| sbSelect.append("SELECT username FROM ");
| sbSelect.append(USERS_TABLE_NAME);
| sbSelect.append(" WHERE username=?");
| stmtSelect = conn.prepareStatement(sbSelect.toString());
| stmtSelect.setString(1, userName);
|
| rs = stmtSelect.executeQuery();
| if (rs.next()) {
| throw new DuplicateKeyException("ejbCreate:
Existing user.");
| }
|
| StringBuffer sbInsert = new StringBuffer();
| sbInsert.append("INSERT INTO ");
| sbInsert.append(USERS_TABLE_NAME);
| sbInsert
| .append(" ( username, forename,
surname, email, password ) ");
| sbInsert.append(" VALUES (?, ?, ?, ?, ?) ");
|
| stmtInsert = conn.prepareStatement(sbInsert.toString());
| stmtInsert.setString(1, userName);
| stmtInsert.setString(2, forename);
| stmtInsert.setString(3, surname);
| stmtInsert.setString(4, email);
| stmtInsert.setString(5, password);
|
| int rows = stmtInsert.executeUpdate();
|
| // there must be only 1 row affected
| if (rows != 1) {
| throw new CreateException("Failed to create
user");
| }
|
| // init variables
| this.userName = userName;
| this.forename = forename;
| this.surname = surname;
| this.email = email;
| this.password = password;
|
| } catch (SQLException e) {
| throw new EJBException("ejbCreate: " + e.getMessage());
| } finally {
| UserDBUtil.closeStatement(stmtSelect);
| UserDBUtil.closeStatement(stmtInsert);
| UserDBUtil.closeResultSet(rs);
| UserDBUtil.closeConnection(conn);
| }
|
| return userName;
| }
|
| public void ejbPostCreate(String userName, String forename, String
surname,
| String email, String password) throws CreateException {
| }
|
| public String ejbFindByPrimaryKey(String primaryKey) throws
FinderException {
| // get the connection for the db
| Connection conn = UserDBUtil.getConnection();
|
| PreparedStatement stmtSelect = null;
| ResultSet rs = null;
|
| try {
| StringBuffer sbSelect = new StringBuffer();
| sbSelect.append("SELECT username FROM ");
| sbSelect.append(USERS_TABLE_NAME);
| sbSelect.append(" WHERE username=?");
| stmtSelect = conn.prepareStatement(sbSelect.toString());
| stmtSelect.setString(1, primaryKey);
|
| System.out.println("Executing select query with key= "
+ primaryKey);
|
| rs = stmtSelect.executeQuery();
| if (!rs.next()) {
| throw new ObjectNotFoundException(
| "ejbFindByPrimaryKey: User not
found.");
| } else{
| System.out.println(rs.getString("username"));
| }
|
| return primaryKey;
|
| } catch (SQLException e) {
| throw new EJBException("ejbFindByPrimaryKey: " +
e.getMessage());
| } finally {
| UserDBUtil.closeStatement(stmtSelect);
| UserDBUtil.closeResultSet(rs);
| UserDBUtil.closeConnection(conn);
| }
| }
|
| public void ejbActivate() throws EJBException, RemoteException {
| }
|
| public void ejbPassivate() throws EJBException, RemoteException {
| }
|
| public void ejbLoad() throws EJBException, RemoteException {
| // get the connection for the db
| Connection conn = UserDBUtil.getConnection();
|
| ResultSet rs = null;
| PreparedStatement stmtSelect = null;
|
| // the primary key
| String userName = (String) context.getPrimaryKey();
|
| try {
|
| StringBuffer sbSelect = new StringBuffer();
|
| sbSelect.append("SELECT ");
| sbSelect.append("username, forename, surname, email,
password FROM ");
| sbSelect.append(USERS_TABLE_NAME);
| sbSelect.append(" WHERE ");
| sbSelect.append("username");
| sbSelect.append("= ?");
|
| stmtSelect = conn.prepareStatement(sbSelect.toString());
| stmtSelect.setString(1, userName);
|
| rs = stmtSelect.executeQuery();
|
| boolean beanFound = false;
|
| if (rs.next()) {
| this.userName = rs.getString("username");
| this.forename = rs.getString("forename");
| this.surname = rs.getString("surname");
| this.email = rs.getString("email");
| this.password = rs.getString("password");
| beanFound = true;
| }
|
| // the bean with the given key value could not be found
| if (!beanFound) {
| throw new NoSuchEntityException("ejbLoad: " +
"User not found.");
| }
|
| } catch (SQLException e) {
| throw new EJBException("ejbLoad: " + e.getMessage());
| } finally {
| UserDBUtil.closeStatement(stmtSelect);
| UserDBUtil.closeResultSet(rs);
| UserDBUtil.closeConnection(conn);
| }
| }
|
| public void ejbRemove() throws RemoveException, EJBException,
| RemoteException {
| // get the connection for the db
| Connection conn = UserDBUtil.getConnection();
|
| PreparedStatement stmtRemove = null;
|
| try {
| StringBuffer sbRemove = new StringBuffer();
| sbRemove.append("DELETE FROM " + USERS_TABLE_NAME + "
WHERE username=?");
|
| stmtRemove = conn.prepareStatement(sbRemove.toString());
| stmtRemove.setString(1, userName);
|
| if (stmtRemove.executeUpdate() != 1) {
| throw new EJBException("ejbRemove: Failed to
remove user.");
| }
|
| } catch (SQLException e) {
| throw new EJBException("ejbRemove: " + e.getMessage());
| }
|
| }
|
| public void ejbStore() throws EJBException, RemoteException {
| // get the connection for the db
| Connection conn = UserDBUtil.getConnection();
|
| PreparedStatement stmtUpdate = null;
|
| try {
| StringBuffer sbUpdate = new StringBuffer();
| sbUpdate.append("UPDATE " + USERS_TABLE_NAME);
| sbUpdate
| .append(" SET forename=?, surname=?,
email=?, password=? WHERE username=?");
| stmtUpdate.setString(1, forename);
| stmtUpdate.setString(2, surname);
| stmtUpdate.setString(3, email);
| stmtUpdate.setString(4, password);
| stmtUpdate.setString(5, userName);
|
| stmtUpdate = conn.prepareStatement(sbUpdate.toString());
|
| if (stmtUpdate.executeUpdate() != 1) {
| throw new NoSuchEntityException("ejbStore: User
not found.");
| }
|
| } catch (SQLException e) {
| throw new EJBException("ejbStore: " + e.getMessage());
| } finally {
| UserDBUtil.closeStatement(stmtUpdate);
| UserDBUtil.closeConnection(conn);
| }
| }
|
| public void unsetEntityContext() throws EJBException, RemoteException {
| context = null;
| }
|
| public void setEntityContext(EntityContext arg0) throws EJBException,
| RemoteException {
| this.context = arg0;
| }
|
| }
|
|
View the original post :
http://www.jboss.com/index.html?module=bb&op=viewtopic&p=3899241#3899241
Reply to the post :
http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=3899241
-------------------------------------------------------
This SF.Net email is sponsored by:
Power Architecture Resource Center: Free content, downloads, discussions,
and more. http://solutions.newsforge.com/ibmarch.tmpl
_______________________________________________
JBoss-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/jboss-user