<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP ‘LoginServlet.jsp‘ starting page</title>
</head>
<body>
<form action="<%=request.getContextPath()%>/mfrEdit1.do" method="post">
<table border="1">
<tr>
<td>厂商代码</td>
<td><input type="text" name="cno">
<input type="hidden" name="method" value="find"/>
</td>
</tr>
<tr>
<td>
<input type="submit" value="查询"/>
</td>
</tr>
</table>
</form>
</body>
</html>
package servlet;
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.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import entity.Cusinfo;
@SuppressWarnings("serial")
public class EditServlet1 extends HttpServlet{
private final String URL="jdbc:oracle:thin:@127.0.0.1:1521:orcl";
private final String USER="gsuser";
private final String PWD="160127";
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
String cno=req.getParameter("cno");
int i = deletemfrEdit(cno);
if (i > 0) {
// 删除成功了
resp.sendRedirect(req.getContextPath() + "/Success.jsp");
} else {
resp.sendRedirect("http://www.sohu.com");
}
}
private int deletemfrEdit(String cno) {
String delete= "DELETE FROM MFREDIT A WHERE A.CNO=?";
int i = 0;
// 加载驱动
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = DriverManager.getConnection(URL, USER, PWD);
pstmt = conn.prepareStatement(delete);
// 设置参数
pstmt.setString(1, cno);
//pstmt.setString(2, cname); // 执行
i = 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 i;
}
原文:http://www.cnblogs.com/fanweichao/p/3916006.html