add<用户注册>
parent
9d5d22c813
commit
d2febc02e5
|
@ -19,14 +19,12 @@ public class RegisterServlet extends HttpServlet {
|
||||||
// 从请求中获取表单参数
|
// 从请求中获取表单参数
|
||||||
String username = request.getParameter("username");
|
String username = request.getParameter("username");
|
||||||
String password = request.getParameter("password");
|
String password = request.getParameter("password");
|
||||||
|
String phone = request.getParameter("phone");
|
||||||
// 创建用户对象
|
|
||||||
User user = new User(0,username, password);
|
|
||||||
|
|
||||||
UserDao userDao = new UserDao();
|
UserDao userDao = new UserDao();
|
||||||
int save = 0;
|
int save = 0;
|
||||||
try {
|
try {
|
||||||
save = userDao.addUser("1","1");
|
save = userDao.addUser(username, phone,password);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,9 +11,9 @@ import java.util.List;
|
||||||
public class UserDao {
|
public class UserDao {
|
||||||
private final QueryRunner queryRunner = new QueryRunner(DBUtils.getDataSource());
|
private final QueryRunner queryRunner = new QueryRunner(DBUtils.getDataSource());
|
||||||
|
|
||||||
public int addUser(String name, String email) throws Exception {
|
public int addUser(String username, String phone,String password) throws Exception {
|
||||||
String sql = "INSERT INTO user (username, password) VALUES (?, ?)";
|
String sql = "INSERT INTO user (username,phone, password,admin) VALUES (?, ?,?,?)";
|
||||||
return queryRunner.update(sql, name, email);
|
return queryRunner.update(sql, username, phone,password,0);
|
||||||
}
|
}
|
||||||
|
|
||||||
public User getUserById(int id) throws Exception {
|
public User getUserById(int id) throws Exception {
|
||||||
|
|
|
@ -1,19 +1,23 @@
|
||||||
package example.model;
|
package example.model;
|
||||||
|
|
||||||
import com.sun.xml.internal.bind.v2.model.core.ID;
|
|
||||||
|
|
||||||
public class User {
|
public class User {
|
||||||
private int id;
|
private int id;
|
||||||
private String username;
|
private String username;
|
||||||
private String password;
|
private String password;
|
||||||
|
private Boolean admin;
|
||||||
|
private String phone;
|
||||||
|
|
||||||
// 构造器
|
public User() {
|
||||||
public User() {}
|
}
|
||||||
|
|
||||||
public User(int id,String username, String password) {
|
public User(int id, String username, String password, Boolean admin, String phone) {
|
||||||
this.id = id;
|
this.id = id;
|
||||||
this.username = username;
|
this.username = username;
|
||||||
this.password = password;
|
this.password = password;
|
||||||
|
this.admin = admin;
|
||||||
|
this.phone = phone;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getId() {
|
public int getId() {
|
||||||
|
@ -39,4 +43,20 @@ public class User {
|
||||||
public void setPassword(String password) {
|
public void setPassword(String password) {
|
||||||
this.password = password;
|
this.password = password;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Boolean getAdmin() {
|
||||||
|
return admin;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAdmin(Boolean admin) {
|
||||||
|
this.admin = admin;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getPhone() {
|
||||||
|
return phone;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPhone(String phone) {
|
||||||
|
this.phone = phone;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,7 +9,7 @@ public class DBUtils {
|
||||||
try {
|
try {
|
||||||
dataSource = new ComboPooledDataSource();
|
dataSource = new ComboPooledDataSource();
|
||||||
dataSource.setDriverClass("com.mysql.cj.jdbc.Driver");
|
dataSource.setDriverClass("com.mysql.cj.jdbc.Driver");
|
||||||
dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/jsp_temp_db?useSSL=false&serverTimezone=UTC");
|
dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/work?useSSL=false&serverTimezone=UTC");
|
||||||
dataSource.setUser("root");
|
dataSource.setUser("root");
|
||||||
dataSource.setPassword("root");
|
dataSource.setPassword("root");
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
|
|
@ -1,9 +1,11 @@
|
||||||
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
|
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
|
||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html>
|
<html>
|
||||||
|
|
||||||
<head>
|
<head>
|
||||||
<script src="https://www.liyansheng.top/cdn/watermark.js"></script>
|
<script src="https://www.liyansheng.top/cdn/watermark.js"></script>
|
||||||
</head>
|
</head>
|
||||||
|
|
||||||
<body>
|
<body>
|
||||||
<h2>用户注册</h2>
|
<h2>用户注册</h2>
|
||||||
<form action="register" method="post">
|
<form action="register" method="post">
|
||||||
|
@ -11,8 +13,11 @@
|
||||||
<input type="text" id="username" name="username" required><br><br>
|
<input type="text" id="username" name="username" required><br><br>
|
||||||
<label for="password">密码:</label>
|
<label for="password">密码:</label>
|
||||||
<input type="password" id="password" name="password" required><br><br>
|
<input type="password" id="password" name="password" required><br><br>
|
||||||
|
<label for="phone">电话:</label>
|
||||||
|
<input type="text" id="phone" name="phone" required><br><br>
|
||||||
<button type="submit">注册</button>
|
<button type="submit">注册</button>
|
||||||
</form>
|
</form>
|
||||||
<br>
|
<br>
|
||||||
</body>
|
</body>
|
||||||
|
|
||||||
</html>
|
</html>
|
Reference in New Issue