码迷,mamicode.com
首页 > 其他好文 > 详细

宠物管理系统

时间:2020-06-04 21:22:10      阅读:106      评论:0      收藏:0      [点我收藏+]

标签:prot   into   etc   arraylist   resultset   管理系   open   ddt   direct   

缺点:唯一性判断要求采用异步(jQuery Ajax)的方式进行实现(不是用该方法实现的)
使用的是servlet2.5
技术图片技术图片技术图片
技术图片
package dao;

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

import entity.Pet;

public class PetDaoImpl extends BaseDao implements PetDao {
    // 添加宠物
    public boolean insertpet(Pet pet) {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            // 连接对象
            conn = BaseDao.getConnection();
            String sql = "insert into pet(petName,petBreed,petSex,birthday,description)values(?,?,?,?,?)";
            // 获取执行sql语句
            ps = conn.prepareStatement(sql);
            // 执行sql语句
            ps.setString(1, pet.getPetName());
            ps.setInt(2, pet.getPetBreed());
            ps.setInt(3, pet.getPetSex());
            ps.setString(4, pet.getBirthday());
            ps.setString(5, pet.getDescription());
            int row = ps.executeUpdate();
            if (row > 0) {
                return true;
            } else {
                return false;
            }

        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            closeAll(conn, ps, rs);
        }
    }

    // 判断宠物是否存在 (如果存在返回true 不存在返回false)
    public boolean isExist(int petId) {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            // 获取连接
            conn = getConnection();
            // 编写sql语句
            String sql = "select * from pet where petId=?";
            ps = conn.prepareStatement(sql);
            ps.setInt(1, petId);
            // 执行sql语句
            rs = ps.executeQuery();
            if (rs.next()) {
                return true;
            }
            return false;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        } finally {
            closeAll(conn, ps, rs);
        }
    }

    // 根据品种查询宠物
    public List<Pet> sebreed(int petbreed) {
        Pet pet = null;
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<Pet> pets = new ArrayList<Pet>();
        try {
            // 获取连接
            conn = getConnection();
            // 编写sql语句
            String sql;
            // 如果品种等于0查询全部宠物,否则按相应查询
            sql = "select * from pet where petBreed=?";
            ps = conn.prepareStatement(sql);
            ps.setInt(1, petbreed);
            // 执行sql语句
            rs = ps.executeQuery();
            while (rs.next()) {
                String petName = rs.getString("petName");
                String petbirthday = rs.getString("birthday");
                int petSex = rs.getInt("petSex");
                pet = new Pet(petName, petSex, petbirthday);
                pets.add(pet);
            }
            return pets;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        } finally {
            closeAll(conn, ps, rs);
        }

    }

    // 查询宠物
    public List<Pet> selpet() {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<Pet> pets = new ArrayList<Pet>();
        try {
            // 获取连接
            conn = getConnection();
            // 编写sql语句
            String sql;
            // 如果品种等于0查询全部宠物,否则按相应查询
            sql = "select * from pet";
            ps = conn.prepareStatement(sql);
            // 执行sql语句
            rs = ps.executeQuery();
            while (rs.next()) {
                String petName = rs.getString("petName");
                String petbirthday = rs.getString("birthday");
                int petSex = rs.getInt("petSex");
            Pet    pet = new Pet(petName, petSex, petbirthday);
                pets.add(pet);
            }
            return pets;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        } finally {
            closeAll(conn, ps, rs);
        }
    }

    // public static void main(String[] args) {
    // PetDaoImpl pet=new PetDaoImpl();
    // try {
    //
    // System.out.println(pet.selpet("4"));
    // } catch (Exception e) {
    // // TODO Auto-generated catch block
    // e.printStackTrace();
    // }
    // }
    // 验证宠物昵称(根据宠物名称查询)
    public boolean sepetName(String petName) throws Exception {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        // 获取连接
        try {
            conn = getConnection();
            // 编写sql语句
            String sql;
            sql = "select * from pet where petName=?";
            ps = conn.prepareStatement(sql);
            ps.setString(1, petName);
            // 执行sql语句
            rs = ps.executeQuery();
            if (rs.next()) {
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll(conn, ps, rs);
        }
        return false;
    }
    // public static void main(String[] args) throws Exception {
    // Pet pet=PetDaoImpl.sepetName("旺旺");
    // try {
    // System.out.println(pet.getBirthday());
    // } catch (Exception e) {
    // // TODO Auto-generated catch block
    // e.printStackTrace();
    // }
    // }
}
PetDaoImpl
技术图片
package service;

