“爱心宠物诊所”系统
“爱心”宠物诊所的职员在工作中需要查阅和管理如下信息:诊所的兽医、客户以及客户的宠物。诊所的兽医具有不同的专业特长,例如:有的擅长牙科,有的擅长内科等。诊所的职员使用浏览器访问该系统。客户的每个宠物都具有唯一的名称。
“爱心”宠物诊所的职员需要使用系统提供的如下功能:
浏览诊所的兽医以及他们的专业特长;
浏览宠物的主人(即诊所的客户)的相关信息;
更新宠物的主人的相关信息;
向系统中增加一个新客户;
浏览宠物的相关信息;
更新宠物的相关信息;
向系统中增加一个新宠物;
浏览宠物的访问历史记录;
向宠物的访问历史记录添加一次访问;
此外,诊所的职员在使用系统提供的上述功能之前需要进行登录。当职员不需要使用系统的上述功能时,也可退出系统。
MVC框架
JSP实现视图层设计,Servlet实现控制层设计,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 | 加密盐 |
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 | 电话 |
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 | 兽医电话 |
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 | 主人名字 |
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 | 描述 |
代码 运行效果
导航栏 内联代码片
<body>
<%--导航栏--%>
<nav class="navbar navbar-default">
<div class="container">
<div class="navbar-header active">
<a class="navbar-brand active" href="${basePath}/home">首页a>
div>
<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>
div>
nav>
body>
兽医信息前端页面 内联代码片
<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>IDth>
<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>
后端业务逻辑处理 内联代码片
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);
}
}
}
宠物信息 内联代码片
<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">IDth>
<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>
宠物信息后端业务逻辑处理 内联代码片
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);
}
}
}
连接代码片
。
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("资源已释放!");
}
}
代码已上传Gitee仓库,需要源码可自行下载
完结,欢迎指正