管理员注册
This commit is contained in:
@@ -5,10 +5,7 @@ import com.hellogithub.entity.userEntity;
|
|||||||
import com.hellogithub.utils.JdbcUtils;
|
import com.hellogithub.utils.JdbcUtils;
|
||||||
import org.apache.commons.dbutils.QueryRunner;
|
import org.apache.commons.dbutils.QueryRunner;
|
||||||
|
|
||||||
import java.sql.Connection;
|
import java.sql.*;
|
||||||
import java.sql.PreparedStatement;
|
|
||||||
import java.sql.ResultSet;
|
|
||||||
import java.sql.SQLException;
|
|
||||||
|
|
||||||
public class AdminDao {
|
public class AdminDao {
|
||||||
public static adminEntity AdminLogin(String adminName, String password,String loginTime) {
|
public static adminEntity AdminLogin(String adminName, String password,String loginTime) {
|
||||||
@@ -38,7 +35,9 @@ public class AdminDao {
|
|||||||
JdbcUtils.closeConnection(resultSet, preparedStatement, connection);
|
JdbcUtils.closeConnection(resultSet, preparedStatement, connection);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
* 更新管理员登录时间
|
||||||
|
* */
|
||||||
public int updateAdminLoginTime(String adminName, String loginTime){
|
public int updateAdminLoginTime(String adminName, String loginTime){
|
||||||
int num;
|
int num;
|
||||||
try {
|
try {
|
||||||
@@ -51,5 +50,66 @@ public class AdminDao {
|
|||||||
}
|
}
|
||||||
return num;
|
return num;
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
* 根据用户名查询管理员
|
||||||
|
* */
|
||||||
|
public adminEntity findByAdminName(String adminName) {
|
||||||
|
ResultSet resultSet = null;
|
||||||
|
PreparedStatement preparedStatement = null;
|
||||||
|
Connection connection = null;
|
||||||
|
try {
|
||||||
|
connection = JdbcUtils.getConnection();
|
||||||
|
String Sql = "select * from admin where adminName=? and isValid = 1";
|
||||||
|
preparedStatement = connection.prepareStatement(Sql);
|
||||||
|
preparedStatement.setString(1, adminName);
|
||||||
|
resultSet = preparedStatement.executeQuery();
|
||||||
|
if (!resultSet.next()) { // 查询不到用户数据
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
// 将db中数据 返回给客户端 查询到数据
|
||||||
|
Integer id = resultSet.getInt(1);
|
||||||
|
String dbUserName = resultSet.getString(2);
|
||||||
|
String dbUserPwd = resultSet.getString(3);
|
||||||
|
adminEntity adminEntity = new adminEntity(dbUserName, dbUserPwd);
|
||||||
|
return adminEntity;
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
return null;
|
||||||
|
} finally {
|
||||||
|
JdbcUtils.closeConnection(resultSet, preparedStatement, connection);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 管理员注册
|
||||||
|
*
|
||||||
|
* */
|
||||||
|
public int adminRegister(String adminName,String adminPwd){
|
||||||
|
Connection connection = null;
|
||||||
|
PreparedStatement preparedStatement = null;
|
||||||
|
try {
|
||||||
|
connection = JdbcUtils.getConnection();
|
||||||
|
// sql语句写的操作 ----加上事务
|
||||||
|
JdbcUtils.beginTransaction(connection); // 开启事务
|
||||||
|
String insertSql = "INSERT INTO admin (adminId, adminName, adminPassword,isValid,loginTime) VALUES (null, ?,?,1,?);";
|
||||||
|
preparedStatement = connection.prepareStatement(insertSql);
|
||||||
|
preparedStatement.setString(1,adminName);
|
||||||
|
preparedStatement.setString(2, adminPwd);
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -17,5 +17,11 @@ public class AdminService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
public adminEntity findByAdminName(String adminName){
|
||||||
|
return adminDao.findByAdminName(adminName);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int adminRegister(String adminName,String pwd){
|
||||||
|
return adminDao.adminRegister(adminName,pwd);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -0,0 +1,88 @@
|
|||||||
|
package com.hellogithub.servlet.systemAdmin;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSONObject;
|
||||||
|
import com.hellogithub.entity.adminEntity;
|
||||||
|
import com.hellogithub.entity.userEntity;
|
||||||
|
import com.hellogithub.service.AdminService;
|
||||||
|
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 org.apache.commons.lang3.StringUtils;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpSession;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.PrintWriter;
|
||||||
|
import java.util.HashMap;
|
||||||
|
@WebServlet("/adminRegister")
|
||||||
|
public class AdminRegisterServlet extends HttpServlet {
|
||||||
|
AdminService adminService=new AdminService();
|
||||||
|
@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 adminName = req.getParameter("adminname");
|
||||||
|
if (StringUtils.isEmpty(adminName)) {
|
||||||
|
setResultError("用户名称不能够是为空", writer);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
String adminPwd = req.getParameter("password");
|
||||||
|
// 参数验证
|
||||||
|
if (StringUtils.isEmpty(adminPwd)) {
|
||||||
|
setResultError("密码不能够是为空", writer);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 管理员注册之前根据用户名称查询该管理员是否存在如果不存在的情况下才可以注册 如果存在的话就无法注册
|
||||||
|
adminEntity adminEntity = adminService.findByAdminName(adminName);
|
||||||
|
if (adminEntity != null) {
|
||||||
|
setResultError("用户名: " + adminName + "已被注册!", writer);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
//用户数据注册
|
||||||
|
int register = adminService.adminRegister(adminName, adminPwd);
|
||||||
|
if (register <= 0) {
|
||||||
|
// 注册失败了 //转发到错误页面
|
||||||
|
setResultError("注册失败!", writer);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
setResultOK("注册成功!", writer);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
||||||
|
super.doDelete(req, resp);
|
||||||
|
}
|
||||||
|
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