import java.util.List;

import dao.PetDao;
import dao.PetDaoImpl;
import entity.Pet;

public class PetServiceImpl implements PetService {
    // 接口名 =new 实现类(多态)
    PetDao petdao = new PetDaoImpl();
    Pet pet = new Pet();

    // 获取全部宠物信息
    public List<Pet> selALLPet() throws Exception {
        return petdao.selpet();
    }

    // 根本品种获取宠物信息
    public List<Pet> sebreed(int petbreed) {
        return petdao.sebreed(petbreed);
    }

    // 添加宠物
    public boolean insertpet(Pet pet) {
        try {
            if (!petdao.isExist(pet.getPetId())) {
                petdao.insertpet(pet);
                return true;
            } else {
                return false;
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return false;
        }
    }

    // 宠物昵称判断(查询语句)
    public boolean isName(String petName) throws Exception {
        return petdao.sepetName(petName);
    }
}
PetServiceImpl
技术图片
package servlet;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

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.Pet;
import service.PetService;
import service.PetServiceImpl;
//@WebServlet("/AddServlet")
public class AddServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException  {
        request.setCharacterEncoding("utf-8");
        //获取数据
        String petName=request.getParameter("petid");
        int petBreed=Integer.parseInt(request.getParameter("invarieties"));
        int petSex=Integer.parseInt(request.getParameter("sex"));
        String birthday=request.getParameter("date");
        String description=request.getParameter("petdesc");
        //将获取的数据封装到实力类中
        Pet pet=new Pet(petName, petBreed,petSex, birthday,description);
        PetService petservice=new PetServiceImpl();
        boolean flag=petservice.insertpet(pet);
        if(flag){
            request.setAttribute("error", "addtrue");
        }else{
            request.setAttribute("error", "addflase");
        }
        try {
            request.getRequestDispatcher("SeAllServlet").forward(request, response);
        } catch (ServletException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

}
AddServlet
技术图片
package servlet;



import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import entity.Pet;
import service.PetService;
import service.PetServiceImpl;

public class SeAllServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PetService petservice=new PetServiceImpl();
        try {
            List<Pet>  pets=petservice.selALLPet();
            request.setAttribute("pets", pets);
            //因为request中有数据所以请求转发跳转(重定向会丢失数据)
            request.getRequestDispatcher("index.jsp").forward(request, response);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

}
SeAllServlet
技术图片
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.Pet;
import service.PetService;
import service.PetServiceImpl;


/**
 * Servlet implementation class Servlet
 */
//@WebServlet("/SebreedServlet")
public class SebreedServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int  petBreed=Integer.parseInt(request.getParameter("varieties"));
        if(petBreed==0){
            response.sendRedirect("SeAllServlet");
        }else{
        Pet pet=new Pet(petBreed);
        PetService petservice=new PetServiceImpl();

        try {
            List<Pet>    pets=petservice.sebreed(petBreed);
            request.setAttribute("pets", pets);
            //因为request中有数据所以请求转发跳转(重定向会丢失数据)
            request.getRequestDispatcher("index.jsp").forward(request, response);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        }
        
    }

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

}
SebreedServlet
技术图片
package servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import service.PetService;
import service.PetServiceImpl;

public class SepetNameServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String petName = request.getParameter("petid");
        PetService petservice = new PetServiceImpl();
        try {
            // 根据宠物名称查询(如果有此宠物返回true没有返回false赋值给flag)
            boolean flag = petservice.isName(petName);
            // 如果名称存在给增加页面传一个标识
            if (flag) {
                request.setAttribute("error", "adderror");
                request.getRequestDispatcher("addpet.jsp").forward(request, response);
            } else {
                request.getRequestDispatcher("AddServlet").forward(request, response);
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

}
SepetNameServlet

 

宠物管理系统

标签:prot   into   etc   arraylist   resultset   管理系   open   ddt   direct   

原文地址:https://www.cnblogs.com/w19991012/p/13045918.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!