jsp+serverlet+jdbc+mysql实现增删改查
时间: 2023-09-13 16:07:55 浏览: 105
基于jsp+servlet+jdbc+MySQL的学生管理系统.zip
5星 · 资源好评率100%
首先,需要创建一个数据库和对应的表。假设我们创建了一个名为“student”的数据库,其中有一个名为“student_info”的表,包含id、name、age和gender四个字段。
接下来,需要创建一个Java Web项目,将mysql-connector-java包添加到项目中。
在项目中创建一个servlet,并在web.xml中进行配置。
在servlet中,使用JDBC连接数据库,并实现增删改查操作。下面是一个示例代码:
```
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class StudentServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private Connection conn = null;
private PreparedStatement pstmt = null;
private ResultSet rs = null;
// 连接数据库
private void getConnection() {
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/student";
String user = "root";
String password = "123456";
try {
Class.forName(driver);
conn = DriverManager.getConnection(url, user, password);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
// 关闭资源
private void close() {
try {
if (rs != null) {
rs.close();
}
if (pstmt != null) {
pstmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
// 查询
private void query(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
getConnection();
String sql = "SELECT * FROM student_info";
try {
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
while (rs.next()) {
out.println(rs.getInt("id") + " " + rs.getString("name") + " " + rs.getInt("age") + " " + rs.getString("gender"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
close();
}
}
// 插入
private void insert(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
getConnection();
int id = Integer.parseInt(request.getParameter("id"));
String name = request.getParameter("name");
int age = Integer.parseInt(request.getParameter("age"));
String gender = request.getParameter("gender");
String sql = "INSERT INTO student_info(id, name, age, gender) VALUES(?, ?, ?, ?)";
try {
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, id);
pstmt.setString(2, name);
pstmt.setInt(3, age);
pstmt.setString(4, gender);
int count = pstmt.executeUpdate();
if (count > 0) {
out.println("插入成功!");
} else {
out.println("插入失败!");
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
close();
}
}
// 更新
private void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
getConnection();
int id = Integer.parseInt(request.getParameter("id"));
String name = request.getParameter("name");
int age = Integer.parseInt(request.getParameter("age"));
String gender = request.getParameter("gender");
String sql = "UPDATE student_info SET name=?, age=?, gender=? WHERE id=?";
try {
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, name);
pstmt.setInt(2, age);
pstmt.setString(3, gender);
pstmt.setInt(4, id);
int count = pstmt.executeUpdate();
if (count > 0) {
out.println("更新成功!");
} else {
out.println("更新失败!");
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
close();
}
}
// 删除
private void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
getConnection();
int id = Integer.parseInt(request.getParameter("id"));
String sql = "DELETE FROM student_info WHERE id=?";
try {
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, id);
int count = pstmt.executeUpdate();
if (count > 0) {
out.println("删除成功!");
} else {
out.println("删除失败!");
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
close();
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
String op = request.getParameter("op");
if ("query".equals(op)) {
query(request, response);
} else if ("insert".equals(op)) {
insert(request, response);
} else if ("update".equals(op)) {
update(request, response);
} else if ("delete".equals(op)) {
delete(request, response);
}
}
}
```
在jsp页面中,可以通过表单提交数据,并调用对应的servlet实现增删改查操作。例如:
```
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="StudentServlet" method="post">
<input type="hidden" name="op" value="insert">
ID:<input type="text" name="id"><br>
姓名:<input type="text" name="name"><br>
年龄:<input type="text" name="age"><br>
性别:<input type="text" name="gender"><br>
<input type="submit" value="插入">
</form>
<form action="StudentServlet" method="post">
<input type="hidden" name="op" value="query">
<input type="submit" value="查询">
</form>
<form action="StudentServlet" method="post">
<input type="hidden" name="op" value="update">
ID:<input type="text" name="id"><br>
姓名:<input type="text" name="name"><br>
年龄:<input type="text" name="age"><br>
性别:<input type="text" name="gender"><br>
<input type="submit" value="更新">
</form>
<form action="StudentServlet" method="post">
<input type="hidden" name="op" value="delete">
ID:<input type="text" name="id"><br>
<input type="submit" value="删除">
</form>
</body>
</html>
```
以上代码实现了一个简单的增删改查功能。需要注意的是,这只是一个示例,实际应用中需要根据具体情况进行修改。
阅读全文