项目名称、背景、功能
项目名称
“企业管理系统定制开发爱心宠物诊所”系统
开发背景
“爱心”企业管理系统定制开发宠物诊所的职员在工作企业管理系统定制开发中需要查阅和管理如下信息:企业管理系统定制开发诊所的兽医、企业管理系统定制开发客户以及客户的宠物。企业管理系统定制开发诊所的兽医具有不同的专业特长,例如:企业管理系统定制开发有的擅长牙科,有的擅长内科等。诊所的职员使用浏览器访问该系统。客户的每个宠物都具有唯一的名称。
功能
“爱心”宠物诊所的职员需要使用系统提供的如下功能:
浏览诊所的兽医以及他们的专业特长;
浏览宠物的主人(即诊所的客户)的相关信息;
更新宠物的主人的相关信息;
向系统中增加一个新客户;
浏览宠物的相关信息;
更新宠物的相关信息;
向系统中增加一个新宠物;
浏览宠物的访问历史记录;
向宠物的访问历史记录添加一次访问;
此外,诊所的职员在使用系统提供的上述功能之前需要进行登录。当职员不需要使用系统的上述功能时,也可退出系统。
系统概述
系统思维导图
设计模式
MVC框架
JSP实现视图层设计,实现控制层设计,JavaBean实现模型层设计
M(Model)指数据模型层,V(View)指视图层,C(Controller)指控制层
使用 MVC 的目的是将 M 和 V 的实现代码分离,使同一个程序可以有不同的表现形式
用户环境
编译器:IntelliJ IDEA;
数据库:MySQL ;
数据库可视化软件:SQLyog;
服务器:Tomcat
选用技术
JSP、 JavaBean、 Servlet、 EL表达式、 Bootstrap框架
系统实现
数据库设计
数据库名称:pets
employee:职员表
用户 username varchar (20) ;密码 password varchar(100) ;加密盐 salt char(4)
字段 | 类型 | 约束 | 注释 |
---|---|---|---|
Username | Varchar(20) | Primary key | 用户名 |
Password | Varchar(100) | Not Null | 密码 |
Salt | Char(4) | Not Null | 加密盐 |
SQLyog可视化表
owner:主人信息表
主人编号id int(20);主人姓名name varchar(20);地址 address varchar(255);城市 city varchar(20);电话 tel varchar(20)
字段 | 类型 | 约束 | 注释 |
---|---|---|---|
Id | int(20) | Primary key | 主人编号 |
Name | Varchar(20) | Not null | 姓名 |
Address | Varchar(255) | Not null | 地址 |
City | Varchar(20) | Not null | 城市 |
Tel | Varchar(20) | Not null | 电话 |
SQLyog可视化表
vets:兽医信息表
自增id id int(20);兽医名字 name varchar(20);兽医专业 special varchar(20);兽医电话 tel int(20)
字段 | 类型 | 约束 | 注释 |
---|---|---|---|
Id | int(20) | Primary key | 自增id |
Name | Varchar(20) | Not null | 兽医名字 |
Special | Varchar(20) | Not null | 兽医专业 |
Tel | Int(20) | Not null | 兽医电话 |
SQLyog可视化表
pets:宠物表
宠物编号 id int(20); 名称 name varchar(20);出生日期 date;类型 type varchar(20);主人id ownerId varchar(20);主人名字 ownerName varchar(20)
字段 | 类型 | 约束 | 注释 |
---|---|---|---|
Id | int(20) | Primary key | 宠物编号 |
Name | Varchar(20) | Not null | 名称 |
Date | 无 | Not null | 出生日期 |
Type | Varchar(20) | Not null | 类型 |
OwnerId | Varchar(20) | Not null | 主人id |
OwnerName | Varchar(20) | Not null | 主人名字 |
SQLyog可视化表
visits:访问表
编号 id int(20); 宠物id petId int(20); 宠物名字 petName varchar(20);访问日期 date ;描述 description varchar(255)
字段 | 类型 | 约束 | 注释 |
---|---|---|---|
Id | int(20) | Primary key | 编号 |
petId | int(20) | Not null | 宠物id |
PetName | Varchar(20) | Not null | 宠物名字 |
Date | 无 | Not null | 时间 |
Description | Varchar(255) | Not null | 描述 |
SQLyog可视化表
系统servlet和JSP概览
系统导航栏
代码 运行效果
导航栏 内联代码片
<body> <%--导航栏--%> <nav class="navbar navbar-default"> <div class="container"> <!-- Brand and toggle get grouped for better mobile display --> <div class="navbar-header active"> <a class="navbar-brand active" href="${basePath}/home">首页</a> </div> <!-- Collect the nav links, forms, and other content for toggling --> <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1"> <ul class="nav navbar-nav"> <li class=""><a href="${basePath}/vets/list"> 兽医信息 </a></li> </ul> <ul class="nav navbar-nav"> <li class=""><a href="${basePath}/pets/list"> 宠物信息 </a></li> </ul> <ul class="nav navbar-nav"> <li class=""><a href=""> 今日公告 </a></li> </ul> <ul class="nav navbar-nav"> <li class=""><a href=""> 帮助 </a></li> </ul> <ul class="nav navbar-nav navbar-right"> <li><a href="${basePath}/loginOut"> 职员:<%=emp.getUsername()%> - 退出 </a></li> </ul> <ul class="nav navbar-nav navbar-right"> <li class=""><a href=""> 联系我们 </a></li> </ul> </div><!-- /.navbar-collapse --> </div><!-- /.container-fluid --> </nav> </body>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
兽医信息页面
兽医信息前端页面代码 运行效果
前端页面代码
兽医信息前端页面 内联代码片
<body><jsp:include page="../common/top.jsp"></jsp:include><div class="container"> <h3 style="text-align: center; margin-bottom: 20px">兽医信息列表</h3> <div style="margin-bottom: 30px"> <form class="form-inline left" action="${basePath}/vets/list" method="get"> <div class="form-group"> <label for="searchName">姓名:</label> <input type="text" class="form-control" id="searchName" placeholder="兽医姓名" name="name"> </div> <div class="form-group"> <label for="searchNativePlace">专业:</label> <input type="text" class="form-control" id="searchNativePlace" placeholder="兽医专业" name="specialties"> </div> <button type="submit" class="btn btn-default">搜索</button> <a href="${basePath}/vet/add" class="btn btn-info" style="float: right; margin-right: 10px">添加兽医</a> </form> </div> <table class="table table-bordered"> <thead> <tr> <th>ID</th> <th>姓名</th> <th>专业</th> <th>电话</th> <th>操作</th> </tr> </thead> <tbody> <c:forEach items="${page.datas}" var="vet" > <tr> <td>${vet.id}</td> <td>${vet.name}</td> <td>${vet.specialties}</td> <td>${vet.tel}</td> <td> <a class="btn btn-default btn-sm " role="button" href="${basePath}/vet/update?id=${vet.id}">修改</a> <a class="btn btn-default btn-sm " role="button" href="${basePath}/vet/del?id=${vet.id}">删除</a> </td> </tr> </c:forEach> </tbody> </table> <!--分页--> <c:set var="searchPath" value="${basePath}/vets/list?name=${param.name}&specialties=${param.specialties}"/> <%@include file="/common/page.jsp"%></div></body>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
后端业务逻辑处理代码
后端业务逻辑处理 内联代码片
package petClinic.service;import petClinic.model.SearchData;import petClinic.model.Vet;import petClinic.tool.CommTool;import petClinic.tool.Page;import petClinic.utils.JDBC;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.util.ArrayList;import java.util.List;/** * 兽医对应的service * made by chigua */public class VetService { private static VetService VETSERVICE; /** * 获取单例 */ public static synchronized VetService instance() { if (VETSERVICE == null) { VETSERVICE = new VetService(); } return VETSERVICE; } /** * 搜寻查询的兽医数据 * @param searchData * @return */ public List<Vet> findAllofThis(SearchData searchData) { String sql = "select * from `vets` where 1=1 "; //判断是否为空 if (!CommTool.isEmpty(searchData.getName())) { sql += "and name like ? "; } if (!CommTool.isEmpty(searchData.getSpecialties())) { sql += "and specialties=? "; } Connection conn = null; PreparedStatement st = null; ResultSet rs = null; List<Vet> vets = new ArrayList<Vet>(); try { conn = JDBC.getConnection("pets"); st = conn.prepareStatement(sql); int index = 0; if (!CommTool.isEmpty(searchData.getName())) { sql += "and name like ? "; index++; st.setString(index, "%" + searchData.getName() + "%"); } if (!CommTool.isEmpty(searchData.getSpecialties())) { sql += "and specialties=? "; index++; st.setString(index, searchData.getSpecialties()); } rs = st.executeQuery(); while (rs.next()) { int id = rs.getInt(1); String name = rs.getString(2); String specialties = rs.getString(3); int tel = rs.getInt(4); Vet vet = new Vet(id, name, specialties, tel); vets.add(vet); } } catch (SQLException throwables) { throwables.printStackTrace(); } finally { JDBC.release(conn, st, rs); } return vets; } /** * 查找页面的数据 * * @param page * @param searchData * @return */ public Page<Vet> pageSearch(Page<Vet> page, SearchData searchData) { String sql = "select * from `vets` where 1=1 "; //判断是否为空 if (!CommTool.isEmpty(searchData.getName())) { sql += "and name like ? "; } if (!CommTool.isEmpty(searchData.getSpecialties())) { sql += "and specialties=? "; } sql += "limit ?,? "; Connection conn = null; PreparedStatement st = null; ResultSet rs = null; List<Vet> vets = new ArrayList<Vet>(); try { conn = JDBC.getConnection("pets"); st = conn.prepareStatement(sql); int index = 0; if (!CommTool.isEmpty(searchData.getName())) { sql += "and name like ? "; index++; st.setString(index, "%" + searchData.getName() + "%"); } if (!CommTool.isEmpty(searchData.getSpecialties())) { sql += "and specialties=? "; index++; st.setString(index, searchData.getSpecialties()); } index++; st.setInt(index, page.getOffset()); index++; st.setInt(index, page.getLimit()); rs = st.executeQuery(); while (rs.next()) { int id = rs.getInt(1); String name = rs.getString(2); String specialties = rs.getString(3); int tel = rs.getInt(4); Vet vet = new Vet(id, name, specialties, tel); vets.add(vet); } } catch (SQLException throwables) { throwables.printStackTrace(); } finally { JDBC.release(conn, st, rs); } page.setDatas(vets); return page; } /** * 添加兽医信息 * * @param vet * @return */ public int add(Vet vet) { Connection conn = null; PreparedStatement st = null; int i = 0; try { conn = JDBC.getConnection("pets"); String sql = "insert into `vets` values (null,?,?,?)"; st = conn.prepareStatement(sql); st.setString(1, vet.getName()); st.setString(2, vet.getSpecialties()); st.setInt(3, vet.getTel()); i = st.executeUpdate(); } catch (SQLException throwables) { throwables.printStackTrace(); } finally { JDBC.release(conn, st, null); } return i; } /** * 通过id获取兽医信息 * * @param id * @return */ public Vet getById(int id) { Connection conn = null; PreparedStatement st = null; ResultSet rs = null; Vet vet = null; try { conn = JDBC.getConnection("pets"); String sql = "select * from `vets` where `id`=?"; st = conn.prepareStatement(sql); st.setInt(1, id); rs = st.executeQuery(); while (rs.next()) { String name = rs.getString(2); String specialties = rs.getString(3); String tel = rs.getString(4); vet = new Vet(id, name, specialties, Integer.parseInt(tel)); } } catch (SQLException throwables) { throwables.printStackTrace(); } finally { JDBC.release(conn, st, rs); } return vet; } /** * 修改兽医信息 * * @param vet * @return */ public int update(Vet vet) { Connection conn = null; PreparedStatement st = null; int i = 0; try { conn = JDBC.getConnection("pets"); String sql = "update `vets` set `name`=?, `specialties`=?, `tel`=? where id=?"; st = conn.prepareStatement(sql); st.setString(1, vet.getName()); st.setString(2, vet.getSpecialties()); st.setInt(3, vet.getTel()); st.setInt(4, vet.getId()); i = st.executeUpdate(); if (i > 0) { System.out.println("编号" + vet.getId() + "兽医信息修改成功!"); i = 1; } } catch (SQLException throwables) { throwables.printStackTrace(); } finally { JDBC.release(conn, st, null); } return i; } /** * 兽医信息删除 * * @param id */ public void del(int id) { Connection conn = null; PreparedStatement st = null; try { conn = JDBC.getConnection("pets"); String sql = "delete from `vets` where `id`=?"; st = conn.prepareStatement(sql); st.setInt(1, id); int i = st.executeUpdate(); if (i > 0) System.out.println("编号为" + id + "的兽医已删除"); } catch (SQLException throwables) { throwables.printStackTrace(); } finally { JDBC.release(conn, st, null); } }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194
- 195
- 196
- 197
- 198
- 199
- 200
- 201
- 202
- 203
- 204
- 205
- 206
- 207
- 208
- 209
- 210
- 211
- 212
- 213
- 214
- 215
- 216
- 217
- 218
- 219
- 220
- 221
- 222
- 223
- 224
- 225
- 226
- 227
- 228
- 229
- 230
- 231
- 232
- 233
- 234
- 235
- 236
- 237
- 238
- 239
- 240
- 241
- 242
- 243
- 244
- 245
- 246
- 247
- 248
- 249
- 250
- 251
- 252
- 253
- 254
- 255
- 256
- 257
- 258
- 259
- 260
- 261
- 262
- 263
- 264
- 265
- 266
宠物信息页面
代码 运行效果
前端页面代码
宠物信息 内联代码片
<body><jsp:include page="../common/top.jsp"></jsp:include><div class="container"> <h3 style="text-align: center; margin-bottom: 20px">宠物信息列表</h3> <div style="margin-bottom: 30px"> <form class="form-inline left" action="${basePath}/pets/list" method="get"> <div class="form-group"> <label for="searchName">宠物名称:</label> <input type="text" class="form-control" id="searchName" placeholder="宠物名称" name="name"> </div> <div class="form-group"> <label for="searchType">宠物类型:</label> <%--<input type="text" class="form-control" id="searchType" placeholder="宠物类型" name="type">--%> <select class="form-control" name="type" id="searchType"> <option value="" selected>请选择宠物类型</option> <option value="秋田">秋田</option> <option value="沙皮">沙皮</option> <option value="赤影">赤影</option> <option value="西施">西施</option> <option value="柴犬">柴犬</option> <option value="巴哥">巴哥</option> <option value="印度犬">印度犬</option> <option value="京巴">京巴</option> <option value="哈士奇">哈士奇</option> </select> </div> <div class="form-group"> <label for="searchOwnerName">宠物主人:</label> <input type="text" class="form-control" id="searchOwnerName" placeholder="宠物主人" name="ownerName"> </div> <button type="submit" class="btn btn-default">搜索</button> <a href="${basePath}/pet/add" class="btn btn-info" style="float: right; margin-right: 10px">宠物信息添加</a> <a href="${basePath}/owner/add" class="btn btn-info" style="float: right; margin-right: 10px">添加主人信息</a> </form> </div> <table class="table table-bordered" style="text-align: center"> <thead> <tr> <th style="text-align: center">ID</th> <th style="text-align: center">宠物名称</th> <th style="text-align: center">宠物春日期</th> <th style="text-align: center">宠物类型</th> <th style="text-align: center">宠物主人</th> <th style="text-align: center">宠物、宠物主人相关操作</th> </tr> </thead> <tbody> <c:forEach items="${page.datas}" var="pet" > <tr> <td>${pet.id}</td> <td>${pet.name}</td> <td>${pet.date}</td> <td>${pet.type}</td> <td>${pet.ownerName}</td> <td> <a class="btn btn-default btn-sm " role="button" href="${basePath}/pet/update?id=${pet.id}">修改</a> <a class="btn btn-default btn-sm " role="button" href="${basePath}/pet/del?id=${pet.id}">删除</a> <a class="btn btn-default btn-sm " role="button" href="${basePath}/owner/display?id=${pet.ownerId}">查看主人信息</a> <a class="btn btn-default btn-sm " role="button" href="${basePath}/visit/display?id=${pet.id}">查看宠物历史信息</a> <a class="btn btn-default btn-sm " role="button" href="${basePath}/visit/add?id=${pet.id}&name=${pet.name}">添加宠物浏览信息</a> </td> </tr> </c:forEach> </tbody> </table> <!--分页--> <c:set var="searchPath" value="${basePath}/pets/list?name=${param.name}&type=${param.type}&${param.ownerName}"/> <%@include file="/common/page.jsp"%> <div> <hr> <c:if test="${owner!=null}"> <h4 align="center">宠物主人:${owner.name}</h4> <h4 align="center">地址:${owner.address}</h4> <h4 align="center">城市:${owner.city}</h4> <h4 align="center">电话:${owner.tel}</h4> <hr> </c:if> <c:if test="${visits!=null}"> <c:forEach items="${visits}" var="visit"> <h4>记录日期:${visit.date}</h4> <h4>宠物主人:${visit.petName}</h4> <h4>记录说明:${visit.description}</h4> <hr> </c:forEach> </c:if> </div></div></body>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
后端业务逻辑处理代码
宠物信息后端业务逻辑处理 内联代码片
package petClinic.service;import petClinic.model.Owner;import petClinic.model.Pet;import petClinic.model.PetsSearchData;import petClinic.tool.CommTool;import petClinic.tool.Page;import petClinic.utils.JDBC;import java.sql.*;import java.util.ArrayList;import java.util.List;/** * 宠物对应的service */public class PetService { private static PetService PETSERVICE; /** * 获取单例 */ public static synchronized PetService instance() { if (PETSERVICE == null) { PETSERVICE = new PetService(); } return PETSERVICE; } /** * 搜寻查询的宠物信息 * * @param petsSearchData * @return */ public List<Pet> findAllofThis(PetsSearchData petsSearchData) { String sql = "select * from `pets` where 1=1 "; //判断是否为空 if (!CommTool.isEmpty(petsSearchData.getName())) { sql += "and name like ? "; } if (!CommTool.isEmpty(petsSearchData.getType())) { sql += "and type = ? "; } if (!CommTool.isEmpty(petsSearchData.getOwnerName())) { sql += "and ownerName like ? "; } Connection conn = null; PreparedStatement st = null; ResultSet rs = null; List<Pet> pets = new ArrayList<Pet>(); try { conn = JDBC.getConnection("pets"); st = conn.prepareStatement(sql); int index = 0; if (!CommTool.isEmpty(petsSearchData.getName())) { //sql += "and name like ? "; index++; st.setString(index, "%" + petsSearchData.getName() + "%"); } if (!CommTool.isEmpty(petsSearchData.getType())) { //sql += "and type = ? "; index++; st.setString(index, petsSearchData.getType()); } if (!CommTool.isEmpty(petsSearchData.getOwnerName())) { //sql += "and owner like ? "; index++; st.setString(index, "%" + petsSearchData.getOwnerName() + "%"); } rs = st.executeQuery(); while (rs.next()) { int id = rs.getInt(1); String name = rs.getString(2); String date = rs.getString(3); String type = rs.getString(4); String ownerId = rs.getString(5); String ownerName = rs.getString(6); Pet pet = new Pet(id, name, date, type, ownerId, ownerName); pets.add(pet); } } catch (SQLException throwables) { throwables.printStackTrace(); } finally { JDBC.release(conn, st, rs); } return pets; } /** * 查询页面的数据 * * @param page * @param petsSearchData * @return */ public Page<Pet> pageSearch(Page<Pet> page, PetsSearchData petsSearchData) { String sql = "select * from `pets` where 1=1 "; //判断是否为空 if (!CommTool.isEmpty(petsSearchData.getName())) { sql += "and name like ? "; } if (!CommTool.isEmpty(petsSearchData.getType())) { sql += "and type=? "; } if (!CommTool.isEmpty(petsSearchData.getOwnerName())) { sql += "and ownerName like ? "; } sql += "limit ?,?"; Connection conn = null; PreparedStatement st = null; ResultSet rs = null; List<Pet> pets = new ArrayList<Pet>(); try { conn = JDBC.getConnection("pets"); st = conn.prepareStatement(sql); int index = 0; if (!CommTool.isEmpty(petsSearchData.getName())) { sql += "and name like ? "; index++; st.setString(index, "%" + petsSearchData.getName() + "%"); } if (!CommTool.isEmpty(petsSearchData.getType())) { sql += "and type=? "; index++; st.setString(index, petsSearchData.getType()); } if (!CommTool.isEmpty(petsSearchData.getOwnerName())) { sql += "and ownerName like ? "; index++; st.setString(index, "%" + petsSearchData.getOwnerName() + "%"); } index++; st.setInt(index, page.getOffset()); index++; st.setInt(index, page.getLimit()); rs = st.executeQuery(); while (rs.next()) { int id = rs.getInt(1); String name = rs.getString(2); String date = rs.getString(3); String type = rs.getString(4); String ownerId = rs.getString(5); String ownerName = rs.getString(6); Pet pet = new Pet(id, name, date, type, ownerId,ownerName); pets.add(pet); } } catch (SQLException throwables) { throwables.printStackTrace(); } finally { JDBC.release(conn, st, rs); } page.setDatas(pets); return page; } /** * 查询所有主人信息 * * @return */ public List<Owner> findAllOwners() { List<Owner> owners = new ArrayList<Owner>(); Connection conn = null; PreparedStatement st = null; ResultSet rs = null; try { conn = JDBC.getConnection("pets"); String sql = "select * from owners"; st = conn.prepareStatement(sql); rs = st.executeQuery(); while (rs.next()) { int id = rs.getInt(1); String name = rs.getString(2); String address = rs.getString(3); String city = rs.getString(4); String tel = rs.getString(5); Owner owner = new Owner(id, name, address, city, tel); owners.add(owner); } } catch (SQLException throwables) { throwables.printStackTrace(); } finally { JDBC.release(conn, st, rs); } return owners; } /** * 宠物信息添加 * @param pet * @return */ public int add(Pet pet) { Connection conn = null; PreparedStatement st = null; int i = 0; try { conn = JDBC.getConnection("pets"); String sql = "insert into `pets` values (null,?,?,?,?,?)"; st = conn.prepareStatement(sql); st.setString(1, pet.getName()); st.setString(2, pet.getDate()); st.setString(3, pet.getType()); st.setString(4, pet.getOwnerId()); st.setString(5, pet.getOwnerName()); i = st.executeUpdate(); } catch (SQLException throwables) { throwables.printStackTrace(); } finally { JDBC.release(conn, st, null); } return i; } /** * 通过主人名字查询是否存在 * @param ownerName * @return */ public Boolean findOwnerByName(String ownerName) { Connection conn = null; PreparedStatement st = null; ResultSet rs = null; Boolean exit = false; try { conn = JDBC.getConnection("pets"); String sql = "select * from owners where name = ?"; st = conn.prepareStatement(sql); st.setString(1,ownerName); rs = st.executeQuery(); exit = rs.next(); } catch (SQLException throwables) { throwables.printStackTrace(); }finally { JDBC.release(conn,st,rs); } return exit; } /** * 通过id获取宠物信息 * @param id * @return */ public Pet getById(int id) { Connection conn = null; PreparedStatement st = null; ResultSet rs = null; Pet pet = null; try { conn = JDBC.getConnection("pets"); String sql = "select * from `pets` where `id`=?"; st = conn.prepareStatement(sql); st.setInt(1, id); rs = st.executeQuery(); while (rs.next()) { String name = rs.getString(2); String date = rs.getString(3); String type = rs.getString(4); String ownerId = rs.getString(5); String ownerName = rs.getString(6); pet = new Pet(id,name,date,type,ownerId,ownerName); } } catch (SQLException throwables) { throwables.printStackTrace(); } finally { JDBC.release(conn, st, rs); } return pet; } /** * 宠物信息修改 * @param pet * @return */ public int update(Pet pet) { Connection conn = null; PreparedStatement st = null; int i = 0; try { conn = JDBC.getConnection("pets"); String sql = "update pets set name=?,date=?,type=?,ownerId=?,ownerName=? where id=?"; st = conn.prepareStatement(sql); st.setString(1, pet.getName()); st.setString(2, pet.getDate()); st.setString(3, pet.getType()); st.setString(4, pet.getOwnerId()); st.setString(5, pet.getOwnerName()); st.setInt(6,pet.getId()); i = st.executeUpdate(); } catch (SQLException throwables) { throwables.printStackTrace(); } finally { JDBC.release(conn, st, null); } return i; } /** * 删除宠物信息 * @param id */ public void del(int id) { Connection conn = null; PreparedStatement st = null; try { conn = JDBC.getConnection("pets"); String sql = "delete from `pets` where `id`=?"; st = conn.prepareStatement(sql); st.setInt(1, id); int i = st.executeUpdate(); if (i > 0) System.out.println("编号为" + id + "的宠物已删除"); } catch (SQLException throwables) { throwables.printStackTrace(); } finally { JDBC.release(conn, st, null); } }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194
- 195
- 196
- 197
- 198
- 199
- 200
- 201
- 202
- 203
- 204
- 205
- 206
- 207
- 208
- 209
- 210
- 211
- 212
- 213
- 214
- 215
- 216
- 217
- 218
- 219
- 220
- 221
- 222
- 223
- 224
- 225
- 226
- 227
- 228
- 229
- 230
- 231
- 232
- 233
- 234
- 235
- 236
- 237
- 238
- 239
- 240
- 241
- 242
- 243
- 244
- 245
- 246
- 247
- 248
- 249
- 250
- 251
- 252
- 253
- 254
- 255
- 256
- 257
- 258
- 259
- 260
- 261
- 262
- 263
- 264
- 265
- 266
- 267
- 268
- 269
- 270
- 271
- 272
- 273
- 274
- 275
- 276
- 277
- 278
- 279
- 280
- 281
- 282
- 283
- 284
- 285
- 286
- 287
- 288
- 289
- 290
- 291
- 292
- 293
- 294
- 295
- 296
- 297
- 298
- 299
- 300
- 301
- 302
- 303
- 304
- 305
- 306
- 307
- 308
- 309
- 310
- 311
- 312
- 313
- 314
- 315
- 316
- 317
- 318
- 319
- 320
- 321
- 322
- 323
- 324
- 325
- 326
- 327
- 328
- 329
- 330
- 331
- 332
- 333
- 334
- 335
- 336
- 337
- 338
- 339
- 340
- 341
数据库连接JDBC
连接代码片
。
package petClinic.utils;import java.sql.*;public class JDBC { private static String url = null; private static String username = null; private static String password = null; //获取连接,传入数据库名称参数 public static Connection getConnection(String databaseName) throws SQLException { try { Class.forName("com.mysql.cj.jdbc.Driver");//固定写法,加载驱动 url = "jdbc:mysql://localhost:3306/"+databaseName +//传入要连接的数据库参数 "?characterEncoding=UTF-8&useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode" + "=false&serverTimezone=UTC"; username = "root";//用户名 password = "123456";//用户密码 } catch (Exception e) { e.printStackTrace(); } //System.out.println("链接成功!"); return DriverManager.getConnection(url,username,password); } //传入数据库名称和要执行的sql语句,返回一个PreparedStatement public static PreparedStatement execute(String databaseName,String sql){ Connection conn = null; PreparedStatement st = null; ResultSet rs = null; try { conn = getConnection(databaseName); st = conn.prepareStatement(sql); return st; } catch (SQLException throwables) { throwables.printStackTrace(); } return null; } //释放连接资源 public static void release(Connection conn, Statement st, ResultSet rs){ if(rs!=null){ try { rs.close(); } catch (SQLException throwables) { throwables.printStackTrace(); } } if(st!=null){ try { st.close(); } catch (SQLException throwables) { throwables.printStackTrace(); } } if(conn!=null){ try { conn.close(); } catch (SQLException throwables) { throwables.printStackTrace(); } } //System.out.println("资源已释放!"); }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
完结,欢迎指正