2020.12.02

一、今日学习内容

     关于志愿者系统:

    Dao:

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 entity.Teenager;
import util.DBUtil;
public class TeenagerDao {
    
    public boolean add(Teenager ten)
    {
        String sql="insert into teenager(name,sex,minzu,shijian,age,mianmao,fuwu)values"
                + "('" + ten.getName() + "','" + ten.getSex() + "','" + ten.getMinzu() + "','" + ten.getShijian() + "','" + ten.getAge() + "','" + ten.getMianmao() + "','" + ten.getFuwu() + "')";
        //创建数据库链接
        Connection conn=DBUtil.getConn();
        Statement state=null;
        boolean f=false;
        int a = 0;
        try {
            state = conn.createStatement();
            a=state.executeUpdate(sql);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            //关闭连接
            DBUtil.close(state, conn);
        }
        if(a>0)
            f=true;
        return f;
    }
    
    public Teenager getbyname(String name) {
        String sql = "select * from teenager where name ='" + name + "'";
        Connection conn = DBUtil.getConn();
        Statement state = null;
        ResultSet rs = null;
        Teenager ten = null;
        
        try {
            state = conn.createStatement();
            rs = state.executeQuery(sql);
            while (rs.next()) {
                int id=rs.getInt("id");
                String sex2 = rs.getString("sex");
                String minzu2 = rs.getString("minzu");
                String age=rs.getString("age");
                String mianmao2 = rs.getString("mianmao");
                String fuwu2 =rs.getString("fuwu");
                String shijian2=rs.getString("shijian");
                ten = new Teenager(id,name, sex2, minzu2,shijian2,age,mianmao2,fuwu2);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(rs, state, conn);
        }
        
        return ten;
    }
    
    
    //delete
        public boolean delete(String name)
        {
            String sql="delete * from teenager where name='" + name + "'";
            Connection conn = DBUtil.getConn();
            Statement state = null;
            int a = 0;
            boolean f = false;
            try {
                state = conn.createStatement();
                a=state.executeUpdate(sql);
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                DBUtil.close(state, conn);
            }
            
            if (a > 0) {
                f = true;
            }
            return f;
        }
        //update
        public boolean update(Teenager ten)
        {
            String sql="update teenager set name='" + ten.getName() + "',sex='" + ten.getSex() + "',minzu='" + ten.getMinzu() + "',shijian='" + ten.getShijian() + "',age='" + ten.getAge() + "',mianmao='" + ten.getMianmao() + "',fuwu='" + ten.getFuwu()
            + "' where id='" + ten.getId() + "'";
             Connection conn = DBUtil.getConn();
                Statement state = null;
                boolean f = false;
                int a = 0;

                try {
                    state = conn.createStatement();
                    a = state.executeUpdate(sql);
                } catch (SQLException e) {
                    e.printStackTrace();
                } finally {
                    DBUtil.close(state, conn);
                }
                
                if (a > 0) {
                    f = true;
                }
                return f;
        }
        //姓名不得重复
        public boolean name(String name) {
            boolean flag = false;
            String sql = "select name from teenager where name = '" + name + "'";
            Connection conn = DBUtil.getConn();
            Statement state = null;
            ResultSet rs = null;
            
            try {
                state=conn.createStatement();
                rs = state.executeQuery(sql);
                while (rs.next()) {
                    flag = true;
                }
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                DBUtil.close(rs, state, conn);
            }
            return flag;
        }
        
        //查找
        public List<Teenager> search(String name,String sex,String minzu,String mianmao,String fuwu,String shijian)
        {
            String sql = "select * from teenager where ";
            if (name != "") {
                sql += "name like '%" +name+ "%'";
            }
            if (sex != "") {
                sql += "sex like '%" +sex+ "%'";
            }
            if(minzu!="") {
                sql+="minzu like '%"+minzu+ "%'";
            }
            if(mianmao!="") {
                sql+="mianmao like '%" +mianmao+ "%'";
            }
            if(fuwu!="") {
                sql+="fuwu like '%" +fuwu+ "%'";
            }
            if(shijian!="") {
                sql+="shijian like '%" +shijian+ "%'";
            }
            List<Teenager> list = new ArrayList<>();
            Connection conn = DBUtil.getConn();
            Statement state = null;
            ResultSet rs = null;
            Teenager bean = null;
            try {
                state = conn.createStatement();
                rs = state.executeQuery(sql);
                while (rs.next()) {
                    String name2=rs.getString("name");
                    String sex2 = rs.getString("sex");
                    String minzu2 = rs.getString("minzu");
                    String age=rs.getString("age");
                    String mianmao2 = rs.getString("mianmao");
                    String fuwu2 =rs.getString("fuwu");
                    String shijian2=rs.getString("shijian");
                    bean =new Teenager(name2, sex2, minzu2,shijian2,age,mianmao2,fuwu2);
                    list.add(bean);
                }
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                DBUtil.close(rs, state, conn);
            }
            
            return list;
        }
        //全部数据
        public List<Teenager> list()
        {
            String sql = "select * from teenager";
            List<Teenager> list = new ArrayList<>();
            Connection conn = DBUtil.getConn();
            Statement state = null;
            ResultSet rs = null;
            
            try {
                state = conn.createStatement();
                rs = state.executeQuery(sql);
                while (rs.next()) {
                    Teenager bean = null;
                    int id=rs.getInt("id");
                    String name2=rs.getString("name");
                    String sex2 = rs.getString("sex");
                    String minzu2 = rs.getString("minzu");
                    String age=rs.getString("age");
                    String mianmao2 = rs.getString("mianmao");
                    String fuwu2 =rs.getString("fuwu");
                    String shijian2=rs.getString("shijian");
                    bean =new Teenager(id,name2, sex2, minzu2,shijian2,age,mianmao2,fuwu2);
                    list.add(bean);
                }
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                DBUtil.close(rs, state, conn);
            }
            
            return list;
        }
}

service:

package service;
import java.util.List;
import Dao.TeenagerDao;
import entity.Teenager;
public class TeenagerService {
    TeenagerDao tDao=new TeenagerDao();
    public boolean add(Teenager ten)
    {
        boolean f = false;
        if(!tDao.name(ten.getName()))
        {
            tDao.add(ten);
            f=true;
        }
        return f;
    }
    
