首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

hibernate中Query query=session.createQuery的有关问题

2012-12-15 
hibernate中Query querysession.createQuery的问题本是用hibernate和strut来实现登录的登录部分代码如下:

hibernate中Query query=session.createQuery的问题
本是用hibernate和strut来实现登录的
登录部分代码如下:
public boolean login(Users user) throws SQLException {
boolean flag = false;
String usernum = user.getUsernum();
String password = user.getPassword();

Session session=util.getSession();
System.out.println("aaa");
Query query=session.createQuery("from Users u where u.usernum=? and u.password");
System.out.println("bbbb");
query.setString(0, usernum);
query.setString(0, password);
List list=query.list();
if(list!=null || list.size()>0)
{
flag=true;
}
return flag;
}



提示错误500:The server encountered an internal error () that prevented it from fulfilling this request.


红色部分执行不过去,控制台只能输出aaa部分,后面的不能输出了,就是session.createQuery的问题吧?

高手帮帮忙。困扰了一天了都,郁闷啊、、、、

[最优解释]
试试看下面的语句吧:
Query query=session.createQuery("from Users u where u.usernum=? and u.password=?");
query.setParameter(0, usernum);
query.setParameter(1, password);
我这里没测试环境……我不晓得能不能通过。

query.setString(0, usernum);应该是这个方法使用不正确。

[其他解释]
createSQLQuery
[其他解释]
Query query=session.createQuery("from Users u where u.usernum=? and u.password=?");
以上部分要是改为:Query query=session.createQuery("from Users u ");是可以的,难道where部分有错误吗???
[其他解释]
Query query=session.createQuery("from Users u where u.usernum=? and u.password=?");
System.out.println("bbbb");
query.setString(1, usernum);
query.setString(2, password);



这样有问题?

那就要看属性usernum和password是否为string类型了。
[其他解释]
query.setString(0, usernum);
query.setString(0, password);你都是0,必须有问题
[其他解释]
Query query=session.createQuery("from Users u where u.usernum=:usernum and u.password=:password");
System.out.println("bbbb");
query.setString("usernum", usernum);
query.setString("password", password);



建议使用这方式。。。。

[其他解释]
不好意思。那个:其实是:
query.setString(1, usernum);
query.setString(2, password);
不过不行的。还有
Query query=session.createQuery("from Users u where u.usernum=:usernum and u.password=:password");
System.out.println("bbbb");
query.setString("usernum", usernum);
query.setString("password", password);
我也试过,可是也不行啊。。。提示同样的错误。
[其他解释]
建议用其他方式,可能是其他配置文件的原因

[其他解释]

还是不行啊、、、怎么回事呢。。。。
[其他解释]

引用:
不好意思。那个:其实是:
query.setString(1, usernum);
query.setString(2, password);
不过不行的。还有


Query query=session.createQuery("from Users u where u.usernum=:usernum and u.password=:password");
Sy……


我试过了 还是createSQLQuery 这句过不去
[其他解释]
引用:
createSQLQuery

我试过了 还是createSQLQuery 这句过不去 
[其他解释]

query.setString(0, usernum);
query.setString(1, password);
[其他解释]
占位符的问题。

List<users> users = new ArrayList<users>();
users = session.createQuery("from Users u where u.usernum=? and u.password=?")
.setParameter(0, usernum)
.setParameter(1, password)
.list();
//上面这句相当于:
//users = session.createQuery("from Users u where u.usernum=? and u.password=?").setParameter(0, usernum).setParameter(1, password).list();


另外,楼主的HQL语句的使用方式也有问题。
楼主的代码:

Query query=session.createQuery("from Users u where u.usernum='1' and u.password='abc'");
List list=query.list();

建议使用方法链的方式:

List<users> users = new ArrayList<users>();
users = session.createQuery("from Users u where u.usernum='1' and u.password='abc'")
.list();
//第一句没有分号,语句并未结束。就相当于:
//users = session.createQuery("from Users u where u.usernum='1' and u.password='abc'").list();

[其他解释]
楼主的原句:

Query query=session.createQuery("from Users u where u.usernum=? and u.password");


这句话明显有问题。最后面u.password丢了条件。
应该加个"=?"

Query query=session.createQuery("from Users u where u.usernum=? and u.password=?");

[其他解释]
引用:
占位符的问题。
Java code??1234567List<users> users = new ArrayList<users>();users = session.createQuery("from Users u where u.usernum=? and u.password=?")        .setParameter(0, usernum)      ……


谢谢,这样写就成功了。不过不是很明白,能说的明白点为什么吗?
[其他解释]
引用:
createSQLQuery


这个是可以的。。。。。谢谢
[其他解释]
引用:
谢谢,这样写就成功了。不过不是很明白,能说的明白点为什么吗?

你写个测试用的SQL嘛。
from Users u where u.usernum=? and u.password
对应的SQL语句,测试用例可以写为:
select * from Users u where u.usernum='1' and u.password
正确的SQL应为:
select * from Users u where u.usernum='1' and u.password='abc'
明显的,后面丢了一截,不是么。
[其他解释]
呵呵……我发个站内信给你。我把我的QQ号发你。CSDN上没提示,不能及时回复。


[其他解释]

引用:
引用:谢谢,这样写就成功了。不过不是很明白,能说的明白点为什么吗?
你写个测试用的SQL嘛。
from Users u where u.usernum=? and u.password
对应的SQL语句,测试用例可以写为:
select * from Users u where u.usernum='1' and u.pa……

你好,不是因为丢了后面的and u.password='abc'的那一截。只是发帖子的时候少写了那,
原来的代码是:Query query=session.createQuery("from Users u where u.usernum=? and u.password=?");
query.setString(0, usernum);
query.setString(1, password);
这个样子是不行的,不过你建议使用方法链的方式是可以的!


其实就是Query query=session.createQuery("from Users u where u.usernum=? and u.password=?");
query.setString(0, usernum);
query.setString(1, password);
这段代码和
List<users> users = new ArrayList<users>();
users = session.createQuery("from Users u where u.usernum='1' and u.password='abc'")
        .list();
的区别??麻烦在解释一下啦
[其他解释]
话说,问题解决了,就结贴吧。
分数平均分就好。不要只给几个人。
[其他解释]
引用:
话说,问题解决了,就结贴吧。
分数平均分就好。不要只给几个人。

好的,谢谢啦……………………

热点排行