add<用户注册>

master
liyansheng 2024-12-20 23:13:19 +08:00
parent 9d5d22c813
commit d2febc02e5
5 changed files with 53 additions and 30 deletions

View File

@ -19,14 +19,12 @@ public class RegisterServlet extends HttpServlet {
// 从请求中获取表单参数
String username = request.getParameter("username");
String password = request.getParameter("password");
// 创建用户对象
User user = new User(0,username, password);
String phone = request.getParameter("phone");
UserDao userDao = new UserDao();
int save = 0;
try {
save = userDao.addUser("1","1");
save = userDao.addUser(username, phone,password);
} catch (Exception e) {
throw new RuntimeException(e);
}

View File

@ -11,9 +11,9 @@ import java.util.List;
public class UserDao {
private final QueryRunner queryRunner = new QueryRunner(DBUtils.getDataSource());
public int addUser(String name, String email) throws Exception {
String sql = "INSERT INTO user (username, password) VALUES (?, ?)";
return queryRunner.update(sql, name, email);
public int addUser(String username, String phone,String password) throws Exception {
String sql = "INSERT INTO user (username,phone, password,admin) VALUES (?, ?,?,?)";
return queryRunner.update(sql, username, phone,password,0);
}
public User getUserById(int id) throws Exception {

View File

@ -1,19 +1,23 @@
package example.model;
import com.sun.xml.internal.bind.v2.model.core.ID;
public class User {
private int id;
private String username;
private String password;
private Boolean admin;
private String phone;
// 构造器
public User() {}
public User() {
}
public User(int id,String username, String password) {
this.id= id;
public User(int id, String username, String password, Boolean admin, String phone) {
this.id = id;
this.username = username;
this.password = password;
this.admin = admin;
this.phone = phone;
}
public int getId() {
@ -39,4 +43,20 @@ public class User {
public void setPassword(String 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;
}
}

View File

@ -9,7 +9,7 @@ public class DBUtils {
try {
dataSource = new ComboPooledDataSource();
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.setPassword("root");
} catch (Exception e) {

View File

@ -1,18 +1,23 @@
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
<script src="https://www.liyansheng.top/cdn/watermark.js"></script>
</head>
<body>
<h2>用户注册</h2>
<form action="register" method="post">
<label for="username">用户名:</label>
<input type="text" id="username" name="username" required><br><br>
<label for="password">密码:</label>
<input type="password" id="password" name="password" required><br><br>
<button type="submit">注册</button>
</form>
<br>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<script src="https://www.liyansheng.top/cdn/watermark.js"></script>
</head>
<body>
<h2>用户注册</h2>
<form action="register" method="post">
<label for="username">用户名:</label>
<input type="text" id="username" name="username" required><br><br>
<label for="password">密码:</label>
<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>
</form>
<br>
</body>
</html>