package com.pp;

import java.io.IOException;
import java.sql.SQLException;
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;

/**
 * Servlet implementation class People
 */
@WebServlet("/aaa")
public class aaa extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public aaa() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        request.setCharacterEncoding("utf-8");
        String aab=request.getParameter("aab");
        get login=new get(aab);
        hand L=new hand();
        try {
            L.insert(login);
            request.getRequestDispatcher("NewFile.jsp").forward(request, response);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
     private void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            String aab=request.getParameter("aab");
            hand L=new hand();
            L.delete(aab);
            response.sendRedirect("delete.jsp?delete=yes");
        }
     private void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            String aab=request.getParameter("aab");
            String aab1=request.getParameter("aab1");
            hand L=new hand();
            L.update(aab,aab1);
            response.sendRedirect("update.jsp?update=yes");
        }
     private void show(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            hand L=new hand();
            String aab=request.getParameter("aab");
            
            List<get> list=L.select();
            
            request.setAttribute("list",list);
            request.getRequestDispatcher("show.jsp").forward(request, response);
        }
    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        //doGet(request, response);
        request.setCharacterEncoding("UTF-8");
        String method=request.getParameter("method");
        if("doGet".equals(method)) {
            doGet(request,response);
        }
        else if("delete".equals(method)) {
            delete(request,response);
        }
        else if("update".equals(method)) {
            update(request,response);
        }
        else if("show".equals(method)) {
            show(request,response);
        }
//        else if("delete1".equals(method)) {
//            delete1(request,response);
//        }
//        else if("login".equals(method)) {
//            login(request,response);
//        }
//        
    }

}
package com.pp;
import java.sql.*;
public class DBUtil{
    private DBUtil() {}
    static {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        }catch(ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    public static Connection getConnection() throws SQLException{
        return DriverManager.getConnection("jdbc:mysql://localhost:3306/user?serverTimezone=UTC&useUnicode=true&zeroDateTimeBehavior=convertToNull&autoReconnect=true&characterEncoding=utf-8","root","wangpengxiao");
    }
    public static void close(Connection conn,Statement ps,ResultSet rs) {
        if(rs!=null) {
            try {
                rs.close();
            }catch(SQLException s) {
                s.printStackTrace();
            }
        }
        if(ps!=null) {
            try {
                ps.close();
            }catch(SQLException s) {
                s.printStackTrace();
            }
        }
        if(conn!=null) {
            try {
                conn.close();
            }catch(SQLException s) {
                s.printStackTrace();
            }
        }
    }
}
package com.pp;

public class get {
    String aab;
    
    
    public String getAab() {
        return aab;
    }
    public void setAab(String x) {
        this.aab= x;
    }
    public get(String a) {
        aab=a;
        
    }
}
package com.pp;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class hand {
    public void insert(get L) throws SQLException {
        String sql = "insert into aaa(aab) values(?)";
          Connection connection = DBUtil.getConnection();
          PreparedStatement preparedStatement = null;
          preparedStatement = connection.prepareStatement(sql);
          preparedStatement.setString(1,L.getAab());
          preparedStatement.execute();
    }
    public void delete(String aab) {
        String sql="delete from aaa where aab=?";
        Connection con;
        try {
            con = DBUtil.getConnection();
            PreparedStatement pa=con.prepareStatement(sql);
            pa.setString(1,aab);
            pa.execute();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    public void update(String aab,String aab1) {
        String sql="update aaa set aab=? where aab=?";
        Connection con;
        try {
            con = DBUtil.getConnection();
            PreparedStatement pa=con.prepareStatement(sql);
            pa.setString(1,aab1);
            pa.setString(2,aab);
            
            pa.execute();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
    }
    public List<get> select() {
        String sql="select * from aaa";
        get L=null;
        List<get> list=new ArrayList(); 
        Connection con;
        try {
            con = DBUtil.getConnection();
            PreparedStatement pa=con.prepareStatement(sql);
            ResultSet rs=pa.executeQuery();
            while(rs.next()) {
                String aab=rs.getString("aab");
                L=new get(aab);
                list.add(L);
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return list;
    }
}

 

posted @ 2021-11-22 23:11  慢漫曼蔓  阅读(26)  评论(0)    收藏  举报