idea写一个Servlet以实现增删改查 Emp表,连接oracle数据库
时间: 2024-02-03 16:03:25 浏览: 99
好的,这个问题很棒。为了实现增删改查Emp表的Servlet,需要进行以下步骤:
1. 首先,需要建立一个Emp类,包含Emp表中每个字段的属性,如id、name、age等。
2. 然后,需要在Servlet中建立Oracle数据库连接,在doGet或doPost方法中实现增删改查操作。
3. 对于查询操作,可以使用SELECT语句从Emp表中获取数据,并将结果集存储在List<Emp>对象中。
4. 对于插入操作,可以使用INSERT语句将新的Emp对象插入到Emp表中。
5. 对于更新操作,可以使用UPDATE语句更新Emp表中指定的字段。
6. 对于删除操作,可以使用DELETE语句从Emp表中删除指定的数据。
下面是一个简单的示例代码,演示如何实现增删改查Emp表的Servlet:
```java
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
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 = null;
private PreparedStatement pstmt = null;
private ResultSet rs = null;
private String url = "jdbc:oracle:thin:@localhost:1521:orcl";
private String user = "scott";
private String password = "tiger";
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String method = request.getParameter("method");
if ("add".equals(method)) {
addEmp(request, response);
} else if ("delete".equals(method)) {
deleteEmp(request, response);
} else if ("update".equals(method)) {
updateEmp(request, response);
} else if ("query".equals(method)) {
queryEmp(request, response);
}
}
private void addEmp(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String id = request.getParameter("id");
String name = request.getParameter("name");
String age = request.getParameter("age");
String sql = "INSERT INTO Emp (id, name, age) VALUES (?, ?, ?)";
try {
conn = DriverManager.getConnection(url, user, password);
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, id);
pstmt.setString(2, name);
pstmt.setString(3, age);
pstmt.executeUpdate();
response.sendRedirect("index.jsp");
} catch (SQLException e) {
e.printStackTrace();
} finally {
closeAll();
}
}
private void deleteEmp(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String id = request.getParameter("id");
String sql = "DELETE FROM Emp WHERE id = ?";
try {
conn = DriverManager.getConnection(url, user, password);
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, id);
pstmt.executeUpdate();
response.sendRedirect("index.jsp");
} catch (SQLException e) {
e.printStackTrace();
} finally {
closeAll();
}
}
private void updateEmp(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String id = request.getParameter("id");
String name = request.getParameter("name");
String age = request.getParameter("age");
String sql = "UPDATE Emp SET name = ?, age = ? WHERE id = ?";
try {
conn = DriverManager.getConnection(url, user, password);
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, name);
pstmt.setString(2, age);
pstmt.setString(3, id);
pstmt.executeUpdate();
response.sendRedirect("index.jsp");
} catch (SQLException e) {
e.printStackTrace();
} finally {
closeAll();
}
}
private void queryEmp(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
List<Emp> list = new ArrayList<Emp>();
String sql = "SELECT * FROM Emp";
try {
conn = DriverManager.getConnection(url, user, password);
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
while (rs.next()) {
Emp emp = new Emp();
emp.setId(rs.getString("id"));
emp.setName(rs.getString("name"));
emp.setAge(rs.getString("age"));
list.add(emp);
}
request.setAttribute("list", list);
request.getRequestDispatcher("list.jsp").forward(request, response);
} catch (SQLException e) {
e.printStackTrace();
} finally {
closeAll();
}
}
private void closeAll() {
try {
if (rs != null) {
rs.close();
}
if (pstmt != null) {
pstmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
```
以上代码可能需要根据你的实际情况作一些修改,例如数据库连接信息、SQL语句等。
阅读全文