public User loadUser(User user) { String sql = "select * from user where userID = '" + user.getUserId() + "'and password = '" + user.getPassword() + "'"; System.out.println("登录的sql:"+ sql); User user1 = null; try { user1 = queryRunner.query(connection, sql, new BeanHandler<>(User.class)); } catch (SQLException e) { e.printStackTrace(); } return user1; }输入用户ID为1,密码为123456,为什么显示登录的sql:select * from user where userID = 'null'and password = '123456'
时间: 2024-03-10 18:45:16 浏览: 107
asp.net DiscuzNT登录,退出的代码
根据您提供的代码,如果传入的`User`对象的`userId`属性值为`1`,则`sql`语句应该为:
```
select * from user where userID = '1' and password = '123456'
```
但是,根据您提供的信息,`sql`语句中的`userID`字段被解析为字符串`'null'`。这可能是因为传入的`User`对象的`userId`属性值不是字符串类型,而是其他类型(例如整型)。可以将`userId`属性值转换为字符串类型,例如:
```
public User loadUser(User user) {
String sql = "select * from user where userID = '" + String.valueOf(user.getUserId()) + "'and password = '" + user.getPassword() + "'";
System.out.println("登录的sql:"+ sql);
User user1 = null;
try {
user1 = queryRunner.query(connection, sql, new BeanHandler<>(User.class));
} catch (SQLException e) {
e.printStackTrace();
}
return user1;
}
```
这样,无论传入的`User`对象的`userId`属性值是什么类型,都会被转换为字符串类型,保证`sql`语句正确解析。
阅读全文