Java+Servlet+JSP实现Web宠物诊所管理系统(下)

简介: Java+Servlet+JSP实现Web宠物诊所管理系统

petupdate.jsp

<%@ page language="java" import="cn.dao.UserBean" pageEncoding="UTF-8" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://"
            + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <base href="<%=basePath%>">
    <title>宠物修改</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <link href="css/css.css" rel="stylesheet" type="text/css">
    <style type="text/css">
        #content {
            position: relative;
            top: 3px;
            width: 80%;
            height: 380px;
            margin: auto;
            overflow: hidden;
            z-index: 2;
        }
        .tab1 {
            position: relative;
            top: 60px;
            font-size: 24px;
        }
        #in1, #in2, #in3, #in4 ,#in6{
            width: 250px;
            height: 30px;
            font-size: 24px;
        }
        #in5 {
            width: 80px;
            height: 28px;
            margin-top: 10px;
        }
    </style>
    <script type="text/javascript">
        function checkId() {
            if (document.getElementById("in1").value == ""
                && document.getElementById("in3").value == ""
                && document.getElementById("in2").value == ""
                && document.getElementById("in6").value == "") {
                alert("请输入宠物名相关信息!");
                return false;
            } else if (document.getElementById("in1").value == "") {
                alert("请输入宠物名!");
                return false;
            } else if (document.getElementById("in3").value == "") {
                alert("请输入主人!");
                return false;
            } else if (document.getElementById("in6").value == "") {
                alert("请输入电话!");
                return false;
            }else {
                return true;
            }
        }
    </script>
</head>
<body>
<jsp:include page="top.jsp"></jsp:include>
<!-- include top -->
<%
    UserBean user = (UserBean) request.getAttribute("user");
%>
<center>
    <div id="content">
        <br/>
        <form action="servlet/petupdate" method="post" onsubmit="return checkId()">
            <table border="0" cellpadding="0" cellspacing="2" class="tab1">
                <tr align="center">
                    <td>宠物名&nbsp;</td>
                    <td><input type="text" name="petname" id="in1" value=<%=user.getPetname()%>>*
                    </td>
                </tr>
                <tr align="center">
                    <td>主人&nbsp;</td>
                    <td><input type="text" name="petmaster" id="in3" value=<%=user.getPetmaster()%>>*
                    </td>
                </tr>
                <tr align="center">
                    <td>城市&nbsp;</td>
                    <td><input type="text" name="petcity" id="in2"
                               value=<% if(user.getPetcity()==null){out.print(" ");}else{out.print(user.getPetcity());} %>>&nbsp;&nbsp;
                    </td>
                </tr>
                <tr align="center">
                    <td>地址&nbsp;</td>
                    <td><input type="text" name="petadress" id="in4"
                               value=<% if(user.getPetadress()==null){out.print(" ");}else{out.print(user.getPetadress());} %>>&nbsp;&nbsp;
                    </td>
                </tr>
                <tr align="center">
                    <td>电话&nbsp;</td>
                    <td><input type="text" name="petphone" id="in6"
                               value=<% if(user.getPetphone()==null){out.print(" ");}else{out.print(user.getPetphone());} %>>*
                    </td>
                </tr>
                <tr align="center">
                    <td colspan="2">
                        <input type="hidden" name="id" value=<%=user.getId() %>>
                        <input type="submit" value="提交" id="in5"/>
                        <input type="reset" value="重置" id="in5"/>
                    </td>
                </tr>
            </table>
        </form>
    </div>
</center>
<!-- include footer -->
<jsp:include page="footer.jsp"></jsp:include>
</body>
</html>

petsearch.jsp

<%@ page language="java" pageEncoding="UTF-8" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <base href="<%=basePath%>">
    <title>宠物查询</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <link href="css/css.css" rel="stylesheet" type="text/css">
    <style type="text/css">
        #content {
            position: relative;
            top: 3px;
            width: 80%;
            height: 380px;
            margin: auto;
            overflow: hidden;
            z-index: 2;
        }
        .tab1 {
            position: relative;
            top: 120px;
            font-size: 24px;
        }
        #in1 {
            width: 250px;
            height: 30px;
            font-size: 24px;
        }
        #in3 {
            width: 250px;
            height: 30px;
            font-size: 24px;
        }
        #in2 {
            width: 80px;
            height: 28px;
        }
    </style>
    <script type="text/javascript">
        function checkId() {
            if (document.getElementById("in1").value == ""
                && document.getElementById("in3").value == "") {
                alert("请输入宠物名或所有者名!");
                return false;
            } else if (document.getElementById("in1").value != ""
                && document.getElementById("in3").value != "") {
                alert("模糊查询只能输入一个!");
                return false;
            } else {
                return true;
                ;
            }
        }
    </script>
