想要在添加到数据库之前检查电子邮件ID,但方法无法正常工作

时间:2015-05-17 07:36:49

标签: java xml hibernate email hql

我正在将用户添加到数据库中,其中用户应该具有唯一的电子邮件地址我制作了验证用户电子邮件ID的方法,但是当我为新用户使用相同的电子邮件ID时它无法正常工作给我成功添加的输出,我使用的是hibernate框架,并使用hql从数据库中获取电子邮件地址,

服务器代码是: -

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
    try {
        String firstName=request.getParameter("firstname");
        String lastName=request.getParameter("lastname");
        String email=request.getParameter("email");
        int gender=Integer.parseInt(request.getParameter("gender"));           
        String password=request.getParameter("password");

        UserTable user=new UserTable(firstName, lastName, email, gender, password);

        UserService as=new UserService();
        String result=as.addNewUser(user);
        RequestDispatcher rd=request.getRequestDispatcher("register.jsp");
        request.setAttribute("result", result);
        rd.forward(request, response);
    } finally {            
        out.close();
    }
}

Javabean构造函数:

public UserTable( String firstName, String lastName, String email, int gender, String password) {     
    this.firstName = firstName;
    this.lastName = lastName;
    this.email = email;
    this.gender = gender;
    this.password = password;
}

电子邮件ID的Getter Setter

public String getEmail() {
    return this.email;
}

public void setEmail(String email) {
    this.email = email;
}

添加用户的服务方法:

public String addNewUser(UserTable user){
    String errMsg="User with this email already exist";
    String scsMsg="User Successfully Registered.";
    if (isUserExist(user)) return errMsg;
    Session session=null;
    Transaction tx=null;
    try{
        session=HibernateUtil.getSessionFactory().openSession();            
        tx=session.getTransaction();
        tx.begin();
        session.save(user);
        tx.commit();
        return scsMsg;
    }catch(Exception e){
        if(tx != null){
            tx.rollback();
        }
        return e.getMessage();
    }
    finally{
        session.close();
    }
}

检查电子邮件ID是否已存在: -

public boolean isUserExist(UserTable user){
    Session s=HibernateUtil.getSessionFactory().openSession();
    Transaction tx=null;
    boolean result=false;
    try{
        tx=s.getTransaction();
        tx.begin();
        Query query=s.createQuery("from USER_TABLE where EMAIL='"+user.getEmail()+"'");
        UserTable u=(UserTable)query.uniqueResult();            
        tx.commit();
        if(u != null) return true;
    }
    catch(Exception e){
        if (tx != null) {
            tx.rollback();
        }
        e.printStackTrace();
    }
    finally{
        s.close();
    }
    return result;
}

用户表Hibernate映射XML文件:

<hibernate-mapping>
<class name="model.UserTable" table="USER_TABLE" schema="J2EEDB">
    <id name="userId" type="long">
        <column name="USER_ID" precision="10" scale="0" />
        <generator class="sequence">
            <param name="sequence">USER_ID_SEQ</param>
        </generator>
    </id>
    <property name="firstName" type="string">
        <column name="FIRST_NAME" length="20" not-null="true" />
    </property>
    <property name="lastName" type="string">
        <column name="LAST_NAME" length="20" not-null="true" />
    </property>
    <property name="email" type="string">
        <column name="EMAIL" length="30" not-null="true" />
    </property>
    <property name="gender" type="integer">
        <column name="GENDER" precision="1" scale="0" not-null="true" />
    </property>
    <property name="uoiId" type="java.lang.Integer">
        <column name="UOI_ID" precision="5" scale="0" />
    </property>
    <property name="password" type="string">
        <column name="PASSWORD" length="20" not-null="true" />
    </property>     
</class>

0 个答案:

没有答案