2011-06-24 23 views
0

IM,我怎么能做到这一点select * from student wher userName = ***与HQL如何检查是否使用表格中存在数据休眠使用我的JSP页面和MySQL休眠

,以及如何,如果该用户名在“学生”表中存在我赤?

在我的SQL

我使用

ResultSet resultat = statement.executeQuery(); 

if (resultat.next()) { ....} 

我试试这个

Session hibernateSession = MyDB.HibernateUtil.currentSession(); 
hibernateSession.find("select xxx from Etudinat where p.Nom=xxxx"); 

它给我一个名单,但

我有一个登录表单给我一个用户名和密码

我想要如果该用户名存在表中的学生学生在会话上设置用户

什么是安全的方式做到这一点

+0

什么是例外? – Olaf

+0

i'v编辑我的问题 – David

回答

3

我无法将代码粘贴到评论中。尝试以下HQL:

from Etudinat where Nom = 'xxxx' 

更好的是,传递用户名作为参数。

每个操作请求,代码片段基础上的评论:

Query q = hibernateSession.createQuery("from Etudinat where Nom = :username"); 
q.setParameter("username", xxxx);  
Etudinat e = q.uniqueResult(); 
+0

谢谢你的工作,但是(sql注入)安全吗? – David

+1

@大卫:绝对不是。这就是为什么我建议通过用户名作为参数。您可以通过参数(其中Nom =:username)告诉会话使用HQL创建Query,并将参数username设置为传递给您的DAO的值。然后你可以在查询中调用uniqueResult()方法。 – Olaf

+0

谢谢你,PLZ给我一个ligne如何使用uniqueResult()i'v到tomorow – David