Java 使用JSP Hibernate登录
我在使用Hibernate进行登录时遇到问题。所有代码在语法方面都是完全正确的。NetBeans告诉我我的代码没有问题。然而,当我运行web并测试登录过程时,它不会做出反应,并且地址会在doLogin上被阻塞 所有类都已正确映射。 这就是我的问题:当我试图检索数据时,我的代码在一行上被阻塞了 在doLogin servlet上(我使用NetBeans提供的模板,并在try上填写我的代码。以下简要说明:Java 使用JSP Hibernate登录,java,hibernate,jsp,login,Java,Hibernate,Jsp,Login,我在使用Hibernate进行登录时遇到问题。所有代码在语法方面都是完全正确的。NetBeans告诉我我的代码没有问题。然而,当我运行web并测试登录过程时,它不会做出反应,并且地址会在doLogin上被阻塞 所有类都已正确映射。 这就是我的问题:当我试图检索数据时,我的代码在一行上被阻塞了 在doLogin servlet上(我使用NetBeans提供的模板,并在try上填写我的代码。以下简要说明: Connect con = new Connect(); //my code is stuck
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;
}
}
您可以在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;
}
}
代码没有理由卡在您所说的位置,请检查行
new Connect()
中是否有断点您是否正确配置了数据库?没有stacktrace错误?不应该在那里阻塞…是的,我已检查了所有断点。也许其他人可以帮上忙?谢谢。。。