【数据库】停车场系统

简介: 【数据库】停车场系统

1. 项目架构图

2. 系统分析

2.1. 数据库

2.1.1. 表parking

2.1.2 表record

2.2 java端

2.2.1 JBDC

2.2.1.1JDBC连接
package com.situ.parking.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ResourceBundle;
public class JDBCUtil {
  private static ResourceBundle rb = ResourceBundle.getBundle("com.situ.parking.util.JDBC");
  private static String cLassName = rb.getString("driver");
  private static String url = rb.getString("url");
  private static String user = rb.getString("user");
  private static String password = rb.getString("pass");
  // 1- 加载驱动
  static {
    try {
      Class.forName(cLassName);
    } catch (Exception e) {
      e.getCause();
    }
  }
  // 2- 获得链接
  public static Connection getConnection() throws SQLException {
    return DriverManager.getConnection(url, user, password);
  }
  public static void close(Connection conn, Statement st, ResultSet rs) {
    try {
      if (rs != null) {
        rs.close();
        rs = null;
      }
      if (st != null) {
        st.close();
        st = null;
      }
      if (conn != null) {
        conn.close();
        conn = null;
      }
    } catch (SQLException e) {
      e.printStackTrace();
    }
  }
  public static void close(Connection conn, Statement st) {
    close(conn, st, null);
  }
  public static void main(String[] args) throws SQLException {
    // 加载驱动 + 获得链接
    Connection con = JDBCUtil.getConnection();
    System.out.println(con);
    JDBCUtil.close(con, null); // 关闭的是插入的
  }
  private JDBCUtil() {
  }
}
2.2.1.1JDBC配置文件
user=root
pass=123456
driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3308/demo02?useSSL=false&useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&serverTimezone=Hongkong&allowPublicKeyRetrieval=true

2.2.2 Parking包

