реализации интерфейсов взаимодействия с бд

This commit is contained in:
parap 2023-05-22 03:40:05 +04:00
parent dbabc4561b
commit 0c4943d746
5 changed files with 1076 additions and 0 deletions

View File

@ -0,0 +1,129 @@
package org.dbms.storageImpl;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;
import org.dbms.models.Car;
import org.dbms.models.Parking;
import org.dbms.storageContracts.ICarStorage;
import org.dbms.storageContracts.IParkingStorage;
import org.springframework.stereotype.Component;
@Component
public class CarStorage implements ICarStorage {
private final static String readRecords;
private final static String readRecordById;
private final static String insertRecord;
private final static String deleteRecord;
static
{
readRecords = "SELECT id, model, year, cost, parkingId FROM cars";
readRecordById = new StringBuilder()
.append("SELECT id, model, year, cost, parkingId FROM cars")
.append("\n")
.append("WHERE id = ?").toString();
insertRecord = "INSERT INTO cars (model, year, cost, parkingId) VALUES(?, ?, ?, ?)";
deleteRecord = "DELETE FROM cars WHERE id = ?";
}
private final Connection connection;
private final IParkingStorage parkingStorage;
public CarStorage(Connection connection, IParkingStorage parkingStorage) {
this.connection = connection;
this.parkingStorage = parkingStorage;
}
@Override
public List<Car> readAll() {
ArrayList<Car> list = new ArrayList<>();
try {
PreparedStatement preparedStatement = connection.prepareStatement(readRecords);
ResultSet resultSet = preparedStatement.executeQuery();
list.ensureCapacity(resultSet.getFetchSize());
while (resultSet.next()) {
Long id = resultSet.getLong(1);
String model = resultSet.getString(2);
Date year = resultSet.getDate(3);
Double cost = resultSet.getDouble(4);
Long parkingId = resultSet.getLong(5);
Parking p = parkingStorage.getElementById(parkingId);
list.add(new Car(id, model, year, cost, p));
}
resultSet.close();
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
return list;
}
@Override
public Car getElementById(Long carId) {
Car res = null;
try {
PreparedStatement preparedStatement = connection.prepareStatement(readRecordById);
preparedStatement.setLong(1, carId);
ResultSet resultSet = preparedStatement.executeQuery();
if(!resultSet.next()) return null;
Long id = resultSet.getLong(1);
String model = resultSet.getString(2);
Date year = resultSet.getDate(3);
Double cost = resultSet.getDouble(4);
Long parkingId = resultSet.getLong(5);
Parking p = parkingStorage.getElementById(parkingId);
res = new Car(id, model, year, cost, p);
resultSet.close();
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
return res;
}
public void insert(Car car) {
try {
PreparedStatement preparedStatement = connection.prepareStatement(insertRecord);
preparedStatement.setString(1, car.getModel());
preparedStatement.setDate(2, new java.sql.Date(car.getYear().getTime()));
preparedStatement.setDouble(3, car.getCost());
preparedStatement.setLong(4, car.getParking().getId());
preparedStatement.executeUpdate();
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public void delete(Long carId) {
try {
PreparedStatement preparedStatement = connection.prepareStatement(deleteRecord);
preparedStatement.setLong(1, carId);
preparedStatement.executeUpdate();
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}

View File

@ -0,0 +1,179 @@
package org.dbms.storageImpl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;
import org.dbms.models.Client;
import org.dbms.models.Parking;
import org.dbms.storageContracts.IClientStorage;
import org.springframework.stereotype.Component;
@Component
public class ClientStorage implements IClientStorage {
private final static String readRecords;
private final static String readRecordById;
private final static String readRecordByLogin;
private final static String insertRecord;
private final static String deleteRecord;
private final static String loginExistQuery;
static
{
readRecords = "SELECT id, name, phone, login, password FROM clients";
readRecordById = new StringBuilder()
.append("SELECT id, name, phone, login, password FROM clients")
.append("\n")
.append("WHERE id = ?").toString();
readRecordByLogin = new StringBuilder()
.append("SELECT id, name, phone, login, password FROM clients")
.append("\n")
.append("WHERE login = ?").toString();
insertRecord = "INSERT INTO clients (name, phone, login, password) VALUES(?, ?, ?, ?)";
deleteRecord = "DELETE FROM clients WHERE id = ?";
loginExistQuery = "SELECT id FROM clients WHERE login = ?";
}
private final Connection connection;
public ClientStorage(Connection connection) {
this.connection = connection;
}
@Override
public List<Client> readAll() {
ArrayList<Client> list = new ArrayList<>();
try {
PreparedStatement preparedStatement = connection.prepareStatement(readRecords);
ResultSet resultSet = preparedStatement.executeQuery();
list.ensureCapacity(resultSet.getFetchSize());
while (resultSet.next()) {
Long id = resultSet.getLong(1);
String name = resultSet.getString(2);
String phone = resultSet.getString(3);
String login = resultSet.getString(4);
String password = resultSet.getString(5);
list.add(new Client(id, name, phone, login, password));
}
resultSet.close();
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
return list;
}
@Override
public Client getElementById(Long clientId) {
Client res = null;
try {
PreparedStatement preparedStatement = connection.prepareStatement(readRecordById);
preparedStatement.setLong(1, clientId);
ResultSet resultSet = preparedStatement.executeQuery();
resultSet.next();
Long id = resultSet.getLong(1);
String name = resultSet.getString(2);
String phone = resultSet.getString(3);
String login = resultSet.getString(4);
String password = resultSet.getString(5);
res = new Client(id, name, phone, login, password);
resultSet.close();
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
return res;
}
@Override
public void insert(Client client) {
try {
PreparedStatement preparedStatement = connection.prepareStatement(insertRecord);
preparedStatement.setString(1, client.getName());
preparedStatement.setString(2, client.getPhone());
preparedStatement.setString(3, client.getLogin());
preparedStatement.setString(4, client.getPassword());
preparedStatement.executeUpdate();
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public void delete(Long clientId) {
try {
PreparedStatement preparedStatement = connection.prepareStatement(deleteRecord);
preparedStatement.setLong(1, clientId);
preparedStatement.executeUpdate();
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public Client getByLogin(String clientLogin) {
Client res = null;
try {
PreparedStatement preparedStatement = connection.prepareStatement(readRecordByLogin);
preparedStatement.setString(1, clientLogin);
ResultSet resultSet = preparedStatement.executeQuery();
if(!resultSet.next()) return null;
Long id = resultSet.getLong(1);
String name = resultSet.getString(2);
String phone = resultSet.getString(3);
String login = resultSet.getString(4);
String password = resultSet.getString(5);
res = new Client(id, name, phone, login, password);
resultSet.close();
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
return res;
}
@Override
public String getIdByLogin(String login) {
try {
PreparedStatement preparedStatement = connection.prepareStatement(loginExistQuery);
preparedStatement.setString(1, login);
ResultSet resultSet = preparedStatement.executeQuery();
if(resultSet.next()) {
return resultSet.getInt(1) + "";
}
return null;
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
}

View File

@ -0,0 +1,394 @@
package org.dbms.storageImpl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;
import org.dbms.models.Car;
import org.dbms.models.Client;
import org.dbms.models.Driver;
import org.dbms.models.Order;
import org.dbms.searchModels.DriverSearch;
import org.dbms.storageContracts.ICarStorage;
import org.dbms.storageContracts.IDriverStorage;
import org.springframework.stereotype.Component;
@Component
public class DriverStorage implements IDriverStorage {
private final static String readRecords;
private final static String readPage;
private final static String readCount;
private final static String readFiltered;
private final static String readFilteredCount;
private final static String readRecordById;
private final static String insertRecord;
private final static String deleteRecord;
static
{
readRecords = "SELECT id, name, licenseYear, carId FROM drivers";
readCount = "SELECT COUNT(*) FROM drivers";
readPage = new StringBuilder()
.append("SELECT id, name, licenseYear, carId FROM drivers")
.append("\n")
.append("ORDER BY id")
.append("\n")
.append("OFFSET ?")
.append("\n")
.append("LIMIT ?").toString();
readRecordById = new StringBuilder()
.append("SELECT id, name, licenseYear, carId FROM drivers")
.append("\n")
.append("WHERE id = ?").toString();
readFiltered = new StringBuilder()
.append("SELECT id, name, licenseYear, carId FROM drivers")
.append("\n")
.append("WHERE ").toString();
readFilteredCount = new StringBuilder()
.append("SELECT COUNT(*) FROM drivers")
.append("\n")
.append("WHERE ").toString();
insertRecord = "INSERT INTO drivers (name, licenseYear, carId) VALUES(?, ?, ?)";
deleteRecord = "DELETE FROM drivers WHERE id = ?";
}
private final Connection connection;
private final ICarStorage carStorage;
public DriverStorage(Connection connection, ICarStorage carStorage) {
this.connection = connection;
this.carStorage = carStorage;
}
@Override
public List<Driver> readAll() {
ArrayList<Driver> list = new ArrayList<>();
try {
PreparedStatement preparedStatement = connection.prepareStatement(readRecords);
ResultSet resultSet = preparedStatement.executeQuery();
list.ensureCapacity(resultSet.getFetchSize());
while (resultSet.next()) {
Long id = resultSet.getLong(1);
String name = resultSet.getString(2);
Date licenseYear = resultSet.getDate(3);
Long carId = resultSet.getLong(4);
Car c = carStorage.getElementById(carId);
list.add(new Driver(id, name, licenseYear, c));
}
resultSet.close();
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
return list;
}
@Override
public List<Driver> readPage(int size, int page) {
ArrayList<Driver> list = new ArrayList<>();
try {
PreparedStatement preparedStatement = connection.prepareStatement(readPage);
preparedStatement.setInt(1, page - 1);
preparedStatement.setInt(2, size);
ResultSet resultSet = preparedStatement.executeQuery();
list.ensureCapacity(resultSet.getFetchSize());
while (resultSet.next()) {
Long id = resultSet.getLong(1);
String name = resultSet.getString(2);
Date licenseYear = resultSet.getDate(3);
Long carId = resultSet.getLong(4);
Car c = carStorage.getElementById(carId);
list.add(new Driver(id, name, licenseYear, c));
}
resultSet.close();
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
return list;
}
public int getCount() {
int res = 0;
try {
PreparedStatement preparedStatement = connection.prepareStatement(readCount);
ResultSet resultSet = preparedStatement.executeQuery();
if (resultSet.next()) {
res = resultSet.getInt(1);
}
resultSet.close();
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
return res;
}
@Override
public int getFilteredCount(DriverSearch filter) {
if(filter.expirienceTo == null && filter.expirienceFrom == null) return getCount();
StringBuilder query = new StringBuilder(readFilteredCount);
boolean isFirst = true;
if(filter.expirienceFrom != null) {
if(!isFirst) {
query.append(" AND ");
}
query.append("EXTRACT(YEAR FROM age(NOW(), licenseYear)) >= ?");
query.append("\n");
isFirst = false;
}
if(filter.expirienceTo != null) {
if(!isFirst) {
query.append(" AND ");
}
query.append("EXTRACT(YEAR FROM age(NOW(), licenseYear)) <= ?");
query.append("\n");
}
int res = 0;
try {
PreparedStatement preparedStatement = connection.prepareStatement(query.toString());
int index = 1;
if(filter.expirienceFrom != null) {
preparedStatement.setInt(index++, filter.expirienceFrom);
}
if(filter.expirienceTo != null) {
preparedStatement.setInt(index, filter.expirienceTo);
}
ResultSet resultSet = preparedStatement.executeQuery();
if (resultSet.next()) {
res = resultSet.getInt(1);
}
resultSet.close();
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
return res;
}
@Override
public List<Driver> getFilteredList(DriverSearch filter) {
if(filter.expirienceTo == null && filter.expirienceFrom == null) return readAll();
StringBuilder query = new StringBuilder(readFiltered);
boolean isFirst = true;
if(filter.expirienceFrom != null) {
if(!isFirst) {
query.append(" AND ");
}
query.append("EXTRACT(YEAR FROM age(NOW(), licenseYear)) >= ?");
query.append("\n");
isFirst = false;
}
if(filter.expirienceTo != null) {
if(!isFirst) {
query.append(" AND ");
}
query.append("EXTRACT(YEAR FROM age(NOW(), licenseYear)) <= ?");
query.append("\n");
}
ArrayList<Driver> list = new ArrayList<>();
try {
PreparedStatement preparedStatement = connection.prepareStatement(query.toString());
int index = 1;
if(filter.expirienceFrom != null) {
preparedStatement.setInt(index++, filter.expirienceFrom);
}
if(filter.expirienceTo != null) {
preparedStatement.setInt(index, filter.expirienceTo);
}
ResultSet resultSet = preparedStatement.executeQuery();
list.ensureCapacity(resultSet.getFetchSize());
while (resultSet.next()) {
Long id = resultSet.getLong(1);
String name = resultSet.getString(2);
Date licenseYear = resultSet.getDate(3);
Long carId = resultSet.getLong(4);
Car c = carStorage.getElementById(carId);
list.add(new Driver(id, name, licenseYear, c));
}
resultSet.close();
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
return list;
}
@Override
public List<Driver> getFilteredPage(DriverSearch filter, int size, int page) {
if(filter.expirienceTo == null && filter.expirienceFrom == null) return readPage(size, page);
StringBuilder query = new StringBuilder(readFiltered);
boolean isFirst = true;
if(filter.expirienceFrom != null) {
if(!isFirst) {
query.append(" AND ");
}
query.append("EXTRACT(YEAR FROM age(NOW(), licenseYear)) >= ?");
query.append("\n");
isFirst = false;
}
if(filter.expirienceTo != null) {
if(!isFirst) {
query.append(" AND ");
}
query.append("EXTRACT(YEAR FROM age(NOW(), licenseYear)) <= ?");
query.append("\n");
}
query.append("ORDER BY id");
query.append("\n");
query.append("OFFSET ?");
query.append("LIMIT ?");
ArrayList<Driver> list = new ArrayList<>();
try {
PreparedStatement preparedStatement = connection.prepareStatement(query.toString());
int index = 1;
if(filter.expirienceFrom != null) {
preparedStatement.setInt(index++, filter.expirienceFrom);
}
if(filter.expirienceTo != null) {
preparedStatement.setInt(index++, filter.expirienceTo);
}
preparedStatement.setInt(index++, page - 1);
preparedStatement.setInt(index, size);
ResultSet resultSet = preparedStatement.executeQuery();
list.ensureCapacity(resultSet.getFetchSize());
while (resultSet.next()) {
Long id = resultSet.getLong(1);
String name = resultSet.getString(2);
Date licenseYear = resultSet.getDate(3);
Long carId = resultSet.getLong(4);
Car c = carStorage.getElementById(carId);
list.add(new Driver(id, name, licenseYear, c));
}
resultSet.close();
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
return list;
}
@Override
public Driver getElementById(Long driverId) {
Driver res = null;
try {
PreparedStatement preparedStatement = connection.prepareStatement(readRecordById);
preparedStatement.setLong(1, driverId);
ResultSet resultSet = preparedStatement.executeQuery();
resultSet.next();
Long id = resultSet.getLong(1);
String name = resultSet.getString(2);
Date licenseYear = resultSet.getDate(3);
Long carId = resultSet.getLong(4);
Car c = carStorage.getElementById(carId);
res = new Driver(id, name, licenseYear, c);
resultSet.close();
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
return res;
}
@Override
public void insert(Driver driver) {
try {
PreparedStatement preparedStatement = connection.prepareStatement(insertRecord);
preparedStatement.setString(1, driver.getName());
preparedStatement.setDate(2, new java.sql.Date(driver.getLicenseYear().getTime()));
preparedStatement.setLong(3, driver.getCar().getId());
preparedStatement.executeUpdate();
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public void delete(Long driverId) {
try {
PreparedStatement preparedStatement = connection.prepareStatement(deleteRecord);
preparedStatement.setLong(1, driverId);
preparedStatement.executeUpdate();
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}

View File

@ -0,0 +1,255 @@
package org.dbms.storageImpl;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;
import org.dbms.models.*;
import org.dbms.storageContracts.IClientStorage;
import org.dbms.storageContracts.IDriverStorage;
import org.dbms.storageContracts.IOrderStorage;
import org.springframework.stereotype.Component;
@Component
public class OrderStorage implements IOrderStorage {
private final static String readRecords;
private final static String readPage;
private final static String readCount;
private final static String readRecordById;
private final static String insertRecord;
private final static String deleteRecord;
private final static String readRecordByClientId;
static
{
readRecords = "SELECT id, _from, _to, price, deliveryWeight, driverId, clientId FROM orders";
readCount = "SELECT COUNT(*) FROM orders WHERE clientId = ?";
readPage = new StringBuilder()
.append("SELECT id, _from, _to, price, deliveryWeight, driverId, clientId FROM orders")
.append("\n")
.append("WHERE clientId = ?")
.append("\n")
.append("ORDER BY id")
.append("\n")
.append("OFFSET ?")
.append("\n")
.append("LIMIT ?").toString();
readRecordById = new StringBuilder()
.append("SELECT id, _from, _to, price, deliveryWeight, driverId, clientId FROM orders")
.append("\n")
.append("WHERE id = ?").toString();
readRecordByClientId = new StringBuilder()
.append("SELECT id, _from, _to, price, deliveryWeight, driverId, clientId FROM orders")
.append("\n")
.append("WHERE clientId = ?").toString();
insertRecord = "INSERT INTO orders (_from, _to, price, deliveryWeight, driverId, clientId) VALUES(?, ?, ?, ?, ?, ?)";
deleteRecord = "DELETE FROM orders WHERE id = ?";
}
private final Connection connection;
private final IDriverStorage driverStorage;
private final IClientStorage clientStorage;
public OrderStorage(Connection connection, IDriverStorage driverStorage, IClientStorage clientStorage) {
this.connection = connection;
this.driverStorage = driverStorage;
this.clientStorage = clientStorage;
}
@Override
public List<Order> readAll() {
ArrayList<Order> list = new ArrayList<>();
try {
PreparedStatement preparedStatement = connection.prepareStatement(readRecords);
ResultSet resultSet = preparedStatement.executeQuery();
list.ensureCapacity(resultSet.getFetchSize());
while (resultSet.next()) {
Long id = resultSet.getLong(1);
String from = resultSet.getString(2);
String to = resultSet.getString(3);
Double price = resultSet.getDouble(4);
int deliveryWeight = resultSet.getInt(5);
Long driverId = resultSet.getLong(5);
Long clientId = resultSet.getLong(5);
Driver d = driverStorage.getElementById(driverId);
Client c = clientStorage.getElementById(clientId);
list.add(new Order(id, from, to, price, deliveryWeight, d, c));
}
resultSet.close();
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
return list;
}
@Override
public List<Order> readPageByClientId(int size, int page, Long cid) {
ArrayList<Order> list = new ArrayList<>();
try {
PreparedStatement preparedStatement = connection.prepareStatement(readPage);
preparedStatement.setLong(1, cid);
preparedStatement.setInt(2, page - 1);
preparedStatement.setInt(3, size);
ResultSet resultSet = preparedStatement.executeQuery();
list.ensureCapacity(resultSet.getFetchSize());
while (resultSet.next()) {
Long id = resultSet.getLong(1);
String from = resultSet.getString(2);
String to = resultSet.getString(3);
Double price = resultSet.getDouble(4);
int deliveryWeight = resultSet.getInt(5);
Long driverId = resultSet.getLong(6);
Long clientId = resultSet.getLong(7);
Driver d = driverStorage.getElementById(driverId);
Client c = clientStorage.getElementById(clientId);
list.add(new Order(id, from, to, price, deliveryWeight, d, c));
}
resultSet.close();
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
return list;
}
@Override
public int getCountByClientId(Long clientId) {
int res = 0;
try {
PreparedStatement preparedStatement = connection.prepareStatement(readCount);
preparedStatement.setLong(1, clientId);
ResultSet resultSet = preparedStatement.executeQuery();
if (resultSet.next()) {
res = resultSet.getInt(1);
}
resultSet.close();
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
return res;
}
@Override
public Order getElementById(Long orderId) {
Order res = null;
try {
PreparedStatement preparedStatement = connection.prepareStatement(readRecordById);
preparedStatement.setLong(1, orderId);
ResultSet resultSet = preparedStatement.executeQuery();
resultSet.next();
Long id = resultSet.getLong(1);
String from = resultSet.getString(2);
String to = resultSet.getString(3);
Double price = resultSet.getDouble(4);
int deliveryWeight = resultSet.getInt(5);
Long driverId = resultSet.getLong(5);
Long clientId = resultSet.getLong(5);
Driver d = driverStorage.getElementById(driverId);
Client c = clientStorage.getElementById(clientId);
res = new Order(id, from, to, price, deliveryWeight, d, c);
resultSet.close();
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
return res;
}
@Override
public List<Order> getOrdersByClientId(Long cid) {
ArrayList<Order> list = new ArrayList<>();
try {
PreparedStatement preparedStatement = connection.prepareStatement(readRecordByClientId);
preparedStatement.setLong(1, cid);
ResultSet resultSet = preparedStatement.executeQuery();
list.ensureCapacity(resultSet.getFetchSize());
while (resultSet.next()) {
Long id = resultSet.getLong(1);
String from = resultSet.getString(2);
String to = resultSet.getString(3);
Double price = resultSet.getDouble(4);
int deliveryWeight = resultSet.getInt(5);
Long driverId = resultSet.getLong(6);
Long clientId = resultSet.getLong(7);
Driver d = driverStorage.getElementById(driverId);
Client c = clientStorage.getElementById(clientId);
list.add(new Order(id, from, to, price, deliveryWeight, d, c));
}
resultSet.close();
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
return list;
}
@Override
public void insert(Order order) {
try {
PreparedStatement preparedStatement = connection.prepareStatement(insertRecord);
preparedStatement.setString(1, order.getFrom());
preparedStatement.setString(2, order.getTo());
preparedStatement.setDouble(3, order.getPrice());
preparedStatement.setInt(4, order.getDeliveryWeight());
preparedStatement.setLong(5, order.getDriver().getId());
preparedStatement.setLong(6, order.getClient().getId());
preparedStatement.executeUpdate();
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public void delete(Long orderId) {
try {
PreparedStatement preparedStatement = connection.prepareStatement(deleteRecord);
preparedStatement.setLong(1, orderId);
preparedStatement.executeUpdate();
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}

View File

@ -0,0 +1,119 @@
package org.dbms.storageImpl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;
import org.dbms.models.Client;
import org.dbms.models.Parking;
import org.dbms.storageContracts.IParkingStorage;
import org.springframework.stereotype.Component;
@Component
public class ParkingStorage implements IParkingStorage {
private final static String readRecords;
private final static String readRecordById;
private final static String insertRecord;
private final static String deleteRecord;
static
{
readRecords = "SELECT id, address, capacity FROM parkings";
readRecordById = new StringBuilder()
.append("SELECT id, address, capacity FROM parkings")
.append("\n")
.append("WHERE id = ?").toString();
insertRecord = "INSERT INTO parkings (address, capacity) VALUES(?, ?)";
deleteRecord = "DELETE FROM parkings WHERE id = ?";
}
private final Connection connection;
public ParkingStorage(Connection connection) {
this.connection = connection;
}
@Override
public List<Parking> readAll() {
ArrayList<Parking> list = new ArrayList<>();
try {
PreparedStatement preparedStatement = connection.prepareStatement(readRecords);
ResultSet resultSet = preparedStatement.executeQuery();
list.ensureCapacity(resultSet.getFetchSize());
while (resultSet.next()) {
Long id = resultSet.getLong(1);
String address = resultSet.getString(2);
int capacity = resultSet.getInt(3);
list.add(new Parking(id, address, capacity));
}
resultSet.close();
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
return list;
}
@Override
public Parking getElementById(Long parkingId) {
Parking res = null;
try {
PreparedStatement preparedStatement = connection.prepareStatement(readRecordById);
preparedStatement.setLong(1, parkingId);
ResultSet resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
Long id = resultSet.getLong(1);
String address = resultSet.getString(2);
int capacity = resultSet.getInt(3);
res = new Parking(id, address, capacity);
}
resultSet.close();
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
return res;
}
@Override
public void insert(Parking parking) {
try {
PreparedStatement preparedStatement = connection.prepareStatement(insertRecord);
preparedStatement.setString(1, parking.getAddress());
preparedStatement.setInt(2, parking.getCapacity());
preparedStatement.executeUpdate();
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public void delete(Long parkingId) {
try {
PreparedStatement preparedStatement = connection.prepareStatement(deleteRecord);
preparedStatement.setLong(1, parkingId);
preparedStatement.executeUpdate();
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}