public class MangeServlet extends HttpServlet {
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
* methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
/* TODO output your page here. You may use following sample code. */
String reqtype = request.getParameter("type");
//out.print(reqtype);
Connection cn = (new Conn()).getConn();;
ResultSet rs = null;
if (reqtype.equals("0")) {
String myselected = request.getParameter("select_good");
String mysel = request.getParameter("sel");
String mysql = null;
if (myselected.equals("1")) {
mysql = "select * from goods where id = '" + mysel + "'";
} else if (myselected.equals("2")) {
mysql = "select * from goods where name = '" + mysel + "'";
} else if (myselected.equals("3")) {
mysql = "select * from goods where price = '" + mysel + "'";
}
try {
Statement myst = cn.createStatement();
ResultSet myrs = myst.executeQuery(mysql);
ArrayList<Productbean> products = new ArrayList<>();
while(myrs.next()){
Productbean p = new Productbean();
p.setId(myrs.getString(1));
p.setName(myrs.getString(2));
p.setNum(myrs.getString(3));
p.setDes(myrs.getString(4));
products.add(p);
}
request.setAttribute("sp", products);
request.getRequestDispatcher("goods.jsp").forward(request, response);
} catch (SQLException ex) {
}
}
if (reqtype.equals("1")) {
String myid = request.getParameter("add_goodID");
String mygood = request.getParameter("add_good");
String mynum = request.getParameter("add_goodNum");
String mydes = request.getParameter("add_goodDes");
String mysql0 = "select * from goods where id = '" + myid + "'";
String mysql = "insert into goods values('" + myid + "','" + mygood + "','" + mynum + "','" + mydes + "')";
try {
Statement myst = cn.createStatement();
if (myst.executeQuery(mysql0).next()) {
out.println("<html><script>alert('此产品序号已经存在,请重新输入!');setTimeout(function(){window.location.href='goods.jsp';},1);</script></html>");
} else {
myst.execute(mysql);
out.println("<html><script>alert('添加成功!');setTimeout(function(){window.location.href='goods.jsp';},1);</script></html>");
}
myst.execute(mysql);
response.sendRedirect("goods.jsp");
} catch (SQLException ex) {
}
}
if (reqtype.equals("2")) {
String myid = request.getParameter("del_goodID");
String mysql = "select * from goods where id = '" + myid + "'";
String mysql0 = "delete from goods where id ='" + myid + "'";
//
try {
Statement myst = cn.createStatement();
if (myst.executeQuery(mysql).next()) {
myst.execute(mysql0);
out.println("<html><script>alert('删除成功!');setTimeout(function(){window.location.href='goods.jsp';},1);</script></html>");
} else {
out.println("<html><script>alert('不存在这个香水,请重新输入!');setTimeout(function(){window.location.href='goods.jsp';},1);</script></html>");
}
} catch (SQLException ex) {
}
}
if (reqtype.equals("3")) {
// out.println("<script>alert('是否删除'));</script>");
String myid = request.getParameter("mod_goodID");
String mygood = request.getParameter("mod_good");
String mynum = request.getParameter("mod_goodNum");
String mydes = request.getParameter("mod_goodDes");
String mysql = "update goods set name='" + mygood + "',number='" + mynum + "',des ='" + mydes + "' where id='" + myid + "' ";
System.out.println(mysql);
try {
Statement myst = cn.createStatement();
myst.execute(mysql);
response.sendRedirect("goods.jsp");
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Handles the HTTP <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
* methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
/* TODO output your page here. You may use following sample code. */
String reqtype = request.getParameter("type");
//out.print(reqtype);
Connection cn = (new Conn()).getConn();;
ResultSet rs = null;
if (reqtype.equals("0")) {
String myselected = request.getParameter("select_good");
String mysel = request.getParameter("sel");
String mysql = null;
if (myselected.equals("1")) {
mysql = "select * from goods where id = '" + mysel + "'";
} else if (myselected.equals("2")) {
mysql = "select * from goods where name = '" + mysel + "'";
} else if (myselected.equals("3")) {
mysql = "select * from goods where price = '" + mysel + "'";
}
try {
Statement myst = cn.createStatement();
ResultSet myrs = myst.executeQuery(mysql);
ArrayList<Productbean> products = new ArrayList<>();
while(myrs.next()){
Productbean p = new Productbean();
p.setId(myrs.getString(1));
p.setName(myrs.getString(2));
p.setNum(myrs.getString(3));
p.setDes(myrs.getString(4));
products.add(p);
}
request.setAttribute("sp", products);
request.getRequestDispatcher("goods.jsp").forward(request, response);
} catch (SQLException ex) {
}
}
if (reqtype.equals("1")) {
String myid = request.getParameter("add_goodID");
String mygood = request.getParameter("add_good");
String mynum = request.getParameter("add_goodNum");
String mydes = request.getParameter("add_goodDes");
String mysql0 = "select * from goods where id = '" + myid + "'";
String mysql = "insert into goods values('" + myid + "','" + mygood + "','" + mynum + "','" + mydes + "')";
try {
Statement myst = cn.createStatement();
if (myst.executeQuery(mysql0).next()) {
out.println("<html><script>alert('此产品序号已经存在,请重新输入!');setTimeout(function(){window.location.href='goods.jsp';},1);</script></html>");
} else {
myst.execute(mysql);
out.println("<html><script>alert('添加成功!');setTimeout(function(){window.location.href='goods.jsp';},1);</script></html>");
}
myst.execute(mysql);
response.sendRedirect("goods.jsp");
} catch (SQLException ex) {
}
}
if (reqtype.equals("2")) {
String myid = request.getParameter("del_goodID");
String mysql = "select * from goods where id = '" + myid + "'";
String mysql0 = "delete from goods where id ='" + myid + "'";
//
try {
Statement myst = cn.createStatement();
if (myst.executeQuery(mysql).next()) {
myst.execute(mysql0);
out.println("<html><script>alert('删除成功!');setTimeout(function(){window.location.href='goods.jsp';},1);</script></html>");
} else {
out.println("<html><script>alert('不存在这个香水,请重新输入!');setTimeout(function(){window.location.href='goods.jsp';},1);</script></html>");
}
} catch (SQLException ex) {
}
}
if (reqtype.equals("3")) {
// out.println("<script>alert('是否删除'));</script>");
String myid = request.getParameter("mod_goodID");
String mygood = request.getParameter("mod_good");
String mynum = request.getParameter("mod_goodNum");
String mydes = request.getParameter("mod_goodDes");
String mysql = "update goods set name='" + mygood + "',number='" + mynum + "',des ='" + mydes + "' where id='" + myid + "' ";
System.out.println(mysql);
try {
Statement myst = cn.createStatement();
myst.execute(mysql);
response.sendRedirect("goods.jsp");
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Handles the HTTP <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}