2.2.2.1 Model
package com.situ.parking.parking.model;
public class ParkingModel {
  private String id; // 停车场的编号
  private String isEmpty; // 有没有车
  private String carName; // 车的号码
  private String startDate; // 开始的时间
  public String getId() {
    return id;
  }
  public void setId(String id) {
    this.id = id;
  }
  public String getIsEmpty() {
    return isEmpty;
  }
  public void setIsEmpty(String isEmpty) {
    this.isEmpty = isEmpty;
  }
  public String getCarName() {
    return carName;
  }
  public void setCarName(String carName) {
    this.carName = carName;
  }
  public String getStartDate() {
    return startDate;
  }
  public void setStartDate(String startDate) {
    this.startDate = startDate;
  }
  @Override
  public String toString() {
    return "ParkingModel [id=" + id + ", isEmpty=" + isEmpty + ", carName=" + carName + ", startDate=" + startDate
        + "]";
  }
}
2.2.2.2 Dao
package com.situ.parking.parking.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.situ.parking.parking.model.ParkingModel;
import com.situ.parking.util.JDBCUtil;
public class ParkingDao {
  Connection connection = null;
  PreparedStatement ps = null;
  public String insert(ParkingModel model) {
    String sql = "insert into parking(p_id, isempty, carName, StartDate) values(?,?,?,?)";
    try {
      connection = JDBCUtil.getConnection();
      ps = connection.prepareStatement(sql);
      ps.setString(1, model.getId());
      ps.setString(2, model.getIsEmpty());
      ps.setString(3, model.getCarName());
      ps.setString(4, model.getStartDate());
      int res = ps.executeUpdate();
      return res + "";
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      JDBCUtil.close(connection, ps);
    }
    return -1 + "";
  }
  public String delete(ParkingModel model) {
    String sql = "delete from parking where p_id = ?";
    try {
      connection = JDBCUtil.getConnection();
      ps = connection.prepareStatement(sql);
      ps.setString(1, model.getId());
      int res = ps.executeUpdate();
      return res + "";
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      JDBCUtil.close(connection, ps);
    }
    return -1 + "";
  }
  public String update(ParkingModel model) {
    String sql = "update parking set isempty = ?, carname = ?, startdate = ? where p_id = ?";
    try {
      connection = JDBCUtil.getConnection();
      ps = connection.prepareStatement(sql);
      ps.setString(1, model.getIsEmpty());
      ps.setString(2, model.getCarName());
      ps.setString(3, model.getStartDate());
      ps.setString(4, model.getId());
      int res = ps.executeUpdate();
      return res + "";
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      JDBCUtil.close(connection, ps);
    }
    return -1 + "";
  }
  public List<ParkingModel> select(ParkingModel model) {
    StringBuffer sql = new StringBuffer("select p_id, isempty, carName, startDate from parking where 1 = 1");
    List<Object> list = new ArrayList<Object>();
    list = where(model, sql);
    System.out.println(sql);
    List<ParkingModel> list2 = new ArrayList<ParkingModel>();
    ResultSet set;
    try {
      connection = JDBCUtil.getConnection();
      ps = connection.prepareStatement(sql.toString());
      for (int i = 0; i < list.size(); i++) {
        ps.setObject(i + 1, list.get(i));
      }
      System.out.println("1");
      set = ps.executeQuery();
      System.out.println("1");
      while (set.next()) {
        ParkingModel model2 = new ParkingModel();
        model2.setId(set.getString("p_id"));
        model2.setIsEmpty(set.getString("isempty"));
        model2.setCarName(set.getString("carname"));
        model2.setStartDate(set.getString("startdate"));
        list2.add(model2);
      }
    } catch (Exception e) {
      e.printStackTrace();
      return null;
    } finally {
      JDBCUtil.close(connection, ps);
    }
    return list2;
  }
  public List<Object> where(ParkingModel model, StringBuffer sql) {
    List<Object> list = new ArrayList<Object>();
    String id = model.getId();
    if (id != null && !id.trim().isEmpty()) {
      sql.append(" and p_id = ?");
      list.add(id);
    }
    String isempty = model.getIsEmpty();
    if (isempty != null && !isempty.trim().isEmpty()) {
      sql.append(" and isempty = ?");
      list.add(isempty);
    }
    String carName = model.getCarName();
    if (carName != null && !carName.trim().isEmpty()) {
      sql.append(" and carName = ?");
      list.add(carName);
    }
    String startDate = model.getStartDate();
    if (startDate != null && !startDate.trim().isEmpty()) {
      sql.append(" and startDate = ?");
      list.add(startDate);
    }
    return list;
  }
}
2.2.2.3 Service
package com.situ.parking.parking.service;
import java.util.ArrayList;
import java.util.List;
import com.situ.parking.parking.dao.ParkingDao;
import com.situ.parking.parking.model.ParkingModel;
public class ParkingService {
  ParkingDao dao = new ParkingDao();
  public String insert(ParkingModel model) {
    ParkingModel model2 = new ParkingModel();
    model2.setId(model.getId());
    List<ParkingModel> list = new ArrayList<ParkingModel>();
    list = dao.select(model2);
    if (list == null || list.isEmpty()) {
      return dao.insert(model);
    }
    return null;
  }
  public String delete(ParkingModel model) {
    return dao.delete(model);
  }
  public String update(ParkingModel model) {
    return dao.update(model);
  }
  public List<ParkingModel> selectList(ParkingModel model) {
    return dao.select(model);
  }
  public ParkingModel selectModel(ParkingModel model) {
    List<ParkingModel> list = new ArrayList<ParkingModel>();
    list = dao.select(model);
    if (list == null || list.isEmpty()) {
      return null;
    }
    return list.get(0);
  }
}

2.2.3 record包