</head>
<body>
<jsp:include page="top.jsp"></jsp:include>
<!-- include top -->
<center>
    <div id="content">
        <form action="servlet/petquery" method="post" onsubmit="return checkId()">
            <table border="0" cellpadding="0" cellspacing="2" class="tab1">
                <tr align="center">
                    <td>宠物名:</td>
                    <td><input type="text" name="petname" id="in1"/></td>
                </tr>
                <tr align="center">
                    <td>主 &nbsp;人:</td>
                    <td><input type="text" name="petmaster" id="in3"/></td>
                </tr>
                <tr>
                    <td align="center" colspan="2">
                        <input type="submit" value="查询" id="in2"/>
                        <input type="reset" value="重置" id="in2"/>
                        <a href="petadd.jsp"><i><font color="gray" size="3" id="in2">添加</font></i></a>
                    </td>
                </tr>
            </table>
        </form>
    </div>
</center>
<!-- include footer -->
<jsp:include page="footer.jsp"></jsp:include>
</body>
</html>

docadd.jsp

<%@ page language="java" pageEncoding="UTF-8" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://"
            + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <base href="<%=basePath%>">
    <title>医生新增</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <link href="css/css.css" rel="stylesheet" type="text/css">
    <style type="text/css">
        #content {
            position: relative;
            top: 3px;
            width: 80%;
            height: 380px;
            margin: auto;
            overflow: hidden;
            z-index: 2;
        }
        .tab1 {
            position: relative;
            top: 60px;
            font-size: 24px;
        }
        #in1, #in2, #in3, #in4 {
            width: 250px;
            height: 30px;
            font-size: 24px;
        }
        #in5 {
            width: 80px;
            height: 28px;
            margin-top: 10px;
        }
    </style>
    <script type="text/javascript">
        function checkId() {
            if (document.getElementById("in1").value == ""
                && document.getElementById("in3").value == "" && document.getElementById("in2").value == "") {
                alert("请输入兽医姓名和专业!");
                return false;
            } else if (document.getElementById("in1").value == "") {
                alert("请输入姓名");
                return false;
            } else if (document.getElementById("in3").value == "") {
                alert("请输入专业!");
                return false;
            } else {
                return true;
            }
        }
    </script>
</head>
<body>
<jsp:include page="top.jsp"></jsp:include>
<!-- include top -->
<center>
    <div id="content">
        <br/>
        <form action="servlet/docadd" method="post" onsubmit="return checkId()">
            <table border="0" cellpadding="0" cellspacing="2" class="tab1">
                <tr align="center">
                    <td>姓名&nbsp;</td>
                    <td><input type="text" name="name" id="in1"/>*
                    </td>
                </tr>
                <tr align="center">
                    <td>专业&nbsp;</td>
                    <td><input type="text" name="major" id="in3"/>*
                    </td>
                </tr>
                <tr align="center">
                    <td>电话&nbsp;</td>
                    <td><input type="text" name="phone" id="in2"/>&nbsp;&nbsp;
                    </td>
                </tr>
                <tr align="center">
                    <td>地址&nbsp;</td>
                    <td><input type="text" name="address" id="in4"/>&nbsp;&nbsp;
                    </td>
                </tr>
                <tr align="center">
                    <td colspan="2">
                        <input type="submit" value="提交" id="in5"/>
                        <input type="reset" value="重置" id="in5"/>
                    </td>
                </tr>
            </table>
        </form>
    </div>
</center>
<!-- include footer -->
<jsp:include page="footer.jsp"></jsp:include>
</body>
</html>

docupdate.jsp

