JDBC+三层结构+jsp简单案例
时间: 2023-09-15 13:20:06 浏览: 39
好的,下面是一个简单的JDBC+三层结构+jsp的示例:
1. 创建数据库表
首先,我们创建一个名为“user”的表,包含id、username和password三个字段。
2. 编写JavaBean层
在JavaBean层,我们定义一个User类,包含id、username和password三个属性,以及相应的getter和setter方法。
```java
public class User {
private int id;
private String username;
private String password;
// getter and setter methods
}
```
3. 编写DAO层
在DAO层,我们使用JDBC连接数据库,并编写相应的增删改查方法。
```java
public class UserDao {
// JDBC连接参数
private String url = "jdbc:mysql://localhost:3306/test";
private String username = "root";
private String password = "root";
// 查询用户信息
public User getUserById(int id) {
User user = null;
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
conn = DriverManager.getConnection(url, username, password);
String sql = "SELECT * FROM user WHERE id = ?";
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, id);
rs = pstmt.executeQuery();
if (rs.next()) {
user = new User();
user.setId(rs.getInt("id"));
user.setUsername(rs.getString("username"));
user.setPassword(rs.getString("password"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (rs != null) {
rs.close();
}
if (pstmt != null) {
pstmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
return user;
}
// 添加用户信息
public int addUser(User user) {
int result = 0;
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = DriverManager.getConnection(url, username, password);
String sql = "INSERT INTO user(username, password) VALUES (?, ?)";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, user.getUsername());
pstmt.setString(2, user.getPassword());
result = pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (pstmt != null) {
pstmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
return result;
}
// 更新用户信息
public int updateUser(User user) {
int result = 0;
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = DriverManager.getConnection(url, username, password);
String sql = "UPDATE user SET username = ?, password = ? WHERE id = ?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, user.getUsername());
pstmt.setString(2, user.getPassword());
pstmt.setInt(3, user.getId());
result = pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (pstmt != null) {
pstmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
return result;
}
// 删除用户信息
public int deleteUser(int id) {
int result = 0;
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = DriverManager.getConnection(url, username, password);
String sql = "DELETE FROM user WHERE id = ?";
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, id);
result = pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (pstmt != null) {
pstmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
return result;
}
}
```
4. 编写Service层
在Service层,我们调用DAO层的方法,并进行相应的业务逻辑处理。
```java
public class UserService {
private UserDao userDao = new UserDao();
// 获取用户信息
public User getUserById(int id) {
return userDao.getUserById(id);
}
// 添加用户信息
public int addUser(User user) {
return userDao.addUser(user);
}
// 更新用户信息
public int updateUser(User user) {
return userDao.updateUser(user);
}
// 删除用户信息
public int deleteUser(int id) {
return userDao.deleteUser(id);
}
}
```
5. 编写Controller层
在Controller层,我们通过JSP页面获取用户输入的数据,并调用Service层的方法进行相应的操作。
```java
public class UserController extends HttpServlet {
private UserService userService = new UserService();
// 处理GET请求
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
// 处理POST请求
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 设置字符编码
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
// 获取操作类型
String action = request.getParameter("action");
if ("add".equals(action)) {
// 添加用户信息
String username = request.getParameter("username");
String password = request.getParameter("password");
User user = new User();
user.setUsername(username);
user.setPassword(password);
int result = userService.addUser(user);
if (result > 0) {
response.sendRedirect("list.jsp");
} else {
response.getWriter().println("添加用户信息失败!");
}
} else if ("delete".equals(action)) {
// 删除用户信息
int id = Integer.parseInt(request.getParameter("id"));
int result = userService.deleteUser(id);
if (result > 0) {
response.sendRedirect("list.jsp");
} else {
response.getWriter().println("删除用户信息失败!");
}
} else if ("update".equals(action)) {
// 更新用户信息
int id = Integer.parseInt(request.getParameter("id"));
String username = request.getParameter("username");
String password = request.getParameter("password");
User user = new User();
user.setId(id);
user.setUsername(username);
user.setPassword(password);
int result = userService.updateUser(user);
if (result > 0) {
response.sendRedirect("list.jsp");
} else {
response.getWriter().println("更新用户信息失败!");
}
}
}
}
```
6. 编写JSP页面
最后,在JSP页面上展示用户信息,并提供相应的操作。
```jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>用户信息列表</title>
</head>
<body>
<h1>用户信息列表</h1>
<table border="1">
<thead>
<tr>
<th>ID</th>
<th>用户名</th>
<th>密码</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<c:forEach var="user" items="${users}">
<tr>
<td>${user.id}</td>
<td>${user.username}</td>
<td>${user.password}</td>
<td>
<a href="delete.jsp?id=${user.id}&action=delete">删除</a>
<a href="update.jsp?id=${user.id}&username=${user.username}&password=${user.password}&action=update">修改</a>
</td>
</tr>
</c:forEach>
</tbody>
</table>
<hr>
<h1>添加用户信息</h1>
<form action="UserController" method="post">
<input type="hidden" name="action" value="add">
<p>用户名:<input type="text" name="username"></p>
<p>密码:<input type="password" name="password"></p>
<p><input type="submit" value="添加"></p>
</form>
</body>
</html>
```
以上就是一个简单的JDBC+三层结构+jsp的案例,其中采用了MVC的设计模式,将业务逻辑、数据访问和表示分离开来,使代码更加清晰和易于维护。