jsp+servlet+jdbc+mysql增删改查项目带源码
时间: 2023-05-10 19:50:32 浏览: 261
jsp+servlet+jdbc实现增删改查项目
3星 · 编辑精心推荐
对于一个具有JSP、Servlet、JDBC和MySQL技术的增删改查项目,主要有以下几个步骤:
1. 创建MySQL数据库和表格
在MySQL中创建对应的数据库和表格,以存储项目中需要的数据。
2. 编写JavaBean类
JavaBean类是用来封装表格中每一行数据的实体类。需要与表格中的每一列数据对应。
3. 编写JDBC类
JDBC类主要是连接MySQL数据库和Java代码,实现对数据库的增删改查操作。
4. 编写Servlet类
每一个增删改查页面都对应一个Servlet类,Servlet类主要是处理用户提交的请求,并跳转到相应的JSP页面。
5. 编写JSP页面
JSP页面主要是呈现数据和处理用户提交的表单数据,通过JSP页面将用户请求传递给Servlet类来实现增删改查操作。
6. 部署项目
将以上代码部署到Tomcat服务器上,并在网页中访问。
项目源码:
1. JavaBean类:
```java
// User.java
public class User {
private int id;
private String name;
private int age;
// Getters and Setters
}
```
2. JDBC类:
```java
// JDBCUtils.java
public class JDBCUtils {
private static String driver = "com.mysql.cj.jdbc.Driver";
private static String url = "jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=UTC";
private static String username = "root";
private static String password = "password";
public static Connection getConnection() throws Exception {
Class.forName(driver);
return DriverManager.getConnection(url, username, password);
}
public static void close(Connection conn, PreparedStatement ps, ResultSet rs) {
try {
if (rs != null) {
rs.close();
}
if (ps != null) {
ps.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
```
3. Servlet类:
```java
// AddUserServlet.java
public class AddUserServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name = request.getParameter("name");
int age = Integer.parseInt(request.getParameter("age"));
User user = new User();
user.setName(name);
user.setAge(age);
Connection conn = null;
PreparedStatement ps = null;
try {
conn = JDBCUtils.getConnection();
String sql = "INSERT INTO user(name, age) VALUES(?, ?)";
ps = conn.prepareStatement(sql);
ps.setString(1, user.getName());
ps.setInt(2, user.getAge());
ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCUtils.close(conn, ps, null);
}
response.sendRedirect(request.getContextPath() + "/list");
}
}
// ListUserServlet.java
public class ListUserServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
List<User> userList = new ArrayList<>();
try {
conn = JDBCUtils.getConnection();
String sql = "SELECT * FROM user";
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
while (rs.next()) {
int id = rs.getInt("id");
String name = rs.getString("name");
int age = rs.getInt("age");
User user = new User();
user.setId(id);
user.setName(name);
user.setAge(age);
userList.add(user);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCUtils.close(conn, ps, rs);
}
request.setAttribute("userList", userList);
request.getRequestDispatcher("/WEB-INF/jsp/list.jsp").forward(request, response);
}
}
// DeleteUserServlet.java
public class DeleteUserServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int id = Integer.parseInt(request.getParameter("id"));
Connection conn = null;
PreparedStatement ps = null;
try {
conn = JDBCUtils.getConnection();
String sql = "DELETE FROM user WHERE id = ?";
ps = conn.prepareStatement(sql);
ps.setInt(1, id);
ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCUtils.close(conn, ps, null);
}
response.sendRedirect(request.getContextPath() + "/list");
}
}
// EditUserServlet.java
public class EditUserServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int id = Integer.parseInt(request.getParameter("id"));
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
User user = new User();
try {
conn = JDBCUtils.getConnection();
String sql = "SELECT * FROM user WHERE id = ?";
ps = conn.prepareStatement(sql);
ps.setInt(1, id);
rs = ps.executeQuery();
while (rs.next()) {
String name = rs.getString("name");
int age = rs.getInt("age");
user.setId(id);
user.setName(name);
user.setAge(age);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCUtils.close(conn, ps, rs);
}
request.setAttribute("user", user);
request.getRequestDispatcher("/WEB-INF/jsp/edit.jsp").forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int id = Integer.parseInt(request.getParameter("id"));
String name = request.getParameter("name");
int age = Integer.parseInt(request.getParameter("age"));
User user = new User();
user.setId(id);
user.setName(name);
user.setAge(age);
Connection conn = null;
PreparedStatement ps = null;
try {
conn = JDBCUtils.getConnection();
String sql = "UPDATE user SET name = ?, age = ? WHERE id = ?";
ps = conn.prepareStatement(sql);
ps.setString(1, user.getName());
ps.setInt(2, user.getAge());
ps.setInt(3, user.getId());
ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCUtils.close(conn, ps, null);
}
response.sendRedirect(request.getContextPath() + "/list");
}
}
```
4. JSP页面:
```jsp
<!-- add.jsp -->
<form method="post" action="${pageContext.request.contextPath}/add">
<label>Name:</label>
<input type="text" name="name"><br>
<label>Age:</label>
<input type="text" name="age"><br>
<input type="submit" value="Submit">
</form>
<!-- list.jsp -->
<table border="1">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Age</th>
<th>Operation</th>
</tr>
</thead>
<tbody>
<c:forEach items="${userList}" var="user">
<tr>
<td>${user.id}</td>
<td>${user.name}</td>
<td>${user.age}</td>
<td>
<a href="${pageContext.request.contextPath}/edit?id=${user.id}">Edit</a>
<a href="${pageContext.request.contextPath}/delete?id=${user.id}">Delete</a>
</td>
</tr>
</c:forEach>
</tbody>
</table>
<a href="${pageContext.request.contextPath}/add">Add User</a>
<!-- edit.jsp -->
<form method="post" action="${pageContext.request.contextPath}/edit">
<input type="hidden" name="id" value="${user.id}">
<label>Name:</label>
<input type="text" name="name" value="${user.name}"><br>
<label>Age:</label>
<input type="text" name="age" value="${user.age}"><br>
<input type="submit" value="Submit">
</form>
```
阅读全文