<%@ page language="java" import="cn.dao.UserBean" pageEncoding="UTF-8" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://"
            + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <base href="<%=basePath%>">
    <title>医生修改</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <link href="css/css.css" rel="stylesheet" type="text/css">
    <style type="text/css">
        #content {
            position: relative;
            top: 3px;
            width: 80%;
            height: 380px;
            margin: auto;
            overflow: hidden;
            z-index: 2;
        }
        .tab1 {
            position: relative;
            top: 60px;
            font-size: 24px;
        }
        #in1, #in2, #in3, #in4 {
            width: 250px;
            height: 30px;
            font-size: 24px;
        }
        #in5 {
            width: 80px;
            height: 28px;
            margin-top: 10px;
        }
    </style>
    <script type="text/javascript">
        function checkId() {
            if (document.getElementById("in1").value == ""
                && document.getElementById("in3").value == "" && document.getElementById("in2").value == "") {
                alert("请输入兽医姓名和专业!");
                return false;
            } else if (document.getElementById("in1").value == "") {
                alert("请输入姓名");
                return false;
            } else if (document.getElementById("in3").value == "") {
                alert("请输入专业!");
                return false;
            } else {
                return true;
            }
        }
    </script>
</head>
<body>
<jsp:include page="top.jsp"></jsp:include>
<!-- include top -->
<%
    UserBean user = (UserBean) request.getAttribute("user");
%>
<center>
    <div id="content">
        <br/>
        <form action="servlet/docupdate" method="post" onsubmit="return checkId()">
            <table border="0" cellpadding="0" cellspacing="2" class="tab1">
                <tr align="center">
                    <td>姓名&nbsp;</td>
                    <td><input type="text" name="name" id="in1" value=<%=user.getDocname()%>>*
                    </td>
                </tr>
                <tr align="center">
                    <td>专业&nbsp;</td>
                    <td><input type="text" name="major" id="in3" value=<%=user.getDocmajor()%>>*
                    </td>
                </tr>
                <tr align="center">
                    <td>电话&nbsp;</td>
                    <td><input type="text" name="phone" id="in2"
                               value=<% if(user.getDocphone()==null){out.print(" ");}else{out.print(user.getDocphone());} %>>&nbsp;&nbsp;
                    </td>
                </tr>
                <tr align="center">
                    <td>地址&nbsp;</td>
                    <td><input type="text" name="address" id="in4"
                               value=<% if(user.getDocadress()==null){out.print(" ");}else{out.print(user.getDocadress());} %>>&nbsp;&nbsp;
                    </td>
                </tr>
                <tr align="center">
                    <td colspan="2">
                        <input type="hidden" name="id" value=<%=user.getId() %>>
                        <input type="submit" value="提交" id="in5"/>
                        <input type="reset" value="重置" id="in5"/>
                    </td>
                </tr>
            </table>
        </form>
    </div>
</center>
<!-- include footer -->
<jsp:include page="footer.jsp"></jsp:include>
</body>
</html>

docsearch.jsp

<%@ page language="java" pageEncoding="UTF-8" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <base href="<%=basePath%>">
    <title>医生查询</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <link href="css/css.css" rel="stylesheet" type="text/css">
    <style type="text/css">
        #content {
            position: relative;
            top: 3px;
            width: 80%;
            height: 380px;
            margin: auto;
            overflow: hidden;
            z-index: 2;
        }
        .tab1 {
            position: relative;
            top: 120px;
            font-size: 24px;
        }
        #in1 {
            width: 250px;
            height: 30px;
            font-size: 24px;
        }
        #in3 {
            width: 250px;
            height: 30px;
            font-size: 24px;
        }
        #in2 {
            width: 80px;
            height: 28px;
        }
    </style>
    <script type="text/javascript">
        function checkId() {
            if (document.getElementById("in1").value == ""
                && document.getElementById("in3").value == "") {
                alert("请输入兽医名或专业!");
                return false;
            } else if (document.getElementById("in1").value != ""
                && document.getElementById("in3").value != "") {
                alert("少年,两个都输了,您这是要闹哪样啊!");
                return false;
            } else {
                return true;
                ;
            }
        }
    </script>
</head>
<body>
<script type="text/javascript">
    <%
    if(session.getAttribute("update").equals("del")){
        %>
    alert("删除成功!");
    <%
}else if(session.getAttribute("update").equals("delno")){
     %>
    alert("删除失败 !");
    <%
 }else if(session.getAttribute("update").equals("suc")){
     %>
    alert("添加成功 !");
    <%
 }else if(session.getAttribute("update").equals("err")){
     %>
    alert("添加失败 !");
    <%
    }else if(session.getAttribute("update").equals("ok")){
     %>
    alert("编辑成功 !");
    <%
    }
    session.setAttribute("update", "null");
      %>
    <%
    if(session.getAttribute("error").equals("cuo")){
    %>
    alert("出错了!");
    <% }
    session.setAttribute("error", "test");
     %>
