添加用户注册/add sql
This commit is contained in:
@@ -4,21 +4,23 @@ import com.hellogithub.entity.UserEntity;
|
||||
import com.hellogithub.utils.JdbcUtils;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.Date;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
|
||||
/**
|
||||
* 用户Dao层
|
||||
* */
|
||||
*/
|
||||
public class UserDao {
|
||||
private UserEntity userEntity=new UserEntity();
|
||||
private UserEntity userEntity = new UserEntity();
|
||||
|
||||
/**
|
||||
* 用户登录
|
||||
*
|
||||
* @param userName
|
||||
* @param password
|
||||
* */
|
||||
public UserEntity UserLogin(String userName,String password){
|
||||
*/
|
||||
public UserEntity UserLogin(String userName, String password) {
|
||||
ResultSet resultSet = null;
|
||||
PreparedStatement preparedStatement = null;
|
||||
Connection connection = null;
|
||||
@@ -45,4 +47,70 @@ public class UserDao {
|
||||
JdbcUtils.closeConnection(resultSet, preparedStatement, connection);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户注册
|
||||
*
|
||||
* @param userName
|
||||
* @param userPwd
|
||||
*/
|
||||
public int userRegister(String userName, String userPwd) {
|
||||
Connection connection = null;
|
||||
PreparedStatement preparedStatement = null;
|
||||
try {
|
||||
connection = JdbcUtils.getConnection();
|
||||
// sql语句写的操作 ----加上事务
|
||||
JdbcUtils.beginTransaction(connection); // 开启事务
|
||||
String insertSql = "INSERT INTO `user` (`userId`, `userName`, `userPassword`,`creatTime`,`isValid`) VALUES (null, ?, ?,?,1);";
|
||||
preparedStatement = connection.prepareStatement(insertSql);
|
||||
preparedStatement.setString(1, userName);
|
||||
preparedStatement.setString(2, userPwd);
|
||||
Date time= new java.sql.Date(new java.util.Date().getTime());
|
||||
preparedStatement.setDate(3, time);
|
||||
int result = preparedStatement.executeUpdate();
|
||||
// 代码执行没有问题的情况下 则会提交数据
|
||||
JdbcUtils.commitTransaction(connection); // 提交事务
|
||||
return result;
|
||||
} catch (Exception e) {
|
||||
// 程序代码报错之后 是需要回滚事务
|
||||
e.printStackTrace();
|
||||
JdbcUtils.rollBackTransaction(connection);// 回滚事务
|
||||
return 0;
|
||||
} finally {
|
||||
JdbcUtils.closeConnection(preparedStatement, connection);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过用户名查询
|
||||
*
|
||||
* @param userName
|
||||
*/
|
||||
public UserEntity findByUserName(String userName) {
|
||||
ResultSet resultSet = null;
|
||||
PreparedStatement preparedStatement = null;
|
||||
Connection connection = null;
|
||||
try {
|
||||
connection = JdbcUtils.getConnection();
|
||||
String loginSql = "select * from user where userName=?";
|
||||
preparedStatement = connection.prepareStatement(loginSql);
|
||||
preparedStatement.setString(1, userName);
|
||||
resultSet = preparedStatement.executeQuery();
|
||||
if (!resultSet.next()) { // 查询不到用户数据
|
||||
return null;
|
||||
}
|
||||
// 将db中数据 返回给客户端 查询到数据
|
||||
Integer id = resultSet.getInt(1);
|
||||
String dbUserName = resultSet.getString(2);
|
||||
String dbUserPwd = resultSet.getString(3);
|
||||
UserEntity mayiktUserEntity = new UserEntity(dbUserName, dbUserPwd);
|
||||
return mayiktUserEntity;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
} finally {
|
||||
JdbcUtils.closeConnection(resultSet, preparedStatement, connection);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -8,7 +8,27 @@ import com.hellogithub.entity.UserEntity;
|
||||
* */
|
||||
public class UserService {
|
||||
private UserDao userDao=new UserDao();
|
||||
/**
|
||||
* 用户登录
|
||||
* @param userName
|
||||
* @param password
|
||||
* */
|
||||
public UserEntity UserLogin(String userName,String password){
|
||||
return userDao.UserLogin(userName,password);
|
||||
}
|
||||
/**
|
||||
* 用户注册
|
||||
* @param userName
|
||||
* @param userPwd
|
||||
* */
|
||||
public int userRegister(String userName, String userPwd) {
|
||||
return userDao.userRegister(userName,userPwd);
|
||||
}
|
||||
/**
|
||||
* 通过用户名查询
|
||||
* @param userName
|
||||
* */
|
||||
public UserEntity findByUserName(String userName) {
|
||||
return userDao.findByUserName(userName);
|
||||
}
|
||||
}
|
||||
|
95
src/com/hellogithub/servlet/UserRegisterServlet.java
Normal file
95
src/com/hellogithub/servlet/UserRegisterServlet.java
Normal file
@@ -0,0 +1,95 @@
|
||||
package com.hellogithub.servlet;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.hellogithub.entity.UserEntity;
|
||||
import com.hellogithub.service.UserService;
|
||||
import com.hellogithub.utils.RandomValidateCode;
|
||||
import jakarta.servlet.ServletException;
|
||||
import jakarta.servlet.annotation.WebServlet;
|
||||
import jakarta.servlet.http.HttpServlet;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import jakarta.servlet.http.HttpSession;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
import java.util.HashMap;
|
||||
|
||||
@WebServlet("/userRegister")
|
||||
public class UserRegisterServlet extends HttpServlet {
|
||||
private UserService userService = new UserService();
|
||||
|
||||
@Override
|
||||
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
||||
doPost(req, resp);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
||||
resp.setCharacterEncoding("UTF-8");
|
||||
resp.setContentType("text/html; charset=utf-8");
|
||||
// 设置响应头允许ajax跨域访问
|
||||
String curOrigin = req.getHeader("Origin");
|
||||
resp.setHeader("Access-Control-Allow-Origin", curOrigin == null ? "true" : curOrigin);
|
||||
resp.setHeader("Access-Control-Allow-Credentials", "true");
|
||||
resp.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE, HEAD");
|
||||
resp.setHeader("Access-Control-Max-Age", "3600");
|
||||
resp.setHeader("Access-Control-Allow-Headers", "access-control-allow-origin, authority, content-type, version-info, X-Requested-With");
|
||||
resp.setContentType("application/json;charset=UTF-8");
|
||||
PrintWriter writer = resp.getWriter();
|
||||
String userName = req.getParameter("username");
|
||||
if (StringUtils.isEmpty(userName)) {
|
||||
setResultError("用户名称不能够是为空", writer);
|
||||
return;
|
||||
}
|
||||
String userPwd = req.getParameter("password");
|
||||
// 参数验证
|
||||
if (StringUtils.isEmpty(userPwd)) {
|
||||
//转发到错误页面
|
||||
setResultError("密码不能够是为空", writer);
|
||||
return;
|
||||
}
|
||||
// 比较图形验证码
|
||||
String userCode = req.getParameter("code"); // 用户输入的图形验证码
|
||||
// 从session中获取图形验证码
|
||||
HttpSession session = req.getSession();
|
||||
String sessionCode = (String) session.getAttribute(RandomValidateCode.RANDOMVALIDATECODE);
|
||||
if (!sessionCode.equalsIgnoreCase(userCode)) {
|
||||
setResultError("图形验证码不正确,请重新输入!", writer);
|
||||
return;
|
||||
}
|
||||
// 用户注册之前根据用户名称查询该用户是否存在如果不存在的情况下才可以注册 如果存在的话就无法注册
|
||||
UserEntity userEntity = userService.findByUserName(userName);
|
||||
if (userEntity != null) {
|
||||
setResultError("用户名: " + userName + "已被注册!", writer);
|
||||
return;
|
||||
}
|
||||
//用户数据注册
|
||||
int register = userService.userRegister(userName, userPwd);
|
||||
if (register <= 0) {
|
||||
// 注册失败了 //转发到错误页面
|
||||
setResultError("注册失败!", writer);
|
||||
return;
|
||||
}
|
||||
setResultOK("注册成功!", writer);
|
||||
}
|
||||
|
||||
public void setResult(Integer code, String msg, PrintWriter writer) {
|
||||
HashMap<String, Object> result = new HashMap<>();
|
||||
result.put("code", code);
|
||||
result.put("msg", msg);
|
||||
String jsonString = JSONObject.toJSONString(result);
|
||||
writer.println(jsonString);
|
||||
writer.close();
|
||||
}
|
||||
|
||||
public void setResultError(String msg, PrintWriter writer) {
|
||||
setResult(500, msg, writer);
|
||||
}
|
||||
|
||||
public void setResultOK(String msg, PrintWriter writer) {
|
||||
setResult(200, msg, writer);
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user