2013-05-29 56 views
0

我有一個使Hibernate進行登錄過程的問題。所有代碼在語法方面都是完全正確的。 NetBeans告訴我,我的代碼沒有問題。但是,當我運行網絡,並測試登錄過程時,它沒有反應,並且地址被doLogin填滿。使用JSP登錄進入休眠狀態

所有類都已正確映射。 這是我的問題:當我嘗試檢索數據時,我的代碼在一行中被刪除。

上doLogin的servlet(我用NetBeans和剛剛在我的try代碼填充提供的模板下面是簡要:

Connect con = new Connect(); //my code is stucked on this line. 
//I've done testing where's the cause of the stuck, and this line is the cause. 
List logger = con.getLogin(username, password); 

,並要清楚:

Connect.java

public class Connect { 
    Session sesi; 

     public Connect() { 
      sesi = HibernateUtil.getSessionFactory().openSession(); 
     } 



     public List getLogin(String username, String password){ 
      return sesi.createQuery("from MsUser WHERE username = '"+username+"' and password = '"+password+"'").list(); 
     } 

    } 

而且由於查詢是HQL,這裏是MsUser類:

public class MsUser { 

    public MsUser() { 
    } 

    private int userID; 
    private String username; 
    private String firstname; 
    private String lastname; 
    private String email; 
    private String password; 
    private String gender; 
    private String address; 
    private String phone; 
    private String photo; 

    public MsUser(int userID, String username, String firstname, String lastname, String email, String password, String gender, String address, String phone, String photo) { 
     this.userID = userID; 
     this.username = username; 
     this.firstname = firstname; 
     this.lastname = lastname; 
     this.email = email; 
     this.password = password; 
     this.gender = gender; 
     this.address = address; 
     this.phone = phone; 
     this.photo = photo; 
    } 

    public String getAddress() { 
     return address; 
    } 

    public void setAddress(String address) { 
     this.address = address; 
    } 

    public String getEmail() { 
     return email; 
    } 

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

    public String getFirstname() { 
     return firstname; 
    } 

    public void setFirstname(String firstname) { 
     this.firstname = firstname; 
    } 

    public String getGender() { 
     return gender; 
    } 

    public void setGender(String gender) { 
     this.gender = gender; 
    } 

    public String getLastname() { 
     return lastname; 
    } 

    public void setLastname(String lastname) { 
     this.lastname = lastname; 
    } 

    public String getPassword() { 
     return password; 
    } 

    public void setPassword(String password) { 
     this.password = password; 
    } 

    public String getPhone() { 
     return phone; 
    } 

    public void setPhone(String phone) { 
     this.phone = phone; 
    } 

    public String getPhoto() { 
     return photo; 
    } 

    public void setPhoto(String photo) { 
     this.photo = photo; 
    } 

    public int getUserID() { 
     return userID; 
    } 

    public void setUserID(int userID) { 
     this.userID = userID; 
    } 

    public String getUsername() { 
     return username; 
    } 

    public void setUsername(String username) { 
     this.username = username; 
    } 


} 

請大家幫忙。但我懷疑Connect的構造函數是主要原因。任何人都可以建議或修理或告訴我是什麼原因導致我的。

附錄:

HibernateUtil.java 
/* 
* To change this template, choose Tools | Templates 
* and open the template in the editor. 
*/ 
package Controller; 

import org.hibernate.cfg.AnnotationConfiguration; 
import org.hibernate.SessionFactory; 

/** 
* Hibernate Utility class with a convenient method to get Session Factory 
* object. 
* 
* @author Ginanjar 
*/ 
public class HibernateUtil { 

    private static final SessionFactory sessionFactory; 

    static { 
     try { 
      // Create the SessionFactory from standard (hibernate.cfg.xml) 
      // config file. 
      sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory(); 
     } catch (Throwable ex) { 
      // Log the exception. 
      System.err.println("Initial SessionFactory creation failed." + ex); 
      throw new ExceptionInInitializerError(ex); 
     } 
    } 

    public static SessionFactory getSessionFactory() { 
     return sessionFactory; 
    } 
} 
+0

療法是沒有理由卡住在你說的地方的代碼,檢查是否有任何新連接()' –

+0

行中的斷點是否正確配置了DB? – WeMakeSoftware

+0

沒有堆棧跟蹤錯誤?不應該阻止那裏... –

回答

0

你可以試試下面的代碼在你的hibernateUtil.java文件:

SessionFactory factory = new Configuration().configure().buildSessionFactory(); 
     session = factory.openSession(); 
     String query = "select reg.username,reg.password from MsUser as reg where reg.username='" + username + "' and reg.password='" + password + "'"; 
     Query DBquery = session.createQuery(query); 
     for (Iterator it = DBquery.iterate(); it.hasNext();) {   it.next(); 
      count++; 
     } 
     System.out.println("Total rows: " + count); 
     if (count == 1) { 
      return true; 
     } else { 
      return false; 
     } 
    } 
相關問題