2.2.3.1 Model
package com.situ.parking.record.model;
public class RecordModel {
  private int id; // 记录的主键
  private String carId; // 停车位的编号
  private String carName; // 车的车牌
  private String startDate; // 开始停放时间
  private String endDate; // 结束停放时间
  private String price; // 价格
  public int getId() {
    return id;
  }
  public void setId(int id) {
    this.id = id;
  }
  public String getCarId() {
    return carId;
  }
  public void setCarId(String carId) {
    this.carId = carId;
  }
  public String getCarName() {
    return carName;
  }
  public void setCarName(String carName) {
    this.carName = carName;
  }
  public String getStartDate() {
    return startDate;
  }
  public void setStartDate(String startDate) {
    this.startDate = startDate;
  }
  public String getEndDate() {
    return endDate;
  }
  public void setEndDate(String endDate) {
    this.endDate = endDate;
  }
  public String getPrice() {
    return price;
  }
  public void setPrice(String price) {
    this.price = price;
  }
  @Override
  public String toString() {
    return "RecordModel [id=" + id + ", carId=" + carId + ", carName=" + carName + ", startDate=" + startDate
        + ", endDate=" + endDate + ", price=" + price + "]";
  }
}
2.2.3.2 Dao
package com.situ.parking.record.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.situ.parking.record.model.RecordModel;
import com.situ.parking.util.JDBCUtil;
public class RecordDao {
  Connection connection = null;
  PreparedStatement ps = null;
  public String insert(RecordModel model) {
    String sql = "insert into record(carid, carname, startdate, enddate, price) values(?,?,?,?,?)";
    try {
      connection = JDBCUtil.getConnection();
      ps = connection.prepareStatement(sql);
      ps.setString(1, model.getCarId());
      ps.setString(2, model.getCarName());
      ps.setString(3, model.getStartDate());
      ps.setString(4, model.getEndDate());
      ps.setString(5, model.getPrice());
      int res = ps.executeUpdate();
      return res + "";
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      JDBCUtil.close(connection, ps);
    }
    return -1 + "";
  }
  public String delete(RecordModel model) {
    String sql = "delete from record where id = ?";
    try {
      connection = JDBCUtil.getConnection();
      ps = connection.prepareStatement(sql);
      ps.setInt(1, model.getId());
      int res = ps.executeUpdate();
      return res + "";
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      JDBCUtil.close(connection, ps);
    }
    return -1 + "";
  }
  public String update(RecordModel model) {
    String sql = "update record set carid = ?, carname = ?, startdate = ?, enddate = ?, price = ? where id = ?";
    try {
      connection = JDBCUtil.getConnection();
      ps = connection.prepareStatement(sql);
      ps.setString(1, model.getCarName());
      ps.setString(2, model.getStartDate());
      ps.setString(3, model.getEndDate());
      ps.setString(4, model.getPrice());
      ps.setInt(5, model.getId());
      int res = ps.executeUpdate();
      return res + "";
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      JDBCUtil.close(connection, ps);
    }
    return -1 + "";
  }
  public List<RecordModel> select(RecordModel model) {
    StringBuffer sql = new StringBuffer(
        "select id, carid, carname, startdate, enddate, price from record where 1=1");
    List<Object> list = new ArrayList<Object>();
    list = where(model, sql);
    List<RecordModel> list2 = new ArrayList<RecordModel>();
    ResultSet set;
    try {
      connection = JDBCUtil.getConnection();
      ps = connection.prepareStatement(sql.toString());
      for (int i = 0; i < list.size(); i++) {
        ps.setObject(i + 1, list.get(i));
      }
      set = ps.executeQuery();
      while (set.next()) {
        RecordModel model2 = new RecordModel();
        model2.setId(set.getInt("id"));
        model2.setCarId(set.getString("carid"));
        model2.setCarName(set.getString("carname"));
        model2.setStartDate(set.getString("startdate"));
        model2.setEndDate(set.getString("enddate"));
        model2.setPrice(set.getString("price"));
        list2.add(model2);
      }
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      JDBCUtil.close(connection, ps);
    }
    return list2;
  }
  public List<Object> where(RecordModel model, StringBuffer sql) {
    List<Object> list = new ArrayList<Object>();
    int id = model.getId();
    if (id != 0) {
      sql.append("and id = ?");
      list.add(id);
    }
    String carId = model.getCarId();
    if (carId != null && !carId.trim().isEmpty()) {
      sql.append("and carid = ?");
      list.add(carId);
    }
    String carname = model.getCarName();
    if (carname != null && !carname.trim().isEmpty()) {
      sql.append("and carname = ?");
      list.add(carname);
    }
    String startdate = model.getStartDate();
    if (startdate != null && !startdate.trim().isEmpty()) {
      sql.append("and startdate = ?");
      list.add(startdate);
    }
    String enddate = model.getEndDate();
    if (enddate != null && !enddate.trim().isEmpty()) {
      sql.append("and enddate = ?");
      list.add(enddate);
    }
    String price = model.getPrice();
    if (price != null && !price.trim().isEmpty()) {
      sql.append("and price = ?");
      list.add(price);
    }
    return list;
  }
}
2.2.3.3 Service
package com.situ.parking.record.service;
import java.util.List;
import com.situ.parking.record.dao.RecordDao;
import com.situ.parking.record.model.RecordModel;
public class RecordService {
  RecordDao dao = new RecordDao();
  public String insert(RecordModel model) {
    return dao.insert(model);
  }
  public String delete(RecordModel model) {
    return dao.delete(model);
  }
  public String update(RecordModel model) {
    return dao.update(model);
  }
  public List<RecordModel> selectList(RecordModel model) {
    return dao.select(model);
  }
  public void selectModel(RecordModel model) {
  }
}

