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实现景区旅游管理系统


相关文章
|
29天前
|
监控 Java API
如何使用Java语言快速开发一套智慧工地系统
使用Java开发智慧工地系统,采用Spring Cloud微服务架构和前后端分离设计,结合MySQL、MongoDB数据库及RESTful API,集成人脸识别、视频监控、设备与环境监测等功能模块,运用Spark/Flink处理大数据,ECharts/AntV G2实现数据可视化,确保系统安全与性能,采用敏捷开发模式,提供详尽文档与用户培训,支持云部署与容器化管理,快速构建高效、灵活的智慧工地解决方案。
|
20天前
|
设计模式 消息中间件 搜索推荐
Java 设计模式——观察者模式:从优衣库不使用新疆棉事件看系统的动态响应
【11月更文挑战第17天】观察者模式是一种行为设计模式,定义了一对多的依赖关系,使多个观察者对象能直接监听并响应某一主题对象的状态变化。本文介绍了观察者模式的基本概念、商业系统中的应用实例,如优衣库事件中各相关方的动态响应,以及模式的优势和实际系统设计中的应用建议,包括事件驱动架构和消息队列的使用。
|
1月前
|
运维 自然语言处理 供应链
Java云HIS医院管理系统源码 病案管理、医保业务、门诊、住院、电子病历编辑器
通过门诊的申请,或者直接住院登记,通过”护士工作站“分配患者,完成后,进入医生患者列表,医生对应开具”长期医嘱“和”临时医嘱“,并在电子病历中,记录病情。病人出院时,停止长期医嘱,开具出院医嘱。进入出院审核,审核医嘱与住院通过后,病人结清缴费,完成出院。
86 3
|
1月前
|
Java 数据库连接 数据库
深入探讨Java连接池技术如何通过复用数据库连接、减少连接建立和断开的开销,从而显著提升系统性能
在Java应用开发中,数据库操作常成为性能瓶颈。本文通过问题解答形式,深入探讨Java连接池技术如何通过复用数据库连接、减少连接建立和断开的开销,从而显著提升系统性能。文章介绍了连接池的优势、选择和使用方法,以及优化配置的技巧。
37 1
|
1月前
|
JavaScript Java 项目管理
Java毕设学习 基于SpringBoot + Vue 的医院管理系统 持续给大家寻找Java毕设学习项目(附源码)
基于SpringBoot + Vue的医院管理系统,涵盖医院、患者、挂号、药物、检查、病床、排班管理和数据分析等功能。开发工具为IDEA和HBuilder X,环境需配置jdk8、Node.js14、MySQL8。文末提供源码下载链接。
|
2月前
|
移动开发 前端开发 JavaScript
java家政系统成品源码的关键特点和技术应用
家政系统成品源码是已开发完成的家政服务管理软件,支持用户注册、登录、管理个人资料,家政人员信息管理,服务项目分类,订单与预约管理,支付集成,评价与反馈,地图定位等功能。适用于各种规模的家政服务公司,采用uniapp、SpringBoot、MySQL等技术栈,确保高效管理和优质用户体验。
|
2月前
|
XML JSON 监控
告别简陋:Java日志系统的最佳实践
【10月更文挑战第19天】 在Java开发中,`System.out.println()` 是最基本的输出方法,但它在实际项目中往往被认为是不专业和不足够的。本文将探讨为什么在现代Java应用中应该避免使用 `System.out.println()`,并介绍几种更先进的日志解决方案。
61 1
|
2月前
|
Java 关系型数据库 API
介绍一款Java开发的企业接口管理系统和开放平台
YesApi接口管理平台Java版,基于Spring Boot、Vue.js等技术,提供API接口的快速研发、管理、开放及收费等功能,支持多数据库、Docker部署,适用于企业级PaaS和SaaS平台的二次开发与搭建。
|
1月前
|
SQL XML 缓存
java中jsp详解!!!
JSP(Java Server Pages)是一种动态网页技术标准,允许在HTML页面中嵌入Java代码,实现网页逻辑与设计分离。JSP本质上是Servlet的简化,支持跨平台运行。JSP通过内置对象(如request、response、session等)和指令(如page、include、taglib)提供强大的功能,同时利用EL表达式和JSTL标签库简化页面开发。JSP的核心优势在于快速开发和维护Web应用。
44 0
|
2月前
|
前端开发 Java 数据库连接
基于Java的校车管理系统(下)
基于Java的校车管理系统(下)
25 0
下一篇
DataWorks