数据库程序设计第五天--管理员权限

一、说在前面

  今天的目标是完成管理员对隔离地信息的增删改查操作

二、任务进度

package Servlet;

import java.io.IOException;
import java.util.ArrayList;
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;

import Bean.IsolatedAreaBean;
import Bean.PersonBean;
import Dao.IPDao;
import Dao.IsolatedAreaDao;

/**
 * Servlet implementation class AreaShowServlet
 */
@WebServlet("/areaShowServlet")
public class AreaShowServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AreaShowServlet() {
        super();
        // TODO Auto-generated constructor stub
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        List<PersonBean> list=new ArrayList();
        String wname=request.getParameter("wname");
        IPDao dao=new IPDao();
        list=dao.list(wname);
        request.setAttribute("list", list);
        request.getRequestDispatcher("personShow.jsp").forward(request, response);
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}
AreaShowServlet
package Bean;
/**
*
*@author 作者:高宇豪
*@version 创建时间:2020年7月15日上午9:49:30
*类说明:
*/
public class IsolatedAreaBean {
    private int wid;
    private String wname;
    private int capacity;
    private int residents;
    private String hospital;
    private String address;
    
    
    public IsolatedAreaBean(String wname, int capacity, int residents, String hospital, String address) {
        super();
        this.wname = wname;
        this.capacity = capacity;
        this.residents = residents;
        this.hospital = hospital;
        this.address = address;
    }
    public IsolatedAreaBean(int wid, String wname, int capacity, int residents, String hospital, String address) {
        super();
        this.wid = wid;
        this.wname = wname;
        this.capacity = capacity;
        this.residents = residents;
        this.hospital = hospital;
        this.address = address;
    }
    public int getWid() {
        return wid;
    }
    public void setWid(int wid) {
        this.wid = wid;
    }
    public String getWname() {
        return wname;
    }
    public void setWname(String wname) {
        this.wname = wname;
    }
    public int getCapacity() {
        return capacity;
    }
    public void setCapacity(int capacity) {
        this.capacity = capacity;
    }
    public int getResidents() {
        return residents;
    }
    public void setResidents(int residents) {
        this.residents = residents;
    }
    public String getHospital() {
        return hospital;
    }
    public void setHospital(String hospital) {
        this.hospital = hospital;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    
    
    
}
IsolatedAreaBean
package Dao;

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

import Bean.IsolatedAreaBean;
import Bean.LoginBean;
import DBUtil.DBUtil;

/**
*
*@author 作者:高宇豪
*@version 创建时间:2020年7月15日上午10:01:07
*类说明:
*/
public class IsolatedAreaDao {
    //添加
        public boolean insert(IsolatedAreaBean area){
            String sql="insert into isolated_area (wname,capacity,residents,hospital,address) values ('"+area.getWname()+"','"+area.getCapacity()+"','"+area.getResidents()+"','"+area.getHospital()+"','"+area.getAddress()+"')";
            Connection conn=DBUtil.getConn();
            Statement state=null;
            try {
                state=conn.createStatement();
                state.executeUpdate(sql);
            }catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
            }finally {
                DBUtil.close(state,conn);
            }
            return true ;
        }
        //删除
        public boolean delete(int wid){
            String sql="delete from isolated_area where wid='"+wid+"'";
            Connection conn=DBUtil.getConn();
            Statement state=null;
            try {
                if(!select(wid)) {
                    return false;
                }
                state=conn.createStatement();
                state.executeUpdate(sql);
            }catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
            }finally {
                DBUtil.close(state,conn);
            }
            return true ;
        }
        //判断是否存在
        public boolean select(IsolatedAreaBean area){
            boolean flag=false;
            String sql="select * from isolated_area where wid='"+area.getWid()+"' ";
            //判断语句正确
            //System.out.println(sql);
            Connection conn=DBUtil.getConn();
            Statement state=null;
            ResultSet rs = null;
            try {
                state=conn.createStatement();
                rs=state.executeQuery(sql);
                while (rs.next()) {
                    flag = true;
                }
            }catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
            }finally {
                DBUtil.close(state,conn);
            }
            return flag;
        }
        public boolean select(int wid){
            boolean flag=false;
            String sql="select * from isolated_area where wid='"+wid+"' ";
            //判断语句正确
            //System.out.println(sql);
            Connection conn=DBUtil.getConn();
            Statement state=null;
            ResultSet rs = null;
            try {
                state=conn.createStatement();
                rs=state.executeQuery(sql);
                while (rs.next()) {
                    flag = true;
                }
            }catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
            }finally {
                DBUtil.close(state,conn);
            }
            return flag;
        }
        public int select(String wname){
            int wid=0;
            String sql="select * from isolated_area where wname='"+wname+"' ";
            //判断语句正确
            //System.out.println(sql);
            Connection conn=DBUtil.getConn();
            Statement state=null;
            ResultSet rs = null;
            try {
                state=conn.createStatement();
                rs=state.executeQuery(sql);
                if (rs.next()) {
                    wid=rs.getInt("wid");
                }
            }catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
            }finally {
                DBUtil.close(state,conn);
            }
            return wid;
        }
        //搜索
        public IsolatedAreaBean search(int wid){
            boolean flag=false;
            String sql="select * from isolated_area where wid='"+wid+"' ";
            //判断语句正确
            //System.out.println(sql);
            Connection conn=DBUtil.getConn();
            Statement state=null;
            ResultSet rs = null;
            IsolatedAreaBean area=null;
            try {
                state=conn.createStatement();
                rs=state.executeQuery(sql);
                
                while (rs.next()) {
                    String wname=rs.getString("wname");
                    int capacity=rs.getInt("capacity");
                    int residents=rs.getInt("residents");
                    String hospital=rs.getString("hospital");
                    String address=rs.getString("address");
                    area=new IsolatedAreaBean(wid, wname, capacity, residents, hospital, address);
                    
                }
            }catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
            }finally {
                DBUtil.close(state,conn);
            }
            return area;
        }
        public boolean search(String wname){
            boolean flag=false;
            String sql="select * from isolated_area where wname='"+wname+"' ";
            //判断语句正确
            //System.out.println(sql);
            Connection conn=DBUtil.getConn();
            Statement state=null;
            ResultSet rs = null;
            IsolatedAreaBean area=null;
            try {
                state=conn.createStatement();
                rs=state.executeQuery(sql);
                
                if (rs.next()) {
                    flag=true;
                }
            }catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
            }finally {
                DBUtil.close(state,conn);
            }
            return flag;
        }
        //遍历
        public List<IsolatedAreaBean> list(){
            String sql="select * from isolated_area order by wid asc";
            
            List<IsolatedAreaBean>list=new ArrayList<>();
            //给集合list创造(new)一个存储空间用于存放数据
            
            Connection conn=DBUtil.getConn();
            Statement state=null;
            ResultSet rs=null;
            try {
                state=conn.createStatement();
                rs=state.executeQuery(sql);
                IsolatedAreaBean area=null;
                while(rs.next()) {
                    int wid=rs.getInt("wid");
                    String wname=rs.getString("wname");
                    int capacity=rs.getInt("capacity");
                    int residents=rs.getInt("residents");
                    String hospital=rs.getString("hospital");
                    String address=rs.getString("address");
                    area=new IsolatedAreaBean(wid, wname, capacity, residents, hospital, address);
                    list.add(area);
                }
            }catch (SQLException e)
            {
                e.printStackTrace();
            }
            finally
            {
                DBUtil.close(state, conn);
            }
            return list;
        }
        public List<IsolatedAreaBean> list(String wname){
            String sql="select * from isolated_area where wname='"+wname+"' order by wid asc";
            
            List<IsolatedAreaBean>list=new ArrayList<>();
            //给集合list创造(new)一个存储空间用于存放数据
            
            Connection conn=DBUtil.getConn();
            Statement state=null;
            ResultSet rs=null;
            try {
                state=conn.createStatement();
                rs=state.executeQuery(sql);
                IsolatedAreaBean area=null;
                while(rs.next()) {
                    int wid=rs.getInt("wid");
                    int capacity=rs.getInt("capacity");
                    int residents=rs.getInt("residents");
                    String hospital=rs.getString("hospital");
                    String address=rs.getString("address");
                    area=new IsolatedAreaBean(wid, wname, capacity, residents, hospital, address);
                    list.add(area);
                }
            }catch (SQLException e)
            {
                e.printStackTrace();
            }
            finally
            {
                DBUtil.close(state, conn);
            }
            return list;
        }
        //更改
        public boolean update(IsolatedAreaBean area){
            String sql="update isolated_area set wname='"+area.getWname()+"',capacity='"+area.getCapacity()+"',residents='"+area.getResidents()+"',hospital='"+area.getHospital()+"',address='"+area.getAddress()+"' where wid='"+area.getWid()+"'";
            //System.out.println(sql);
            Connection conn=DBUtil.getConn();
            Statement state=null;
            try {
                state=conn.createStatement();
                state.executeUpdate(sql);
            }catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
            }finally {
                DBUtil.close(state,conn);
            }
            return true ;
        }
}
IsolatedAreaDao
package Servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import Dao.IsolatedAreaDao;