2.2.3 Test

package com.situ.parking.test;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import com.situ.parking.parking.model.ParkingModel;
import com.situ.parking.parking.service.ParkingService;
import com.situ.parking.record.model.RecordModel;
import com.situ.parking.record.service.RecordService;
public class Test {
  Scanner scanner = new Scanner(System.in);
  DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
  boolean f = true;
  ParkingService parkingService = new ParkingService();
  RecordService recordService = new RecordService();
  public static void main(String[] args) {
    Test parkingManagement = new Test();
    parkingManagement.start();
  }
  public void updateModel(String id) {
    ParkingModel model = new ParkingModel();
    model.setId(id);
    model.setIsEmpty("true");
    model.setCarName("无");
    model.setStartDate("无");
    parkingService.update(model);
  }
  public void start() {
    while (f) {
      System.out.println("1.停车");
      System.out.println("2.取车");
      System.out.println("3.查询全部停车位的状态");
      System.out.println("4.查询全部的消费记录");
      System.out.println("5.退出");
      System.out.println("请输入你要执行的操作:");
      String string = scanner.nextLine();
      start_1(string);
    }
  }
  public void start_1(String string) {
    if (string.equals("1")) {
      parkingCar();
    } else if (string.equals("2")) {
      getCar();
    } else if (string.equals("3")) {
      queryParking();
    } else if (string.equals("4")) {
      queryRecord();
    } else if (string.equals("5")) {
      f = false;
      System.out.println("已为你退出系统!");
    } else {
      System.out.println("你的输入有误!");
    }
  }
  public void parkingCar() {
    System.out.println("请输入车辆的信息:");
    System.out.println("车牌:");
    String carName = scanner.nextLine();
    System.out.println("高度:");
    String height = scanner.nextLine();
    double height_ = Double.parseDouble(height);
    if (height_ > 3.0) {
      System.out.println("对不起,当前车辆高度大于3米,不允许进入。");
      start();
    }
    System.out.println("请输入你想停放的停车位:");
    String id_ = scanner.nextLine();
    ParkingModel model = new ParkingModel();
    model.setId(id_);
    if (parkingService.selectModel(model).getIsEmpty().equals("false")) {
      System.out.println("对不起,当前位置已经有车!");
    } else {
      model.setCarName(carName);
      model.setIsEmpty("false");
      LocalDateTime startDate_ = LocalDateTime.now();
      String startDate = df.format(startDate_);
      model.setStartDate(startDate);
      parkingService.update(model);
    }
  }
  public void getCar() {
    System.out.println("1.按车牌取车");
    System.out.println("2.按停车位取车");
    System.out.println("请选择你的操作:");
    String string = scanner.nextLine();
    if (string.equals("1")) {
      getCarName();
    } else if (string.equals("2")) {
      getCarParking();
    } else {
      System.out.println("你的输入有误。");
      start();
    }
  }
  public void getCarName() {
    System.out.println("请输入你的车牌号:");
    String carName = scanner.nextLine(); // 车牌号
    ParkingModel model = new ParkingModel();
    model.setCarName(carName);
    ParkingModel model1 = parkingService.selectModel(model); // 利用车名称找到记录
    if (model1 != null) {
      String id = model1.getId(); // 获取这是第几个停车位
      updateModel(id);
      String startdate_ = model1.getStartDate();
      LocalDateTime startdate = LocalDateTime.parse(startdate_, df);
      LocalDateTime enddate = LocalDateTime.now();
      int price = (enddate.getSecond() - startdate.getSecond()) / 10 * 2; // 按照10s2元的价格 便于查看
      RecordModel model2 = new RecordModel();
      model2.setCarId(id); // 输入停车位
      model2.setCarName(carName);
      model2.setStartDate(startdate_);
      DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
      model2.setEndDate(fmt.format(enddate));
      model2.setPrice(String.valueOf(price));
      recordService.insert(model2);
    } else {
      System.out.println("没有查到!");
    }
  }
  public void getCarParking() {
    System.out.println("请输入你的停车位:");
    String id = scanner.nextLine();
    ParkingModel model = new ParkingModel();
    model.setId(id);
    ParkingModel model1 = parkingService.selectModel(model); // 利用停车场id找到记录
    if (model1 != null) {
      updateModel(id);
      String carName = model1.getCarName();// 获取这个停车位的车牌号
      String startdate_ = model1.getStartDate();
      LocalDateTime startdate = LocalDateTime.parse(startdate_, df); // 转成时间类型
      LocalDateTime enddate = LocalDateTime.now();
      int price = (enddate.getSecond() - startdate.getSecond()) / 10 * 2; // 按照10s2元的价格 便于查看
      RecordModel model2 = new RecordModel();
      model2.setCarId(id); // 输入停车位
      model2.setCarName(carName);
      model2.setStartDate(startdate_);
      DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
      model2.setEndDate(fmt.format(enddate));
      model2.setPrice(String.valueOf(price));
      recordService.insert(model2);
    } else {
      System.out.println("没有查到!");
    }
  }
  public void queryParking() {
    List<ParkingModel> list = new ArrayList<ParkingModel>();
    ParkingModel model = new ParkingModel();
    list = parkingService.selectList(model);
    for (ParkingModel parkingModel : list) {
      System.out.println(parkingModel);
    }
  }
  public void queryRecord() {
    List<RecordModel> list = new ArrayList<RecordModel>();
    RecordModel model = new RecordModel();
    list = recordService.selectList(model);
    for (RecordModel recordModel : list) {
      System.out.println(recordModel);
    }
  }
}


