gpt4 book ai didi

java - 当我尝试使用 WHERE 子句时出现异常

转载 作者:行者123 更新时间:2023-12-01 13:05:08 24 4
gpt4 key购买 nike

尝试使用 where 子句执行查询时,出现异常:

SEVERE: Unknown column 'suhail03' in 'where clause'
org.hibernate.exception.SQLGrammarException: could not execute query
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:67)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.loader.Loader.doList(Loader.java:2223)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2104)
at org.hibernate.loader.Loader.list(Loader.java:2099)
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:378)
...

这里suhail03where子句中的条件。我哪里出错了?

 private boolean appAuthorized(String username) {

boolean hasAuthorized = false;
try {
Session session = new HVR().getSession();

String hql = "from UCred where username =" + username; // WHERE QUERY

List list = session.createQuery(hql).list();
Iterator iterator = list.iterator();
while(iterator.hasNext()) {
UCred user = (UCred)iterator.next();
if(user.getAccessToken().compareTo("null") == 0) {
hasAuthorized = false;
}else {
hasAuthorized = true;
}
}
}catch(Exception exc) {
exc.printStackTrace();
return false;
}
return hasAuthorized;

}

最佳答案

如果没有参数绑定(bind),您必须像这样连接参数字符串(错误代码):

String hql = "from UCred where username =" + "'" + username + "'";

最好使用位置参数

String hql = "from UCred ucred where ucred.username = :username";

List list = session.createQuery(hql).
.setParameter("username", username)
.list();

关于java - 当我尝试使用 WHERE 子句时出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23322787/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com