![](/img/trans.png)
[英]Can anyone tell me why my old list has all the values which should be there only in the new list.?
[英]Can anyone tell me how should I change the query in order to compare the values from my database with strings
所以我试图从我的数据库中找到一个用户,他的名称和密码字段与作为参数给出的 n 和 p 字符串相同。 这是我的 findBy function:
public User findBy(String n, String p){
try(Session session = sessionFactory.openSession()) {
Transaction tx = null;
try {
tx = session.beginTransaction();
User uss = new User(n,p,TipUser.ANGAJAT);
User us =
session.createQuery("from User as u where u.name = " + uss.getName() + " and u.passwd = " + uss.getPasswd(), User.class)
.uniqueResult();
System.out.println(us + " was found!");
tx.commit();
// return us;
} catch (RuntimeException ex) {
if (tx != null)
tx.rollback();
}
}
return null;
}
我在主要的 function 中这样称呼它:
public static void main(String[] args) {
try {
initialize();
UserRepoORM test = new UserRepoORM();
test.findBy("Luca","luca");
}catch (Exception e){
System.err.println("Exception "+e);
e.printStackTrace();
}finally {
close();
}
}
问题是我遇到的是这个异常:
22 Apr 2021 19:16:25,995 DEBUG org.hibernate.engine.jdbc.spi.SqlExceptionHelper 126 logExceptions - could not prepare statement [select user0_.id as id1_1_, user0_.Name as name2_1_, user0_.Passwd as passwd3_1_, user0_.Tip as tip4_1_, user0_.LogIn as login5_1_ from User user0_ where user0_.Name=Luca and user0_.Passwd=luca]
org.sqlite.SQLiteException: [SQLITE_ERROR] SQL error or missing database (no such column: Luca)
当查询具有某些参数时,您必须使用Query#setParameter
而不是字符串连接来设置它们
Query<User> query = session.createQuery("from User as u where u.name=:name and u.passwd=:passwd", User.class);
query.setParameter("name", uss.getName());
query.setParameter("passwd", uss.getPasswd());
User user = query.uniqueResult();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.