📄 acustomer_edit.java
字号:
package data;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.sql.*;
public class Acustomer_edit
extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=GBK";
private String url;
private String users;
private String password;
private String driverClass;
//Initialize global variables
public void init() throws ServletException {
driverClass = "com.mysql.jdbc.Driver";
url = "jdbc:mysql://localhost:3306/furniture";
users = "root";
password = "123123";
try {
Class.forName(driverClass);
}
catch (ClassNotFoundException ce) {
throw new UnavailableException("加载数据库驱动失败!");
}
}
//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
Connection conn = null;
Statement stmt = null;
try {
conn = DriverManager.getConnection(url, users, password);
stmt = conn.createStatement();
stmt.executeUpdate("use furniture");
String custId = request.getParameter("customerId");
String custName = request.getParameter("custName");
String custSex = request.getParameter("custSex");
String custPhone = request.getParameter("custPhone");
String custAddress = request.getParameter("custAddress");
String option = request.getParameter("select");
if (option.compareTo("add") == 0) {
String sql2 = "insert into customer values(" + custId + "," + custName + "," + custSex + "," + custPhone + "," + custAddress + ")";
out.print("sql2:" + sql2);
stmt.execute(sql2);
}
else {
String sql = "delete from customer where custId=\"" + custId + "\" ";
out.println("sql:" + sql);
stmt.executeUpdate(sql);
}
out.println("操作成功" + "<br>");
out.print("点击进行下一个操作");
out.println("<form method='post' action='Acustomer_edit.jsp' >");
out.println(" <input type='submit' name='Submit' value='下一个' />");
out.println("</form>" + "<br>");
out.print("点击返回主页面");
out.println("<form method='post' action='/webmode/Admin.jsp' >");
out.println(" <input type='submit' name='Submit' value='返回' />");
out.println("</form>");
out.close();
}
catch (SQLException se) {
se.printStackTrace();
}
finally {
if (stmt != null) {
try {
stmt.close();
}
catch (SQLException se) {
se.printStackTrace();
}
stmt = null;
}
if (conn != null) {
try {
conn.close();
}
catch (SQLException se) {
se.printStackTrace();
}
conn = null;
}
}
out.close();
}
//Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
doGet(request, response);
}
//Clean up resources
public void destroy() {
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -