1、理解:
顾客
服务员===servlet层
可以把sevice那个接口,理解为,服务员可以看到的,厨师可以做的哪些菜,或者叫菜单。
厨师===service或者理解为ServiceImpl层
采购员===dao层
菜市场===数据库层
2、
针对输出为单值(0-1)
public User login_user( String account , String password ) {
User user = null;
try {
//1、建立连接
this.getConnection();
//2、设置指令
String sql = "select * from sys_user where account = ? and password = ?";
//3、获取执行SQL语句对象
this.ps = this.conn.prepareStatement(sql);
this.ps.setString(1, account);
this.ps.setString(2, password);
this.rs = this.ps.executeQuery();
if (this.rs.next()) {
user = new User();
user.setUserId(this.rs.getInt("user_id"));
user.setAccount(this.rs.getString("account"));
user.setPassword(this.rs.getString("password"));
user.setStatus(this.rs.getInt("status"));
}
return user;
} catch (Exception ex) {
ex.printStackTrace();//给开发人员使用
throw new RuntimeException("查询用户信息失败");
} finally {
this.close(this.conn, this.ps, this.rs);
}
}
这是整个 查询出来的rs,包括上面的字段名这一行,如果rs.next()存在,意思就是游标往下挪一行,不空,则挨个输出。
针对输出为多值(0-多)
public List<User> getUserList() {
List<User> userList = null;
try {
//1、建立连接
this.getConnection();
//2、设置指令
String sql = "select * from sys_user";
//3、获取执行SQL语句对象
this.ps = this.conn.prepareStatement(sql);
//4、获取结果集
this.rs = this.ps.executeQuery();
if (this.rs.next()) {//5、判断结果集是否为空
userList = new ArrayList<>();
this.rs.previous();
while (this.rs.next()) {
User user = new User();
user.setUserId(this.rs.getInt("user_id"));
user.setAccount(this.rs.getString("account"));
user.setPassword(this.rs.getString("password"));
user.setStatus(this.rs.getInt("status"));
userList.add(user);
}
}
return userList;
} catch (Exception ex) {
ex.printStackTrace();//给开发人员使用
throw new RuntimeException("getUserList()查询用户列表失败");
} finally {
this.close(this.conn, this.ps, this.rs);
}
}
这是整个 查询出来的rs,包括上面的字段名这一行,如果rs.next()存在,意思就是游标往下挪一行,然后游标往上走,这是为什么呢,因为,这是由于,下面while语句还会用游标进行判定,如果不把游标往上移动,就会缺少一行数据。
3、
//1.解决POST请求的中文乱码问题
request.setCharacterEncoding("UTF-8");