</script>
<jsp:include page="top.jsp"></jsp:include>
<!-- include top -->
<center>
    <div id="content">
        <form action="servlet/docquery" method="post" onsubmit="return checkId()">
            <table border="0" cellpadding="0" cellspacing="2" class="tab1">
                <tr align="center">
                    <td>兽医姓名:</td>
                    <td><input type="text" name="docname" id="in1"/></td>
                </tr>
                <tr align="center">
                    <td>专 &nbsp;业:</td>
                    <td><input type="text" name="docmajor" id="in3"/></td>
                </tr>
                <tr>
                    <td align="center" colspan="2">
                        <input type="submit" value="查询" id="in2"/>
                        <input type="reset" value="重置" id="in2"/>
                        <a href="docadd.jsp"><i><font color="gray" size="3" id="in2">添加</font></i></a>
                    </td>
                </tr>
            </table>
        </form>
    </div>
</center>
<!-- include footer -->
<jsp:include page="footer.jsp"></jsp:include>
</body>
</html>

2.后端


DBConn

package cn.dao;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class DocManage {
    // 空构造方法
    public DocManage() {
    }
    // 添加兽医
    public int addPetdoc(UserBean user) {
        String sql = "INSERT INTO petdoctor(docname,docmajor,docphone,docadress) VALUES (?,?,?,?)";
        int i = 0;
        try {
            DBConn.prepareConnection();
            DBConn.con.setAutoCommit(false);
            DBConn.ps = DBConn.con.prepareStatement(sql);
            DBConn.ps.setString(1, user.getDocname());
            DBConn.ps.setString(2, user.getDocmajor());
            DBConn.ps.setString(3, user.getDocphone());
            DBConn.ps.setString(4, user.getDocadress());
            i = DBConn.ps.executeUpdate();
            DBConn.con.commit();
        } catch (SQLException e) {
            DBConn.rollback();
            e.printStackTrace();
        } finally {
            DBConn.close();
        }
        return i;
    }
    // 删除兽医
    public int delPetdocForId(UserBean user) {
        String sql = "DELETE FROM petdoctor WHERE id=?";
        int i = 0;
        try {
            DBConn.prepareConnection();
            DBConn.con.setAutoCommit(false);
            DBConn.ps = DBConn.con.prepareStatement(sql);
            DBConn.ps.setInt(1, user.getId());
            i = DBConn.ps.executeUpdate();
            DBConn.con.commit();
        } catch (SQLException e) {
            DBConn.rollback();
            e.printStackTrace();
        } finally {
            DBConn.close();
        }
        return i;
    }
    // 通过id删除兽医
    public int delPetdocForId(int id) {
        String sql = "DELETE FROM petdoctor WHERE id=?";
        int i = 0;
        try {
            DBConn.prepareConnection();
            DBConn.con.setAutoCommit(false);
            DBConn.ps = DBConn.con.prepareStatement(sql);
            DBConn.ps.setInt(1, id);
            i = DBConn.ps.executeUpdate();
            DBConn.con.commit();
        } catch (SQLException e) {
            DBConn.rollback();
            e.printStackTrace();
        } finally {
            DBConn.close();
        }
        return i;
    }
    // 更新兽医
    public int updatePetdocForId(UserBean user) {
        String sql = "UPDATE petdoctor SET docname=?, docmajor=?,docphone=?,docadress=? WHERE id=?";
        int i = 0;
        try {
            DBConn.prepareConnection();
            DBConn.con.setAutoCommit(false);
            DBConn.ps = DBConn.con.prepareStatement(sql);
            DBConn.ps.setString(1, user.getDocname());
            DBConn.ps.setString(2, user.getDocmajor());
            DBConn.ps.setString(3, user.getDocphone());
            DBConn.ps.setString(4, user.getDocadress());
            DBConn.ps.setInt(5, user.getId());
            i = DBConn.ps.executeUpdate();
            DBConn.con.commit();
        } catch (SQLException e) {
            DBConn.rollback();
            e.printStackTrace();
        } finally {
            DBConn.close();
        }
        return i;
    }
    // 获取所有兽医
    public List<UserBean> getAllPetdoc() {
        String sql = "SELECT * FROM petdoctor";
        List<UserBean> users = new ArrayList<UserBean>();
        try {
            DBConn.prepareConnection();
            DBConn.ps = DBConn.con.prepareStatement(sql);
            ResultSet rs = DBConn.ps.executeQuery();
            while (rs.next()) {
                UserBean user = new UserBean();
                user.setId(rs.getInt("id"));
                user.setDocname(rs.getString("docname"));
                user.setDocmajor(rs.getString("docmajor"));
                user.setDocphone(rs.getString("docphone"));
                user.setDocadress(rs.getString("docadress"));
                users.add(user);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBConn.close();
        }
        return users;
    }
    // 通过兽医名获取兽医
    public List<UserBean> getPetdocForName(UserBean use) {
        String sql = "SELECT * FROM petdoctor WHERE docname=?";
        List<UserBean> users = new ArrayList<UserBean>();
        try {
            DBConn.prepareConnection();
            DBConn.ps = DBConn.con.prepareStatement(sql);
            DBConn.ps.setString(1, use.getDocname());
            ResultSet rs = DBConn.ps.executeQuery();
            while (rs.next()) {
                UserBean user = new UserBean();
                user.setId(rs.getInt("id"));
                user.setDocname(rs.getString("docname"));
                user.setDocmajor(rs.getString("docmajor"));
                user.setDocphone(rs.getString("docphone"));
                user.setDocadress(rs.getString("docadress"));
                users.add(user);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBConn.close();
        }
        return users;
    }
    // 通过兽医专业获取兽医
    public List<UserBean> getPetdocForMajor(UserBean use) {
        String sql = "SELECT * FROM petdoctor WHERE docmajor=?";
        List<UserBean> users = new ArrayList<UserBean>();
        try {
            DBConn.prepareConnection();
            DBConn.ps = DBConn.con.prepareStatement(sql);
            DBConn.ps.setString(1, use.getDocmajor());
            ResultSet rs = DBConn.ps.executeQuery();
            while (rs.next()) {
                UserBean user = new UserBean();
                user.setId(rs.getInt("id"));
                user.setDocname(rs.getString("docname"));
                user.setDocmajor(rs.getString("docmajor"));
                user.setDocphone(rs.getString("docphone"));
                user.setDocadress(rs.getString("docadress"));
                users.add(user);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBConn.close();
        }
        return users;
    }
    // 通过兽医id获取兽医
    public UserBean getPetdocForId(int id) {
        String sql = "SELECT * FROM petdoctor WHERE id=?";
        UserBean user = new UserBean();
        try {
            DBConn.prepareConnection();
            DBConn.ps = DBConn.con.prepareStatement(sql);
            DBConn.ps.setInt(1, id);
            ResultSet rs = DBConn.ps.executeQuery();
            while (rs.next()) {
                user.setId(rs.getInt("id"));
                user.setDocname(rs.getString("docname"));
                user.setDocmajor(rs.getString("docmajor"));
                user.setDocphone(rs.getString("docphone"));
                user.setDocadress(rs.getString("docadress"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBConn.close();
        }
        return user;
    }
    // 通过兽医名获取兽医
    public Integer getIdForName(UserBean user) {
        String sql = "SELECT * FROM petdoctor WHERE docname=?";
        Integer i = 0;
        try {
            DBConn.prepareConnection();
            DBConn.ps = DBConn.con.prepareStatement(sql);
            DBConn.ps.setString(1, user.getLoginname());
            ResultSet rs = DBConn.ps.executeQuery();
            while (rs.next()) {
                i = rs.getInt("id");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBConn.close();
        }
        return i;
    }
}

UserManage

package cn.dao;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class UserManage {
    // 空构造方法
    public UserManage() {
    }
    // 添加用户
    public int addUser(UserBean user) {
        String sql = "INSERT INTO userinfo(username,password,userphone) VALUES (?,?,?)";
        int i = 0;
        try {
            DBConn.prepareConnection();
            DBConn.con.setAutoCommit(false);
            DBConn.ps = DBConn.con.prepareStatement(sql);
            DBConn.ps.setString(1, user.getLoginname());
            DBConn.ps.setString(2, user.getLoginpassword());
            DBConn.ps.setString(3, user.getLoginphone());
            i = DBConn.ps.executeUpdate();
            DBConn.con.commit();
        } catch (SQLException e) {
            DBConn.rollback();
            System.out.println("插入数据失败!");
            i = 2;
        } finally {
            DBConn.close();
        }
        return i;
    }
    // 删除用户
    public int delUserForId(UserBean user) {
        String sql = "DELETE FROM userinfo WHERE id=?";
        int i = 0;
        try {
            DBConn.prepareConnection();
            DBConn.con.setAutoCommit(false);
            DBConn.ps = DBConn.con.prepareStatement(sql);
            DBConn.ps.setInt(1, user.getId());
            i = DBConn.ps.executeUpdate();
            DBConn.con.commit();
        } catch (SQLException e) {
            DBConn.rollback();
            e.printStackTrace();
        } finally {
            DBConn.close();
        }
        return i;
    }
    // 更新用户
    public int updateUseForId(UserBean user) {
        String sql = "UPDATE userinfo SET username=?, password=?,userphone=? WHERE id=?";
        int i = 0;
        try {
            DBConn.prepareConnection();
            DBConn.con.setAutoCommit(false);
            DBConn.ps = DBConn.con.prepareStatement(sql);
            DBConn.ps.setString(1, user.getLoginname());
            DBConn.ps.setString(2, user.getLoginpassword());
            DBConn.ps.setString(3, user.getLoginphone());
            i = DBConn.ps.executeUpdate();
            DBConn.con.commit();
        } catch (SQLException e) {
            DBConn.rollback();
            e.printStackTrace();
        } finally {
            DBConn.close();
        }
        return i;
    }
    // 获取所有用户
    public List<UserBean> getAllUsers() {
        String sql = "SELECT * FROM userinfo";
        List<UserBean> users = new ArrayList<UserBean>();
        try {
            DBConn.prepareConnection();
            DBConn.ps = DBConn.con.prepareStatement(sql);
            ResultSet rs = DBConn.ps.executeQuery();
            while (rs.next()) {
                UserBean user = new UserBean();
                user.setId(rs.getInt("id"));
                user.setLoginname(rs.getString("username"));
                user.setLoginpassword(rs.getString("password"));
                user.setLoginphone(rs.getString("userphone"));
                users.add(user);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBConn.close();
        }
        return users;
    }
    // 通过登录名获取id
    public Integer getIdForName(UserBean user) {
        String sql = "SELECT * FROM userinfo WHERE username=?";
        Integer i = 0;
        try {
            DBConn.prepareConnection();
            DBConn.ps = DBConn.con.prepareStatement(sql);
            DBConn.ps.setString(1, user.getLoginname());
            ResultSet rs = DBConn.ps.executeQuery();
            while (rs.next()) {
                i = rs.getInt("id");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBConn.close();
        }
        return i;
    }
    // 检查用户登录
    public boolean userLogin(UserBean user) {
        String sql = "SELECT * FROM userinfo WHERE username=?";
        boolean flag = false;
        try {
            DBConn.prepareConnection();
            DBConn.ps = DBConn.con.prepareStatement(sql);
            DBConn.ps.setString(1, user.getLoginname());
            ResultSet rs = DBConn.ps.executeQuery();
            while (rs.next()) {
                if (rs.getString("password").equals(user.getLoginpassword()))
                    flag = true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBConn.close();
        }
        return flag;
    }
}

petAdd

package petservlets;
import cn.dao.DocManage;
import cn.dao.PetManage;
import cn.dao.UserBean;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
/**
 * @author shuijianshiqing
 * @date 2021/1/24 11:16
 */
public class petAdd  extends HttpServlet {
    public petAdd() {
        super();
    }
    public void destroy() {
        super.destroy();
    }
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        request.setCharacterEncoding("utf-8");
        UserBean user = new UserBean();
        user.setPetname(request.getParameter("petname"));
        user.setPetmaster(request.getParameter("petmaster"));
        user.setPetcity(request.getParameter("petcity"));
        user.setPetadress(request.getParameter("petadress"));
        user.setPetphone(request.getParameter("petphone"));
        PetManage manage = new PetManage();
        HttpSession session = request.getSession();
        if (manage.addPet(user) == 1) {
            session.setAttribute("update", "suc");
            request.getRequestDispatcher("../petsearch.jsp").forward(request, response);
        } else {
            session.setAttribute("update", "err");
            request.getRequestDispatcher("../petsearch.jsp").forward(request, response);
        }
        out.flush();
        out.close();
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }
    public void init() throws ServletException {
        // Put your code here
    }
}

petDel

package petservlets;
import cn.dao.PetManage;
import cn.dao.UserBean;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
/**
 * @author shuijianshiqing
 * @date 2021/1/24 11:31
 */
public class petDel  extends HttpServlet {
    public petDel() {
        super();
    }
    public void destroy() {
        super.destroy();
        // Put your code here
    }
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        request.setCharacterEncoding("utf-8");
        String mode = request.getParameter("m");
        Integer id = Integer.parseInt(request.getParameter("id"));
        HttpSession session = request.getSession();
        PetManage manage = new PetManage();
        if (mode.equals("del")) {
            if (manage.delPetById(id) == 1) {
                session.setAttribute("update", "del");
            } else {
                session.setAttribute("update", "delno");
            }
            request.getRequestDispatcher("../petsearch.jsp").forward(request,
                    response);
        } else if (mode.equals("update")) {
            UserBean user = new UserBean();
            user = manage.getPetForId(id);
            if (!user.getPetname().equals("")) {
                request.setAttribute("user", user);
                request.getRequestDispatcher("../petupdate.jsp").forward(request,
                        response);
            } else {
                session.setAttribute("error", "cuo");
                request.getRequestDispatcher("../petsearch.jsp").forward(request, response);
            }
        }
        out.flush();
        out.close();
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.flush();
        out.close();
    }
    public void init() throws ServletException {
        // Put your code here
    }
}

petQuery

package petservlets;
import cn.dao.PetManage;
import cn.dao.UserBean;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
/**
 * @author shuijianshiqing
 * @date 2021/1/24 11:31
 */
public class petQuery extends HttpServlet {
    public petQuery() {
        super();
    }
    public void destroy() {
        super.destroy();
        // Put your code here
    }
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request, response);
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        request.setCharacterEncoding("utf-8");
        String name = request.getParameter("petname");
        String master = request.getParameter("petmaster");
        UserBean user = new UserBean();
        PetManage manage = new PetManage();
        List<UserBean> list = new ArrayList<UserBean>();
        request.setAttribute("nopet", "flase");
        if (master.equals("")) {
            list = manage.getPetdocForName(name);
            request.setAttribute("list", list);
            // 无数据时返回提示
            if (list.size() == 0) {
                request.setAttribute("nopet", "nopet");
            }
            request.getRequestDispatcher("../petresult.jsp").forward(request, response);
        } else {
            user.setPetmaster(master);
            list = manage.getPetByMaster(user);
            request.setAttribute("list", list);
            if (list.size() == 0) {
                request.setAttribute("nopet", "nopet");
            }
            request.getRequestDispatcher("../petresult.jsp").forward(request, response);
        }
        out.flush();
        out.close();
    }
    public void init() throws ServletException {
        // Put your code here
    }
}

petUpdate

package petservlets;
import cn.dao.PetManage;
import cn.dao.UserBean;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
/**
 * @author shuijianshiqing
 * @date 2021/1/24 11:31
 */
public class petUpdate extends HttpServlet {
    public petUpdate() {
        super();
    }
    public void destroy() {
        super.destroy();
        // Put your code here
    }
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.flush();
        out.close();
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        request.setCharacterEncoding("utf-8");
        Integer id = Integer.parseInt(request.getParameter("id"));
        String petname = request.getParameter("petname");
        String petmaster = request.getParameter("petmaster");
        String petcity = request.getParameter("petcity");
        String petadress = request.getParameter("petadress");
        String petphone = request.getParameter("petphone");
        UserBean user = new UserBean();
        PetManage manage = new PetManage();
        HttpSession session = request.getSession();
        user.setId(id);
        user.setPetname(petname);
        user.setPetmaster(petmaster);
        user.setPetcity(petcity);
        user.setPetadress(petadress);
        user.setPetphone(petphone);
        if (manage.updatePetForId(user) == 1) {
            session.setAttribute("update", "ok");
        } else {
            session.setAttribute("error", "ng");
        }
        request.getRequestDispatcher("../petsearch.jsp").forward(request, response);
        out.flush();
        out.close();
    }
    public void init() throws ServletException {
        // Put your code here
    }
}


四、其他


1.其他系统实现


Java+JSP实现学生图书管理系统

Java+JSP实现学生信息管理系统

Java+JSP实现用户信息管理系统

Java+Servlet+JSP实现航空订票系统

Java+Servlet+JSP实现房屋租赁管理系统

Java+Servlet+JSP实现学生选课管理系统

Java+Servlet+JSP实现学生成绩管理系统

Java+Servlet+JSP实现宠物诊所管理系统

Java+SSM+Easyui实现网上考试系统

Java+SSM+Bootstrap实现Web学生信息管理系统

Java+Springboot+H-ui实现营销管理系统

Java+Springboot+Mybatis+Bootstrap实现网上商城系统

Java+Springboot+Mybatis+Bootstrap实现景区旅游管理系统


相关文章
|
4天前
|
前端开发 Java 测试技术
Java一分钟之Spring MVC:构建Web应用
【5月更文挑战第15天】Spring MVC是Spring框架的Web应用模块,基于MVC模式实现业务、数据和UI解耦。常见问题包括:配置DispatcherServlet、Controller映射错误、视图解析未设置、Model数据传递遗漏、异常处理未配置、依赖注入缺失和忽视单元测试。解决这些问题可提升代码质量和应用性能。注意配置`web.xml`、`@RequestMapping`、`ViewResolver`、`Model`、`@ExceptionHandler`、`@Autowired`,并编写测试用例。
51 3
|
4天前
|
Java
排课系统【JSP+Servlet+JavaBean】(Java课设)
排课系统【JSP+Servlet+JavaBean】(Java课设)
18 5
|
4天前
|
Java
仓库管理系统【JSP+Servlet+JavaBean】(Java课设)
仓库管理系统【JSP+Servlet+JavaBean】(Java课设)
15 1
|
1天前
|
监控 安全 NoSQL
采用java+springboot+vue.js+uniapp开发的一整套云MES系统源码 MES制造管理系统源码
MES系统是一套具备实时管理能力,建立一个全面的、集成的、稳定的制造物流质量控制体系;对生产线、工艺、人员、品质、效率等多方位的监控、分析、改进,满足精细化、透明化、自动化、实时化、数据化、一体化管理,实现企业柔性化制造管理。
11 3
|
1天前
|
移动开发 前端开发 JavaScript
Java和web前端,IT新人该如何选择?,2024年最新Web前端内存优化面试
Java和web前端,IT新人该如何选择?,2024年最新Web前端内存优化面试
|
2天前
|
Java 数据库连接 数据库
spring--为web(1),富士康java面试题整理
spring--为web(1),富士康java面试题整理
|
2天前
|
JavaScript Java 测试技术
《手把手教你》系列技巧篇(四十六)-java+ selenium自动化测试-web页面定位toast-下篇(详解教程)
【5月更文挑战第10天】本文介绍了使用Java和Selenium进行Web自动化测试的实践,以安居客网站为例。最后,提到了在浏览器开发者工具中调试和观察页面元素的方法。
12 2
|
2天前
|
Web App开发 JavaScript 测试技术
《手把手教你》系列技巧篇(四十五)-java+ selenium自动化测试-web页面定位toast-上篇(详解教程)
【5月更文挑战第9天】本文介绍了在Appium中处理App自动化测试中遇到的Toast元素定位的方法。Toast在Web UI测试中也常见,通常作为轻量级反馈短暂显示。文章提供了两种定位Toast元素的技巧.
10 0
|
4天前
|
Java 关系型数据库 MySQL
java+B/S架构医院绩效考核管理系统源码 医院绩效管理系统4大特点
医院绩效考核管理系统,采用多维度综合绩效考核的形式,针对院内实际情况分别对工作量、KPI指标、科研、教学、管理等进行全面考核。医院可结合实际需求,对考核方案中各维度进行灵活配置,对各维度的权重、衡量标准、数据统计方式进行自定义维护。
13 0
|
4天前
|
开发框架 缓存 Java
JSP 教程 之 Eclipse JSP/Servlet 环境搭建 1
本教程指导如何使用Eclipse搭建JSP/Servlet开发环境。需下载Eclipse J2EE和Tomcat,将Tomcat解压至无中文和空格的D盘目录。启动Tomcat后,服务器运行,通过浏览器访问`http://localhost:8080/`。在Tomcat的webapps\ROOT目录创建test.jsp文件,输入代码并访问`http://localhost:8080/test.jsp`以验证环境配置成功。
7 1