    public boolean del(String name)
    {
        tDao.delete(name);
        return true;
    }
    
    public boolean update(Teenager ten)
    {
        tDao.update(ten);
        return true;
    }
    
    public Teenager getbyname(String name)
    {
        return tDao.getbyname(name);
    }
    
    public List<Teenager> search(String name,String sex,String minzu,String mianmao,String fuwu,String shijian) {
        return tDao.search(name, sex,minzu,mianmao,fuwu,shijian);
    }
    
    //查找所有人
    public List<Teenager> list()
    {
        return tDao.list();
    }
}

entity:

package entity;

public class Teenager {
    private int id;
    private String name;
    private String sex;
    private String minzu;
    private String shijian;
    private String age;
    private String mianmao;
    private String fuwu;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    public String getMinzu() {
        return minzu;
    }
    public void setMinzu(String minzu) {
        this.minzu = minzu;
    }
    public String getShijian() {
        return shijian;
    }
    public void setShijian(String shijian) {
        this.shijian = shijian;
    }
    public String getAge() {
        return age;
    }
    public void setAge(String age) {
        this.age = age;
    }
    public String getMianmao() {
        return mianmao;
    }
    public void setMianmao(String mianmao) {
        this.mianmao = mianmao;
    }
    public String getFuwu() {
        return fuwu;
    }
    public void setFuwu(String fuwu) {
        this.fuwu = fuwu;
    }
    public Teenager(int id, String name, String sex, String minzu, String shijian, String age, String mianmao,String fuwu) {
        super();
        this.id = id;
        this.name = name;
        this.sex = sex;
        this.minzu = minzu;
        this.shijian = shijian;
        this.age = age;
        this.mianmao = mianmao;
        this.fuwu = fuwu;
    }
    public Teenager(String name, String sex, String minzu, String shijian, String age, String mianmao, String fuwu) {
        super();
        this.name = name;
        this.sex = sex;
        this.minzu = minzu;
        this.shijian = shijian;
        this.age = age;
        this.mianmao = mianmao;
        this.fuwu = fuwu;
    }
    public Teenager() {}
}

servlet:

package servlet;
import java.io.IOException;
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 entity.Teenager;
import service.TeenagerService;
@WebServlet("/TeenagerServlet")
public class TeenagerServlet extends HttpServlet{
    private static final long serialVersionUID = 1L;

