用户注册案例
1.需求
案例分析
- 注册的本质:就是往数据库插入1条数据
- 获得输入的所有参数
- 封装输入的所有参数
- 往数据库插入一条数据
技术:DBUtlis
创建一个QueryRunner(连接池)
update(sql,params)
如果注册成功,可以简单打印
准备
- 数据库
use day25;
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(40) DEFAULT NULL,
`password` varchar(40) DEFAULT NULL,
`address` varchar(40) DEFAULT NULL,
`nickname` varchar(40) DEFAULT NULL,
`gender` varchar(10) DEFAULT NULL,
`email` varchar(20) DEFAULT NULL,
`status` varchar(10) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8
- JacaBean
`/**
@Author Acardia
@Date 2020/8/10 17:06
@Version 1.0
*/
public class User implements Serializable {
private Integer id;
private String username;
private String password;
private String address;
private String nickname;
private String gender;
private String email;
private String status;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getNickname() {
return nickname;
}
public void setNickname(String nickname) {
this.nickname = nickname;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
", address='" + address + '\'' +
", nickname='" + nickname + '\'' +
", gender='" + gender + '\'' +
", email='" + email + '\'' +
", status='" + status + '\'' +
'}';
}
}`
-
导入jar(可以从官网下载)
驱动
连接池
DButils
Beanutils
-
index.html’
在这```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>注册页面</title>
</head>
<body>
<center><h1>用户注册</h1>
<form action="register" method="post">
姓名:<input type="text" name="username"><br>
密码:<input type="password" name="password"><br>
昵称:<input type="text" name="nickname"><br>
地址:<input type="text" name="address"><br>
邮箱:<input type="text" name="email" /><br/>
性别:<input type="radio" name="gender" value="female">女
<input type="radio" name="gender" value="male">男<br>
<input type="submit" value="注册">
</form>
</center>
</body>
</html>里插入代码片
【补充】手动修改默认的项目欢迎页(默认是web目录的index.jsp、index.html),可以在web.xml中修改。
以下配置表示,访问项目根目录,会访问index.html页面
代码实现
- RegisterServlet.java
/**
* @Author Acardia
* @Date 2020/8/10 17:10
* @Version 1.0
*/
@WebServlet("/register")
public class RegisterServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//写注册功能
//处理乱码
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html; charset=UTF-8");
//1.获取参数
//获得所有的参数
try {
Map<String, String[]> parameterMap = request.getParameterMap();
//封装到javabean
User user = new User();
BeanUtils.populate(user,parameterMap);
//使用DBUtils技术插入到数据OB中
QueryRunner queryRunner = new QueryRunner(C3P0Utils.getDataSource());
String sql = "insert into user values(null,?,?,?,?,?,?,?)";
Object[] params = {
user.getUsername(),
user.getPassword(),
user.getAddress(),
user.getNickname(),
user.getGender(),
user.getEmail(),
"0"
};
int cnt = queryRunner.update(sql,params);
//如果注册成功,简单打印一句话
if (cnt > 0){
// response.getWriter().write("注册OK");
//重定向跳转页面
response.sendRedirect("register.html");
}else{
response.getWriter().write("注册失败");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
测试:
数据库插入成功