相关文章
|
4月前
|
关系型数据库 OLAP 分布式数据库
核心系统转型问题之Gartner分析师对阿里云数据库的评价是啥样的
核心系统转型问题之Gartner分析师对阿里云数据库的评价是啥样的
|
4月前
|
Cloud Native 数据管理 数据挖掘
核心系统转型问题之阿里云数据库用户需求的通用性和差异性如何平衡
核心系统转型问题之阿里云数据库用户需求的通用性和差异性如何平衡
|
1月前
|
数据库连接 Go 数据库
Go语言中的错误注入与防御编程。错误注入通过模拟网络故障、数据库错误等,测试系统稳定性
本文探讨了Go语言中的错误注入与防御编程。错误注入通过模拟网络故障、数据库错误等,测试系统稳定性;防御编程则强调在编码时考虑各种错误情况,确保程序健壮性。文章详细介绍了这两种技术在Go语言中的实现方法及其重要性,旨在提升软件质量和可靠性。
32 1
|
1月前
|
关系型数据库 MySQL Linux
Linux系统如何设置自启动服务在MySQL数据库启动后执行?
【10月更文挑战第25天】Linux系统如何设置自启动服务在MySQL数据库启动后执行?
115 3
|
1月前
|
Java 数据库连接 数据库
深入探讨Java连接池技术如何通过复用数据库连接、减少连接建立和断开的开销,从而显著提升系统性能
在Java应用开发中,数据库操作常成为性能瓶颈。本文通过问题解答形式,深入探讨Java连接池技术如何通过复用数据库连接、减少连接建立和断开的开销,从而显著提升系统性能。文章介绍了连接池的优势、选择和使用方法,以及优化配置的技巧。
39 1
|
2月前
|
SQL 存储 关系型数据库
数据储存数据库管理系统(DBMS)
【10月更文挑战第11天】
148 3
|
2月前
|
存储 关系型数据库 MySQL
PACS系统 中 dicom 文件在mysql 8.0 数据库中的 存储和读取(pydicom 库使用)
PACS系统 中 dicom 文件在mysql 8.0 数据库中的 存储和读取(pydicom 库使用)
43 2
|
3月前
|
JavaScript Java 关系型数据库
毕设项目&课程设计&毕设项目:基于springboot+vue实现的在线考试系统(含教程&源码&数据库数据)
本文介绍了一个基于Spring Boot和Vue.js实现的在线考试系统。随着在线教育的发展,在线考试系统的重要性日益凸显。该系统不仅能提高教学效率,减轻教师负担,还为学生提供了灵活便捷的考试方式。技术栈包括Spring Boot、Vue.js、Element-UI等,支持多种角色登录,具备考试管理、题库管理、成绩查询等功能。系统采用前后端分离架构,具备高性能和扩展性,未来可进一步优化并引入AI技术提升智能化水平。
毕设项目&课程设计&毕设项目:基于springboot+vue实现的在线考试系统(含教程&源码&数据库数据)
|
3月前
|
Java 关系型数据库 MySQL
毕设项目&课程设计&毕设项目:springboot+jsp实现的房屋租租赁系统(含教程&源码&数据库数据)
本文介绍了一款基于Spring Boot和JSP技术的房屋租赁系统,旨在通过自动化和信息化手段提升房屋管理效率,优化租户体验。系统采用JDK 1.8、Maven 3.6、MySQL 8.0、JSP、Layui和Spring Boot 2.0等技术栈,实现了高效的房源管理和便捷的租户服务。通过该系统,房东可以轻松管理房源,租户可以快速找到合适的住所,双方都能享受数字化带来的便利。未来,系统将持续优化升级,提供更多完善的服务。
毕设项目&课程设计&毕设项目:springboot+jsp实现的房屋租租赁系统(含教程&源码&数据库数据)
|
2月前
|
安全 NoSQL 关系型数据库
阿里云数据库:构建高性能与安全的数据管理系统
在企业数字化转型过程中,数据库是支撑企业业务运转的核心。随着数据量的急剧增长和数据处理需求的不断增加,企业需要一个既能提供高性能又能保障数据安全的数据库解决方案。阿里云数据库产品为企业提供了一站式的数据管理服务,涵盖关系型、非关系型、内存数据库等多种类型,帮助企业构建高效的数据基础设施。
132 2