    TeenagerService service = new TeenagerService();
    
    /**
     * 方法选择
     */
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String method = req.getParameter("method");
        
        if ("add".equals(method)) {
            add(req,resp);
        } else if ("del".equals(method)) {
            del(req, resp);
        } else if ("update".equals(method)) {
            update(req, resp);
        } else if ("search".equals(method)) {
            search(req, resp);
        } else if ("list".equals(method)) {
            list(req, resp);
        } else if("getbyname".equals(method)) {
            getbyname(req,resp);
        }else if("getbyname2".equals(method)) {
            getbyname2(req,resp);
        }
    }
    
    private void getbyname(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{
        req.setCharacterEncoding("utf-8");
        String name = req.getParameter("name");
        Teenager ten = service.getbyname(name);
        if(ten==null)
        {
            req.setAttribute("message", "未找到该学生");
            req.getRequestDispatcher("del.jsp").forward(req, resp);
        }
        else 
        {
            req.setAttribute("ten", ten);
            req.getRequestDispatcher("del2.jsp").forward(req,resp);
        }
    }
    
    private void getbyname2(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{
        req.setCharacterEncoding("utf-8");
        String name = req.getParameter("name");
        Teenager ten = service.getbyname(name);
        if(ten==null)
        {
            req.setAttribute("message", "未找到该学生");
            req.getRequestDispatcher("xiugai.jsp").forward(req, resp);
        }
        else 
        {
            req.setAttribute("ten", ten);
            req.getRequestDispatcher("update.jsp").forward(req,resp);
        }
    }
    
    private void add(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        req.setCharacterEncoding("utf-8");
        //获取数据
        String name=req.getParameter("name");
        String sex = req.getParameter("sex");
        String minzu = req.getParameter("minzu");
        String shijian = req.getParameter("shijian");
        String age =req.getParameter("age");
        String mianmao=req.getParameter("mianmao");
        
        String[] fuwus = req.getParameterValues("fuwu");
           StringBuffer buf = new StringBuffer(); 
           for(String fuwu :   fuwus){
              buf.append(fuwu);
          }
          String fuwu = buf.toString();
        Teenager ten =new Teenager(name, sex, minzu,shijian,age,mianmao,fuwu);
        //添加后消息显示
        if(service.add(ten)) {
            req.setAttribute("message", "添加成功");
            req.getRequestDispatcher("add.jsp").forward(req,resp);
        } else {
            req.setAttribute("message", "姓名重复,请重新录入");
            req.getRequestDispatcher("add.jsp").forward(req,resp);
        }
    }
    
    private void list(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{
        req.setCharacterEncoding("utf-8");
        
        List<Teenager> tens = service.list();
        req.setAttribute("tens", tens);
        req.getRequestDispatcher("list.jsp").forward(req,resp);
    }
    
    private void del(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{
        req.setCharacterEncoding("utf-8");
        String name=req.getParameter("name");
        service.del(name);
        req.setAttribute("message", "删除成功!");
        req.getRequestDispatcher("del.jsp").forward(req,resp);
    }
    
    private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{
        req.setCharacterEncoding("utf-8");
        int id = Integer.parseInt(req.getParameter("id"));
        String name=req.getParameter("name");
        String sex = req.getParameter("sex");
        String minzu = req.getParameter("minzu");
        String shijian = req.getParameter("shijian");
        String age =req.getParameter("age");
        String mianmao=req.getParameter("mianmao");
        int num=0;
        String [] fuwus = req.getParameterValues("fuwu");
           StringBuffer buf = new StringBuffer(); 
           for(String fuwu :   fuwus){
              buf.append(fuwu);
              num++;
          }
          String fuwu = buf.toString();
          if(num>4)
          {
                  req.setAttribute("message", "服务类别不得超过四个");
                req.getRequestDispatcher("TeenagerServlet?method=getbyname2").forward(req,resp);
          }
        Teenager ten =new Teenager(id,name, sex, minzu,shijian,age,mianmao,fuwu);
        service.update(ten);
        req.setAttribute("message", "修改成功");
        req.getRequestDispatcher("TeenagerServlet?method=list").forward(req,resp);
    }
    
    private void search(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{
        req.setCharacterEncoding("utf-8");
        String name=req.getParameter("name");
        String sex = req.getParameter("sex");
        
        String minzu = req.getParameter("minzu");
        String shijian = req.getParameter("shijian");
        String mianmao=req.getParameter("mianmao");
        String fuwu=req.getParameter("fuwu");
        List<Teenager> tens = service.search(name, sex,minzu,mianmao,fuwu,shijian);
        if(tens==null)
        {
            req.setAttribute("message", "没有该学生");
            req.getRequestDispatcher("search.jsp").forward(req,resp);
        }
        else {
        req.setAttribute("tens", tens);
        req.getRequestDispatcher("list2.jsp").forward(req,resp);
        }
    }
}

DBUtil:

package util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
* ���ݿ����ӹ���
* @author Zheng
*
*/
public class DBUtil {
  
  public static String db_url = "jdbc:mysql://localhost:3306/test?useSSL=false";
  public static String db_user = "root";
  public static String db_pass = "08301016";
  
  public static Connection getConn () {
      Connection conn = null;
      
      try {
          Class.forName("com.mysql.jdbc.Driver");//��������
          conn = DriverManager.getConnection(db_url, db_user, db_pass);
      } catch (Exception e) {
          e.printStackTrace();
      }
      
      return conn;
  }
  
  /**
   * �ر�����
   * @param state
   * @param conn
   */
  public static void close (Statement state, Connection conn) {
      if (state != null) {
          try {
              state.close();
          } catch (SQLException e) {
              e.printStackTrace();
          }
      }
      
      if (conn != null) {
          try {
              conn.close();
          } catch (SQLException e) {
              e.printStackTrace();
          }
      }
  }
  
  public static void close (ResultSet rs, Statement state, Connection conn) {
      if (rs != null) {
          try {
              rs.close();
          } catch (SQLException e) {
              e.printStackTrace();
          }
      }
      
      if (state != null) {
          try {
              state.close();
          } catch (SQLException e) {
              e.printStackTrace();
          }
      }
      
      if (conn != null) {
          try {
              conn.close();
          } catch (SQLException e) {
              e.printStackTrace();
          }
      }
  }
  
  public static void main(String[] args) throws SQLException {
      Connection conn = getConn();
      PreparedStatement pstmt = null;
      ResultSet rs = null;
      String sql ="select * from teenager";
      pstmt = conn.prepareStatement(sql);
      rs = pstmt.executeQuery();
      if(rs.next()){
          System.out.println("空");
      }else{
          System.out.println("不空");
      }
  }

}

二、遇到的问题:

    开始的数据传不到数据库

三、明日计划

    明天找错误原因

posted @ 2020-12-02 16:31  小仙女W  阅读(178)  评论(0编辑  收藏  举报