我使用带注释的SPRING MVC创建了一个登录应用程序。我不是 能够验证从中创建的数据库的用户名和密码 phpmyadmin使用Hibernate。
以下是我的代码
package Repository;
import java.util.Iterator;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;
import LoginDao.LoginPojo;
import Repository.User;
@SuppressWarnings("deprecation")
public class UserMain {
private static SessionFactory sf;
public void setup() {
try {
sf = new AnnotationConfiguration().configure()
.addPackage("com.registration.domain")
.addAnnotatedClass
(User.class).buildSessionFactory();
} catch (Throwable e) {
System.out.println(e);
}
}
void verifyUser(String usernm,String passwd)
{
Session session = sf.openSession();
Transaction tx = null;
try{
tx = session.beginTransaction();
List<?> lst = session.createQuery("FROM LoginPojo").list();
for (Iterator<?> iterator =
lst.iterator(); iterator.hasNext();){
LoginPojo lpojo = (LoginPojo) iterator.next();
System.out.print("Username: " + lpojo.getUsernm());
System.out.print(" Password: " + lpojo.getPasswd());
if(lpojo.getUsernm().equals(usernm)&& lpojo.getPasswd().equals(passwd))
{
System.out.println("Correct username and password");
}
else
{
System.out.println("Incorrect username and password");
}
}
tx.commit();
}catch (HibernateException e) {
if (tx!=null) tx.rollback();
e.printStackTrace();
}finally {
session.close();
}
}
}