idea写一个Servlet以实现增删改查 Emp表,连接数据库
时间: 2024-02-03 12:03:25 浏览: 125
好的,以下是一个示例代码,可以实现基本的增删改查功能:
```java
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.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/EmpServlet")
public class EmpServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private Connection conn;
private PreparedStatement pstmt;
private ResultSet rs;
public void init() {
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/test";
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();
}
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
String action = request.getParameter("action");
if (action.equals("addEmp")) {
String empno = request.getParameter("empno");
String ename = request.getParameter("ename");
String job = request.getParameter("job");
String mgr = request.getParameter("mgr");
String hiredate = request.getParameter("hiredate");
String sal = request.getParameter("sal");
String comm = request.getParameter("comm");
String deptno = request.getParameter("deptno");
String sql = "INSERT INTO emp (empno, ename, job, mgr, hiredate, sal, comm, deptno) VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
try {
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, empno);
pstmt.setString(2, ename);
pstmt.setString(3, job);
pstmt.setString(4, mgr);
pstmt.setString(5, hiredate);
pstmt.setString(6, sal);
pstmt.setString(7, comm);
pstmt.setString(8, deptno);
pstmt.executeUpdate();
out.println("添加成功!");
} catch (SQLException e) {
e.printStackTrace();
}
} else if (action.equals("deleteEmp")) {
String empno = request.getParameter("empno");
String sql = "DELETE FROM emp WHERE empno = ?";
try {
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, empno);
pstmt.executeUpdate();
out.println("删除成功!");
} catch (SQLException e) {
e.printStackTrace();
}
} else if (action.equals("updateEmp")) {
String empno = request.getParameter("empno");
String ename = request.getParameter("ename");
String job = request.getParameter("job");
String mgr = request.getParameter("mgr");
String hiredate = request.getParameter("hiredate");
String sal = request.getParameter("sal");
String comm = request.getParameter("comm");
String deptno = request.getParameter("deptno");
String sql = "UPDATE emp SET ename = ?, job = ?, mgr = ?, hiredate = ?, sal = ?, comm = ?, deptno = ? WHERE empno = ?";
try {
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, ename);
pstmt.setString(2, job);
pstmt.setString(3, mgr);
pstmt.setString(4, hiredate);
pstmt.setString(5, sal);
pstmt.setString(6, comm);
pstmt.setString(7, deptno);
pstmt.setString(8, empno);
pstmt.executeUpdate();
out.println("修改成功!");
} catch (SQLException e) {
e.printStackTrace();
}
} else if (action.equals("queryEmp")) {
String empno = request.getParameter("empno");
String ename = request.getParameter("ename");
String sql = "SELECT * FROM emp WHERE 1 = 1";
if (empno != null && !empno.trim().equals("")) {
sql += " AND empno = '" + empno + "'";
}
if (ename != null && !ename.trim().equals("")) {
sql += " AND ename = '" + ename + "'";
}
try {
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
while (rs.next()) {
out.println("empno: " + rs.getString("empno") + "<br>");
out.println("ename: " + rs.getString("ename") + "<br>");
out.println("job: " + rs.getString("job") + "<br>");
out.println("mgr: " + rs.getString("mgr") + "<br>");
out.println("hiredate: " + rs.getString("hiredate") + "<br>");
out.println("sal: " + rs.getString("sal") + "<br>");
out.println("comm: " + rs.getString("comm") + "<br>");
out.println("deptno: " + rs.getString("deptno") + "<br><br>");
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public void destroy() {
try {
if (rs != null) {
rs.close();
}
if (pstmt != null) {
pstmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
```
在这个例子中,我们使用了MySQL数据库,数据库名为test,用户名为root,密码为123456。你可以根据自己的实际情况修改这些参数。在init()方法中,我们使用了JDBC驱动来连接数据库。
在doPost()方法中,我们首先获取前端传来的action参数,根据参数的不同执行不同的操作。如果action为addEmp,表示进行插入操作,我们从request中获取表单数据,使用PreparedStatement对数据库进行操作。如果action为deleteEmp,表示进行删除操作,我们从request中获取要删除的empno,对数据库进行操作。如果action为updateEmp,表示进行更新操作,我们从request中获取要更新的empno以及表单数据,对数据库进行操作。如果action为queryEmp,表示进行查询操作,我们从request中获取要查询的empno和ename,根据不同的情况构造不同的SQL语句,使用PreparedStatement查询数据库,并将结果输出到前端。
最后,我们在destroy()方法中关闭ResultSet、PreparedStatement和Connection对象,释放资源。
阅读全文