/**
 * Servlet implementation class DeleteAreaServlet
 */
@WebServlet("/deleteAreaServlet")
public class DeleteAreaServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DeleteAreaServlet() {
        super();
        // TODO Auto-generated constructor stub
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        int wid=Integer.parseInt(request.getParameter("wid"));
        IsolatedAreaDao dao=new IsolatedAreaDao();
        if(dao.delete(wid)){
            request.setAttribute("message", "删除成功");
            request.getRequestDispatcher("isolatedAreaShowServlet").forward(request, response);
        }else {
            request.setAttribute("message", "删除失败");
            request.getRequestDispatcher("isolatedAreaShowServlet").forward(request, response);
        }
        
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}
DeleteAreaServlet
package Servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import Bean.IsolatedAreaBean;
import Bean.LoginBean;
import Dao.IsolatedAreaDao;
import Dao.LoginDao;

/**
 * Servlet implementation class InsertIsolatedAreaServlet
 */
@WebServlet("/insertIsolatedAreaServlet")
public class InsertIsolatedAreaServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public InsertIsolatedAreaServlet() {
        super();
        // TODO Auto-generated constructor stub
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        String wname=request.getParameter("wname");
        String hospital=request.getParameter("hospital");
        String address=request.getParameter("address");
        int capacity=Integer.parseInt(request.getParameter("capacity"));
        int residents=Integer.parseInt(request.getParameter("residents"));
        IsolatedAreaBean area=new IsolatedAreaBean(wname, capacity, residents, hospital, address);
        IsolatedAreaDao dao=new IsolatedAreaDao();
        if(dao.update(area)) {
            request.setAttribute("message", "添加成功");
            request.getRequestDispatcher("isolatedAreaShowServlet").forward(request, response);
        }else {
            request.setAttribute("message", "添加失败");
            request.getRequestDispatcher("isolatedAreaShowServlet").forward(request, response);
        }

        
        
        
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}
insertIsolatedAreaServlet

 

 

posted @ 2020-07-14 12:06  酸奶面包  阅读(121)  评论(0编辑  收藏  举报