Java完整代码校园宿舍管理系统的设计与实现 系统管理员登录系统
时间: 2023-08-14 12:04:17 浏览: 107
很抱歉,我是一个自然语言处理模型,并不具备编写完整代码的能力。不过我可以给您提供一个简单的示例代码,以便您了解如何实现系统管理员登录系统。
1. 创建一个Admin类,用于表示系统管理员的信息:
```java
public class Admin {
private int id;
private String username;
private String password;
private String email;
private String phone;
// 省略构造函数、getter和setter方法
}
```
2. 创建一个AdminDao接口,用于定义对系统管理员信息的增删改查操作:
```java
public interface AdminDao {
public Admin login(String username, String password);
public boolean addAdmin(Admin admin);
public boolean deleteAdmin(int id);
public boolean updateAdmin(Admin admin);
public Admin findAdminById(int id);
public List<Admin> findAllAdmins();
}
```
3. 创建一个AdminDaoImpl类,实现AdminDao接口中的方法:
```java
public class AdminDaoImpl implements AdminDao {
private Connection conn = null;
private PreparedStatement pstmt = null;
private ResultSet rs = null;
// 省略getConnection()方法和closeAll()方法
@Override
public Admin login(String username, String password) {
Admin admin = null;
String sql = "SELECT * FROM admin WHERE username=? AND password=?";
try {
conn = getConnection();
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, username);
pstmt.setString(2, password);
rs = pstmt.executeQuery();
if (rs.next()) {
admin = new Admin();
admin.setId(rs.getInt("id"));
admin.setUsername(rs.getString("username"));
admin.setPassword(rs.getString("password"));
admin.setEmail(rs.getString("email"));
admin.setPhone(rs.getString("phone"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
closeAll(conn, pstmt, rs);
}
return admin;
}
@Override
public boolean addAdmin(Admin admin) {
boolean flag = false;
String sql = "INSERT INTO admin(username, password, email, phone) VALUES(?, ?, ?, ?)";
try {
conn = getConnection();
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, admin.getUsername());
pstmt.setString(2, admin.getPassword());
pstmt.setString(3, admin.getEmail());
pstmt.setString(4, admin.getPhone());
int count = pstmt.executeUpdate();
if (count > 0) {
flag = true;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
closeAll(conn, pstmt, rs);
}
return flag;
}
@Override
public boolean deleteAdmin(int id) {
boolean flag = false;
String sql = "DELETE FROM admin WHERE id=?";
try {
conn = getConnection();
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, id);
int count = pstmt.executeUpdate();
if (count > 0) {
flag = true;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
closeAll(conn, pstmt, rs);
}
return flag;
}
@Override
public boolean updateAdmin(Admin admin) {
boolean flag = false;
String sql = "UPDATE admin SET username=?, password=?, email=?, phone=? WHERE id=?";
try {
conn = getConnection();
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, admin.getUsername());
pstmt.setString(2, admin.getPassword());
pstmt.setString(3, admin.getEmail());
pstmt.setString(4, admin.getPhone());
pstmt.setInt(5, admin.getId());
int count = pstmt.executeUpdate();
if (count > 0) {
flag = true;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
closeAll(conn, pstmt, rs);
}
return flag;
}
@Override
public Admin findAdminById(int id) {
Admin admin = null;
String sql = "SELECT * FROM admin WHERE id=?";
try {
conn = getConnection();
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, id);
rs = pstmt.executeQuery();
if (rs.next()) {
admin = new Admin();
admin.setId(rs.getInt("id"));
admin.setUsername(rs.getString("username"));
admin.setPassword(rs.getString("password"));
admin.setEmail(rs.getString("email"));
admin.setPhone(rs.getString("phone"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
closeAll(conn, pstmt, rs);
}
return admin;
}
@Override
public List<Admin> findAllAdmins() {
List<Admin> admins = new ArrayList<Admin>();
String sql = "SELECT * FROM admin";
try {
conn = getConnection();
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
while (rs.next()) {
Admin admin = new Admin();
admin.setId(rs.getInt("id"));
admin.setUsername(rs.getString("username"));
admin.setPassword(rs.getString("password"));
admin.setEmail(rs.getString("email"));
admin.setPhone(rs.getString("phone"));
admins.add(admin);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
closeAll(conn, pstmt, rs);
}
return admins;
}
}
```
4. 创建一个AdminController类,用于处理系统管理员登录系统的请求:
```java
@Controller
public class AdminController {
@Autowired
private AdminDao adminDao;
@RequestMapping("/login")
public String login(String username, String password, HttpSession session, Model model) {
Admin admin = adminDao.login(username, password);
if (admin != null) {
session.setAttribute("admin", admin);
return "redirect:/index";
} else {
model.addAttribute("error", "用户名或密码错误!");
return "login";
}
}
@RequestMapping("/logout")
public String logout(HttpSession session) {
session.removeAttribute("admin");
return "login";
}
}
```
5. 创建一个login.jsp页面,用于显示登录表单:
```html
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>系统管理员登录</title>
</head>
<body>
<h1>系统管理员登录</h1>
<form action="${pageContext.request.contextPath}/login" method="post">
<table>
<tr>
<td>用户名:</td>
<td><input type="text" name="username"></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="password"></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="登录"></td>
</tr>
</table>
</form>
<c:if test="${not empty error}">
<p style="color: red;">${error}</p>
</c:if>
</body>
</html>
```
6. 创建一个index.jsp页面,用于显示登录成功后的页面:
```html
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>系统管理员首页</title>
</head>
<body>
<h1>系统管理员首页</h1>
<p>欢迎,${admin.username}!</p>
<form action="${pageContext.request.contextPath}/logout" method="post">
<input type="submit" value="退出">
</form>
</body>
</html>
```
以上是一个简单的系统管理员登录系统的示例代码,您可以根据实际需求进行修改和完善。
阅读全文