实体类User(管理员)
package com.hnjd.entity;
public class UserInfo {
private int uid;
private String uname;
private String upsw;
private String urealname;
@Override
public String toString() {
return "UserInfo{" +
"uid=" + uid +
", uname='" + uname + '\'' +
", upsw='" + upsw + '\'' +
", urealname='" + urealname + '\'' +
'}';
}
public int getUid() {
return uid;
}
public void setUid(int uid) {
this.uid = uid;
}
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public String getUpsw() {
return upsw;
}
public void setUpsw(String upsw) {
this.upsw = upsw;
}
public String getUrealname() {
return urealname;
}
public void setUrealname(String urealname) {
this.urealname = urealname;
}
}
实体类Car(车辆)
package com.hnjd.entity;
public class CarInfo {
private int cid;
private String cname;
private String cpl;
private String crs;
private String cprice;
private String cgs;
private int czt;
private int uid;
private int sid;
@Override
public String toString() {
return "CarInfo{" +
"cid=" + cid +
", cname='" + cname + '\'' +
", cpl='" + cpl + '\'' +
", crs='" + crs + '\'' +
", cprice='" + cprice + '\'' +
", cgs='" + cgs + '\'' +
", czt=" + czt +
", uid=" + uid +
", sid=" + sid +
'}';
}
public int getCid() {
return cid;
}
public void setCid(int cid) {
this.cid = cid;
}
public String getCname() {
return cname;
}
public void setCname(String cname) {
this.cname = cname;
}
public String getCpl() {
return cpl;
}
public void setCpl(String cpl) {
this.cpl = cpl;
}
public String getCrs() {
return crs;
}
public void setCrs(String crs) {
this.crs = crs;
}
public String getCprice() {
return cprice;
}
public void setCprice(String cprice) {
this.cprice = cprice;
}
public String getCgs() {
return cgs;
}
public void setCgs(String cgs) {
this.cgs = cgs;
}
public int getCzt() {
return czt;
}
public void setCzt(int czt) {
this.czt = czt;
}
public int getUid() {
return uid;
}
public void setUid(int uid) {
this.uid = uid;
}
public int getSid() {
return sid;
}
public void setSid(int sid) {
this.sid = sid;
}
}
实体类Cus(顾客)
package com.hnjd.entity;
public class CusInfo {
private int sid;
private String sname;
private String scard;
@Override
public String toString() {
return "CusInfo{" +
"sid=" + sid +
", sname='" + sname + '\'' +
", scard='" + scard + '\'' +
'}';
}
public int getSid() {
return sid;
}
public void setSid(int sid) {
this.sid = sid;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public String getScard() {
return scard;
}
public void setScard(String scard) {
this.scard = scard;
}
}
dao层下的接口
package com.hnjd.dao;
import com.hnjd.entity.CarInfo;
import com.hnjd.entity.CusInfo;
import com.hnjd.entity.UserInfo;
import java.util.List;
public interface Dao {
//查询用户信息(登录)
public UserInfo selectUser(String userName,String userPwd);
//查询车辆信息(查询多条用List)
public List<CarInfo> selectCar();
//查询顾客信息(查询多条用List)
public List<CusInfo> selectCus();
//根据车辆Id修改车辆信息(办理出租)
public int updateById(int carId);
//根据车辆ID查询车辆信息
public CarInfo selectById1(int id);
//根据车辆ID查询顾客信息
public CusInfo selectById2(int id);
}
dao层下实现接口的方法类
package com.hnjd.dao;
import com.hnjd.entity.CarInfo;
import com.hnjd.entity.CusInfo;
import com.hnjd.entity.UserInfo;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class DaoImp implements Dao {
private static String driver = "com.mysql.jdbc.Driver";
private static String url = "jdbc:mysql://127.0.0.1:3306/myud?useSSL=false&serverTimezone=Asia/Shanghai";
@Override
public UserInfo selectUser(String userName,String userPwd) {//查询用户信息 登录
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
UserInfo ui = null;
try {
Class.forName(driver);
con = DriverManager.getConnection(url,"root","root");
String sql = "select * from userinfo where uname=? and upsw=?";
ps = con.prepareStatement(sql);
ps.setString(1,userName);
ps.setString(2,userPwd);
rs = ps.executeQuery();
while(rs.next()){
int uid = rs.getInt("uid");
String uname = rs.getString("uname");
String upsw = rs.getString("upsw");
String urealname = rs.getString("urealname");
ui = new UserInfo();
ui.setUid(uid);
ui.setUname(uname);
ui.setUpsw(upsw);
ui.setUrealname(urealname);
}
} catch (Exception e) {
e.printStackTrace();
}finally{
if(con != null){
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(ps != null){
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(rs != null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
return ui;
}
/**
*
* @return list
*/
@Override
public List<CarInfo> selectCar() {//查询车辆信息
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
CarInfo ci = null;
List<CarInfo> list = new ArrayList<CarInfo>();
try {
Class.forName(driver);
con = DriverManager.getConnection(url,"root","root");
String sql = "select * from carinfo";
ps = con.prepareStatement(sql);
rs = ps.executeQuery();
while(rs.next()){
int cid = rs.getInt("cid");
String cname = rs.getString("cname");
String cpl = rs.getString("cpl");
String crs = rs.getString("crs");
String cprice = rs.getString("cprice");
String cgs = rs.getString("cgs");
int czt = rs.getInt("czt");
int uid = rs.getInt("uid");
int sid = rs.getInt("sid");
ci = new CarInfo();
ci.setCid(cid);
ci.setCname(cname);
ci.setCpl(cpl);
ci.setCrs(crs);
ci.setCprice(cprice);
ci.setCgs(cgs);
ci.setCzt(czt);
ci.setUid(uid);
ci.setSid(sid);
list.add(ci);
}
} catch (Exception e) {
e.printStackTrace();
}finally{
if(con != null){
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(ps != null){
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(rs != null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
return list;
}
@Override
public List<CusInfo> selectCus() {//查询顾客信息
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
CusInfo cus = null;
List<CusInfo> list = new ArrayList<CusInfo>();
try {
Class.forName(driver);
con = DriverManager.getConnection(url,"root","root");
String sql = "select * from cusinfo";
ps = con.prepareStatement(sql);
rs = ps.executeQuery();
while(rs.next()){
int sid = rs.getInt("sid");
String sname = rs.getString("sname");
String scard = rs.getString("scard");
cus = new CusInfo();
cus.setSid(sid);
cus.setSname(sname);
cus.setScard(scard);
list.add(cus);
}
} catch (Exception e) {
e.printStackTrace();
}finally{
if(con != null){
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(ps != null){
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(rs != null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
return list;
}
@Override
public int updateById(int carId) {//根据车辆ID修改选定的租车信息(办理出租)
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
int result = 0;
try {
Class.forName(driver);
con = DriverManager.getConnection(url,"root","root");
String sql = "update carinfo set czt = czt+1 where cid=?";
ps = con.prepareStatement(sql);
ps.setInt(1,carId);
result = ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}finally{
if(con != null){
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(ps != null){
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(rs != null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
return result;
}
@Override
public CarInfo selectById1(int id) {//根据车辆ID查询车辆信息
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
CarInfo ci = null;
try {
Class.forName(driver);
con = DriverManager.getConnection(url,"root","root");
String sql = "select * from carinfo where cid=?";
ps = con.prepareStatement(sql);
ps.setInt(1,id);
rs = ps.executeQuery();
while(rs.next()){
int cid = rs.getInt("cid");
String cname = rs.getString("cname");
String cpl = rs.getString("cpl");
String crs = rs.getString("crs");
String cprice = rs.getString("cprice");
String cgs = rs.getString("cgs");
int czt = rs.getInt("czt");
int uid = rs.getInt("uid");
int sid = rs.getInt("sid");
ci = new CarInfo();
ci.setCid(cid);
ci.setCname(cname);
ci.setCpl(cpl);
ci.setCrs(crs);
ci.setCprice(cprice);
ci.setCgs(cgs);
ci.setCzt(czt);
ci.setUid(uid);
ci.setSid(sid);
}
} catch (Exception e) {
e.printStackTrace();
}finally{
if(con != null){
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(ps != null){
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(rs != null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
return ci;
}
@Override
public CusInfo selectById2(int id) {//根据车辆ID查询顾客信息
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
CusInfo cui = null;
try {
Class.forName(driver);
con = DriverManager.getConnection(url,"root","root");
String sql = "select cusinfo.* from carinfo,cusinfo where carinfo.sid=cusinfo.sid and carinfo.cid=?";
ps = con.prepareStatement(sql);
ps.setInt(1,id);
rs = ps.executeQuery();
while(rs.next()){
int sid = rs.getInt("sid");
String sname = rs.getString("sname");
String scard = rs.getString("scard");
cui = new CusInfo();
cui.setSid(sid);
cui.setSname(sname);
cui.setScard(scard);
}
} catch (Exception e) {
e.printStackTrace();
}finally{
if(con != null){
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(ps != null){
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(rs != null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
return cui;
}
}
service层下的接口
package com.hnjd.service;
import com.hnjd.entity.CarInfo;
import com.hnjd.entity.CusInfo;
import com.hnjd.entity.UserInfo;
import java.util.List;
/**
* @auther lucking
* @create 2020-10-24-17:58
**/
public interface service {
//查询用户信息(登录)
public UserInfo selectUser(String userName,String userPwd);
//查询车辆信息
public List<CarInfo> selectCar();
//查询顾客信息
public List<CusInfo> selectCus();
//根据车辆Id修改车辆信息(办理出租)
public int updateById(int carId);
//根据车辆ID查询车辆信息
public CarInfo selectById1(int id);
//根据车辆ID查询顾客信息
public CusInfo selectById2(int id);
}
package com.hnjd.service;
import com.hnjd.entity.CarInfo;
import com.hnjd.entity.CusInfo;
import com.hnjd.entity.UserInfo;
import java.util.List;
public interface service {
//查询用户信息(登录)
public UserInfo selectUser(String userName,String userPwd);
//查询车辆信息
public List<CarInfo> selectCar();
//查询顾客信息
public List<CusInfo> selectCus();
//根据车辆Id修改车辆信息(办理出租)
public int updateById(int carId);
//根据车辆ID查询车辆信息
public CarInfo selectById1(int id);
//根据车辆ID查询顾客信息
public CusInfo selectById2(int id);
}
service层下实现接口方法的类
package com.hnjd.service;
import com.hnjd.dao.DaoImp;
import com.hnjd.entity.CarInfo;
import com.hnjd.entity.CusInfo;
import com.hnjd.entity.UserInfo;
import java.util.List;
public class ServiceImp implements service {
private static DaoImp di = new DaoImp();
@Override
public UserInfo selectUser(String userName, String userPwd) {
return di.selectUser(userName,userPwd);
}
@Override
public List<CarInfo> selectCar() {
return di.selectCar();
}
@Override
public List<CusInfo> selectCus() {
return di.selectCus();
}
@Override
public int updateById(int carId) {
return di.updateById(carId);
}
@Override
public CarInfo selectById1(int id) {
return di.selectById1(id);
}
@Override
public CusInfo selectById2(int id) {
return di.selectById2(id);
}
}
servlet层
package com.hnjd.controller;
import com.hnjd.entity.CarInfo;
import com.hnjd.entity.CusInfo;
import com.hnjd.entity.UserInfo;
import com.hnjd.service.ServiceImp;
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.List;
public class Servlet extends HttpServlet {
private static ServiceImp si = new ServiceImp();//业务层私有化,便于调用
private static String name = null;//经办人(管理员)姓名
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//进行编码,防止乱码
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
PrintWriter pw = response.getWriter();
//获取服务器路径(*.do)
String path = request.getServletPath();
path = path.substring(1,path.length());
if(path.equals("login.do")){//登录
//获取浏览器传过来的账户和密码
String userName = request.getParameter("userName");
String userPwd = request.getParameter("userPwd");
//调用业务层
UserInfo ui = si.selectUser(userName,userPwd);//浏览器传过来的账户和密码与数据库里的用户信息进行对比
if(ui != null){//若ui不为空,证明有此用户,即-->登录成功
this.name = ui.getUrealname();//将管理员姓名给到-->经办人
request.getRequestDispatcher("select.do").forward(request,response);
//登录成功后,浏览器通过 select.do 转发到 select.jsp 页面,得到租车信息页面
}else{
//登录失败
pw.println("登陆失败!");
}
}else if(path.equals("select.do")){//查询租车信息 和 顾客信息
//调用业务层得到租车信息
List<CarInfo> list1 = si.selectCar();//租车信息为多条时,用List集合接收
//List<CusInfo> list2 = si.selectCus();//顾客信息为多条时,用List集合接收
if(list1 != null ){//若,list 集合得到了业务层传过来的数据(即从数据库得到租车信息)
request.setAttribute("list1",list1);//将得到的数据传递到 select.jsp 页面进行接收
//request.setAttribute("list2",list2);
request.setAttribute("name",this.name);//经办人姓名
request.getRequestDispatcher("select.jsp").forward(request,response);
//将得到的数据传递到 select.jdp 页面进行展示
}else{
//list 为 null 时 --> 没有通过业务层得到数据库的数据
pw.println("数据获取失败!");
}
}else if(path.equals("update.do")){//办理出租(修改)
//获取浏览器传过来的 车辆ID 和 顾客ID
String carid = request.getParameter("carid");
//String cusid = request.getParameter("cusid");
//调用业务层确定要修改的某一条车辆数据
int result = si.updateById(Integer.parseInt(carid));
if(result != 0){//修改成功
request.getRequestDispatcher("select.do").forward(request,response);
//修改成功后,服务器地址转发到 select.do,通过它将页面再转发到 select.jsp 页面进行展示
}else{
//修改失败
pw.println("修改失败!");
}
}else if(path.equals("selectByid.do")){//出租状态为 已出租,查询出租详情
//获取浏览器传过来的 车辆ID,以确定某个顾客与车辆的信息
String id = request.getParameter("carid");
System.out.println(id);
//将浏览器传过来的 车辆ID,给到业务层,以确定某个顾客与车辆的信息
CarInfo car = si.selectById1(Integer.parseInt(id));//确定车辆
CusInfo cus = si.selectById2(Integer.parseInt(id));//确定客户
if(car != null && cus != null){//信息确定成功
request.setAttribute("car",car);//将车辆信息传给 xingqing.jsp(详情页面)
request.setAttribute("cus",cus);//将顾客信息传给 xingqing.jsp(详情页面)
request.getRequestDispatcher("xingqing.jsp").forward(request,response);
//将已得到的数据转发到 xingqing.jsp 页面
}else{
//信息确定失败
pw.println("数据获取失败!");
}
}
}
}
XML文件配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>CarsrentServlet</servlet-name>
<servlet-class>com.hnjd.controller.Servlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CarsrentServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
jsp登录页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>登录</title>
</head>
<body>
<p>顺丰租车公司后台管理系统</p>
<form action="login.do" method="post">
账号:<input type="text" name="userName"><br><br>
密码:<input type="password" name="userPwd"><br><br>
<input type="submit" value="登录">
</form>
</body>
</html>
查询页面
<%@ page import="java.util.ArrayList" %>
<%@ page import="com.hnjd.entity.CarInfo" %>
<%@ page import="com.hnjd.entity.CusInfo" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>租车信息</title>
</head>
<body>
<%
//接收 Servlet 类传过来的信息
ArrayList<CarInfo> list1 = (ArrayList<CarInfo>)request.getAttribute("list1");
//ArrayList<CusInfo> list2 = (ArrayList<CusInfo>)request.getAttribute("list2");
if(list1 != null){//接收到数据,进行遍历展示出来
%>
<table border="2">
<thead>
<tr>
<th>编号</th>
<th>车辆名称</th>
<th>排量</th>
<th>可载人数</th>
<th>日租价</th>
<th>出租公司</th>
<th>车辆状态</th>
<th>经办人</th>
<th>操作</th>
</tr>
</thead>
<%
//进行遍历
for(CarInfo car : list1){
%>
<tbody>
<tr>
<th><%=car.getCid()%></th>
<th><%=car.getCname()%></th>
<th><%=car.getCpl()%></th>
<th><%=car.getCrs()%></th>
<th><%=car.getCprice()%></th>
<th><%=car.getCgs()%></th>
<%
if(car.getCzt() == 0){
%>
<th>未出租</th>
<th><%=request.getAttribute("name")%></th>
<th><a href="<%=request.getContextPath()%>/update.do?carid=<%=car.getCid()%>">办理出租</a></th>
<%
}else if(car.getCzt() == 1){
%>
<th><a href="<%=request.getContextPath()%>/selectByid.do?carid=<%=car.getCid()%>">已出租</a></th>
<th><%=request.getAttribute("name")%></th>
<th>暂无业务</th>
<%}%>
</tr>
</tbody>
<%}%>
</table>
<%}%>
</body>
</html>
详情页面
<%@ page import="com.hnjd.entity.CarInfo" %>
<%@ page import="com.hnjd.entity.CusInfo" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>租车明细</title>
</head>
<body>
<%
CarInfo car = (CarInfo)request.getAttribute("car");//获取车辆信息
CusInfo cus = (CusInfo)request.getAttribute("cus");//获取顾客信息
if(car != null && cus != null){
%>
<table>
<thead>
<tr>
<th colspan="2">车辆出租明细</th>
</tr>
</thead>
<tbody>
<tr>
<th>车辆名称:</th>
<th><%=car.getCname()%></th>
</tr>
<tr>
<th>排量:</th>
<th><%=car.getCpl()%></th>
</tr>
<tr>
<th>可载人数:</th>
<th><%=car.getCrs()%></th>
</tr>
<tr>
<th>日租价:</th>
<th><%=car.getCprice()%></th>
</tr>
</tbody>
</table>
<table>
<thead>
<tr>
<th colspan="2">租赁人信息</th>
</tr>
</thead>
<tbody>
<tr>
<th>客户姓名:</th>
<th><%=cus.getSname()%></th>
</tr>
<tr>
<th>身份证号:</th>
<th><%=cus.getScard()%></th>
</tr>
<tr>
<th>租赁时间:</th>
<%
int time = 3;//默认租赁时间为3天
int sum = time * Integer.parseInt(car.getCprice());//租赁总价
%>
<th><%=time%>天</th>
</tr>
<tr>
<th>备注说明:</th>
<th>暂无</th>
</tr>
</tbody>
</table>
<table>
<thead>
<tr>
<th colspan="2">租金合计</th>
</tr>
</thead>
<tbody>
<tr>
<th>合计:</th>
<th><%=sum%>元</th>
</tr>
<tr>
<th><a href="<%=request.getContextPath()%>/select.do">关闭</a></th>
</tr>
</tbody>
</table>
<%}%>
</body>
</html>
项目整体流程图及MySQL所需要创建的表




相关配置文件
使用 tomcat9.0
jdk-13.0.1
mysql-connector-java-8.0.22.jar
本文含有隐藏内容,请 开通VIP 后查看