public boolean findByNameAndPassword(Person person) {
boolean result = true;
Transaction tx = null;
SessionFactory sf = this.sessionFactory;
Session session = sf.getCurrentSession();
try{
tx = session.getTransaction();
tx.begin();
Query q = session
.createQuery("select person(id) from person where username=:username and password=:password");
q.setString("username",person.getUsername());
q.setString("password", person.getPassword());
if (q.list().isEmpty())
result = false;
} tx.commit();
} catch (Exception e) {
e.printStackTrace();
if (tx != null && tx.isActive()) {
try {
// Second try catch as the rollback could fail as well
tx.rollback();
} catch (HibernateException e1) {
e1.printStackTrace();
}
}
}
return result;
}我想在 ssh上从数据库上的信息登陆 但是query感觉写的有问题
表是 person [id username password]
想返回一个result =true; 感觉query那写的不对 求指教
boolean result = true;
Transaction tx = null;
SessionFactory sf = this.sessionFactory;
Session session = sf.getCurrentSession();
try{
tx = session.getTransaction();
tx.begin();
Query q = session
.createQuery("select person(id) from person where username=:username and password=:password");
q.setString("username",person.getUsername());
q.setString("password", person.getPassword());
if (q.list().isEmpty())
result = false;
} tx.commit();
} catch (Exception e) {
e.printStackTrace();
if (tx != null && tx.isActive()) {
try {
// Second try catch as the rollback could fail as well
tx.rollback();
} catch (HibernateException e1) {
e1.printStackTrace();
}
}
}
return result;
}我想在 ssh上从数据库上的信息登陆 但是query感觉写的有问题
表是 person [id username password]
想返回一个result =true; 感觉query那写的不对 求指教
我的if写的对吗? 我想判断表中是否存在name psd 但是网上说是 这么写
if (q.list().size() != 0 ) {
result = true;
}
这句话eclipse说是返回表中元素的数量 于是这个if我感觉我写的不对`