package com.action;

//这里是源包里的p1包中的addServlet
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
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(name="addServlet",urlPatterns = {"StudentAdd.jsp"})
public class addServlet extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        try (PrintWriter out = response.getWriter()) {
            String driver="com.mysql.jdbc.Driver";
            String url="jdbc:mysql://localhost:3306/test?characterEncoding=utf-8"
                    + "&serverTimezone=UTC";
            String usr="root";
            String password="123456";
            try{
                Class.forName(driver);
                Connection conn=DriverManager.getConnection(url,usr,password);
                String sqlString="INSERT INTO admin (Admin_id, Admin_password, Admin_name, Admin_tel)"
                        + "VALUES ( ?, ?, ?, ? )";
                PreparedStatement pstmt=conn.prepareStatement(sqlString);
                int id = Integer.parseInt(request.getParameter("adminid"));
                String pass = request.getParameter("adminpass");
                String name = request.getParameter("adminname");
                String tel = request.getParameter("admintel");
                pstmt.setInt(1, id);
                pstmt.setString(2, pass);
                pstmt.setString(3, name);
                pstmt.setString(4,tel );
                pstmt.execute();
            }catch(Exception e){
                System.err.println("error:"+e);
            }
            response.sendRedirect("StudentManage.jsp");
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    public String getServletInfo() {
        return "Short description";
    }

}
