Added connection to database

- displayed products and suppliers to table but still incomplete
This commit is contained in:
Alex
2026-01-31 17:52:30 -07:00
parent d4fe78b65f
commit 47fe06a5c3
10 changed files with 594 additions and 34 deletions

View File

@@ -1,85 +1,236 @@
DROP DATABASE IF EXISTS Petstoredb;
CREATE DATABASE Petstoredb;
USE Petstoredb;
INSERT INTO store_location (store_name, address, phone, email)
-- Create Tables
CREATE TABLE storeLocation (
storeId INT AUTO_INCREMENT PRIMARY KEY,
storeName VARCHAR(100) NOT NULL,
address VARCHAR(255),
phone VARCHAR(20),
email VARCHAR(100)
);
CREATE TABLE employee (
employeeId INT AUTO_INCREMENT PRIMARY KEY,
firstName VARCHAR(50) NOT NULL,
lastName VARCHAR(50) NOT NULL,
email VARCHAR(100),
phone VARCHAR(20),
role VARCHAR(50),
isActive BOOLEAN DEFAULT TRUE
);
CREATE TABLE employeeStore (
employeeId INT,
storeId INT,
PRIMARY KEY (employeeId, storeId),
FOREIGN KEY (employeeId) REFERENCES employee(employeeId),
FOREIGN KEY (storeId) REFERENCES storeLocation(storeId)
);
CREATE TABLE customer (
customerId INT AUTO_INCREMENT PRIMARY KEY,
firstName VARCHAR(50) NOT NULL,
lastName VARCHAR(50) NOT NULL,
email VARCHAR(100),
phone VARCHAR(20)
);
CREATE TABLE pet (
petId INT AUTO_INCREMENT PRIMARY KEY,
petName VARCHAR(50),
petSpecies VARCHAR(50),
petBreed VARCHAR(50),
petAge INT,
petStatus VARCHAR(20),
petPrice DECIMAL(10, 2)
);
CREATE TABLE adoption (
adoptionId INT AUTO_INCREMENT PRIMARY KEY,
petId INT,
customerId INT,
adoptionDate DATE,
adoptionStatus VARCHAR(20),
FOREIGN KEY (petId) REFERENCES pet(petId),
FOREIGN KEY (customerId) REFERENCES customer(customerId)
);
CREATE TABLE supplier (
supId INT AUTO_INCREMENT PRIMARY KEY,
supCompany VARCHAR(100) NOT NULL,
supContactFirstName VARCHAR(50),
supContactLastName VARCHAR(50),
supEmail VARCHAR(100),
supPhone VARCHAR(20)
);
CREATE TABLE category (
categoryId INT AUTO_INCREMENT PRIMARY KEY,
categoryName VARCHAR(100) NOT NULL,
categoryType VARCHAR(50)
);
CREATE TABLE product (
prodId INT AUTO_INCREMENT PRIMARY KEY,
prodName VARCHAR(100) NOT NULL,
prodSku VARCHAR(50) UNIQUE,
prodPrice DECIMAL(10, 2),
categoryId INT,
prodDesc TEXT,
FOREIGN KEY (categoryId) REFERENCES category(categoryId)
);
CREATE TABLE productSupplier (
supId INT,
prodId INT,
PRIMARY KEY (supId, prodId),
FOREIGN KEY (supId) REFERENCES supplier(supId),
FOREIGN KEY (prodId) REFERENCES product(prodId)
);
CREATE TABLE inventory (
inventoryId INT AUTO_INCREMENT PRIMARY KEY,
prodId INT,
quantity INT DEFAULT 0,
FOREIGN KEY (prodId) REFERENCES product(prodId)
);
CREATE TABLE service (
serviceId INT AUTO_INCREMENT PRIMARY KEY,
serviceName VARCHAR(100) NOT NULL,
serviceDesc TEXT,
serviceDuration INT,
servicePrice DECIMAL(10, 2)
);
CREATE TABLE appointment (
appointmentId INT AUTO_INCREMENT PRIMARY KEY,
serviceId INT,
customerId INT,
appointmentDate DATE,
appointmentTime TIME,
appointmentStatus VARCHAR(20),
FOREIGN KEY (serviceId) REFERENCES service(serviceId),
FOREIGN KEY (customerId) REFERENCES customer(customerId)
);
CREATE TABLE appointmentPet (
appointmentId INT,
petId INT,
PRIMARY KEY (appointmentId, petId),
FOREIGN KEY (appointmentId) REFERENCES appointment(appointmentId),
FOREIGN KEY (petId) REFERENCES pet(petId)
);
CREATE TABLE sale (
saleId INT AUTO_INCREMENT PRIMARY KEY,
saleDate DATETIME,
totalAmount DECIMAL(10, 2),
paymentMethod VARCHAR(50),
employeeId INT,
storeId INT,
FOREIGN KEY (employeeId) REFERENCES employee(employeeId),
FOREIGN KEY (storeId) REFERENCES storeLocation(storeId)
);
CREATE TABLE saleItem (
saleItemId INT AUTO_INCREMENT PRIMARY KEY,
saleId INT,
prodId INT,
quantity INT,
unitPrice DECIMAL(10, 2),
FOREIGN KEY (saleId) REFERENCES sale(saleId),
FOREIGN KEY (prodId) REFERENCES product(prodId)
);
CREATE TABLE activityLog (
logId INT AUTO_INCREMENT PRIMARY KEY,
employeeId INT,
activity TEXT,
logTimestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (employeeId) REFERENCES employee(employeeId)
);
-- Insert Sample Data
INSERT INTO storeLocation (storeName, address, phone, email)
VALUES
('Downtown Branch', '123 Main St', '123-456-7890', 'downtown@petshop.com'),
('North Branch', '456 North Ave', '987-654-3210', 'north@petshop.com');
INSERT INTO employee (first_name, last_name, email, phone, role, is_active)
INSERT INTO employee (firstName, lastName, email, phone, role, isActive)
VALUES
('John', 'Doe', 'john@petshop.com', '111-222-3333', 'Manager', TRUE),
('Sara', 'Smith', 'sara@petshop.com', '444-555-6666', 'Staff', TRUE);
INSERT INTO employee_store (employee_id, store_id)
INSERT INTO employeeStore (employeeId, storeId)
VALUES
(1, 1),
(2, 1),
(2, 2);
INSERT INTO customer (first_name, last_name, email, phone)
INSERT INTO customer (firstName, lastName, email, phone)
VALUES
('Alex', 'Brown', 'alex@gmail.com', '777-888-9999'),
('Emily', 'Clark', 'emily@gmail.com', '666-555-4444');
INSERT INTO pet (pet_name, pet_species, pet_breed, pet_age, pet_status, pet_price)
INSERT INTO pet (petName, petSpecies, petBreed, petAge, petStatus, petPrice)
VALUES
('Buddy', 'Dog', 'Labrador', 2, 'Available', 500.00),
('Milo', 'Cat', 'Persian', 1, 'Available', 300.00);
INSERT INTO adoption (pet_id, customer_id, adoption_date, adoption_status)
INSERT INTO adoption (petId, customerId, adoptionDate, adoptionStatus)
VALUES
(1, 1, '2026-01-15', 'Completed');
INSERT INTO supplier (sup_company, sup_contact_first_name, sup_contact_last_name, sup_email, sup_phone)
INSERT INTO supplier (supCompany, supContactFirstName, supContactLastName, supEmail, supPhone)
VALUES
('PetFood Inc', 'Robert', 'King', 'contact@petfood.com', '888-111-2222');
INSERT INTO category (category_name, category_type)
INSERT INTO category (categoryName, categoryType)
VALUES
('Dog Food', 'Product'),
('Cat Toys', 'Product');
INSERT INTO product (prod_name, prod_sku, prod_price, category_id, prod_desc)
INSERT INTO product (prodName, prodSku, prodPrice, categoryId, prodDesc)
VALUES
('Premium Dog Food', 'DF001', 50.00, 1, 'High quality dog food'),
('Cat Toy Ball', 'CT001', 10.00, 2, 'Colorful toy for cats');
INSERT INTO product_supplier (sup_id, prod_id)
INSERT INTO productSupplier (supId, prodId)
VALUES
(1, 1),
(1, 2);
INSERT INTO inventory (prod_id, quantity)
INSERT INTO inventory (prodId, quantity)
VALUES
(1, 100),
(2, 200);
INSERT INTO service (service_name, service_desc, service_duration, service_price)
INSERT INTO service (serviceName, serviceDesc, serviceDuration, servicePrice)
VALUES
('Pet Grooming', 'Full grooming service', 60, 40.00);
INSERT INTO appointment (service_id, customer_id, appointment_date, appointment_time, appointment_status)
INSERT INTO appointment (serviceId, customerId, appointmentDate, appointmentTime, appointmentStatus)
VALUES
(1, 2, '2026-02-01', '10:30:00', 'Booked');
INSERT INTO appointment_pet (appointment_id, pet_id)
INSERT INTO appointmentPet (appointmentId, petId)
VALUES
(1, 2);
INSERT INTO sale (sale_date, total_amount, payment_method, employee_id, store_id)
INSERT INTO sale (saleDate, totalAmount, paymentMethod, employeeId, storeId)
VALUES
(NOW(), 60.00, 'Card', 2, 1);
INSERT INTO sale_item (sale_id, prod_id, quantity, unit_price)
INSERT INTO saleItem (saleId, prodId, quantity, unitPrice)
VALUES
(1, 2, 2, 10.00);
INSERT INTO activity_log (employee_id, activity)
INSERT INTO activityLog (employeeId, activity)
VALUES
(1, 'Created new sale'),
(2, 'Booked appointment');

View File

@@ -38,6 +38,12 @@
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>9.3.0</version>
</dependency>
</dependencies>
<build>

View File

@@ -1,8 +1,9 @@
module org.example.petshopdesktop {
requires javafx.controls;
requires javafx.fxml;
requires java.sql;
opens org.example.petshopdesktop.models to javafx.base;
opens org.example.petshopdesktop to javafx.fxml;
exports org.example.petshopdesktop;
exports org.example.petshopdesktop.controllers;

View File

@@ -1,11 +1,18 @@
package org.example.petshopdesktop.controllers;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import org.example.petshopdesktop.database.ProductDB;
import org.example.petshopdesktop.models.Product;
import java.sql.SQLException;
public class ProductController {
@@ -19,29 +26,71 @@ public class ProductController {
private Button btnEdit;
@FXML
private TableColumn<?, ?> colProductCategory;
private TableColumn<Product, Integer> colProductCategory;
@FXML
private TableColumn<?, ?> colProductDesc;
private TableColumn<Product, String> colProductDesc;
@FXML
private TableColumn<?, ?> colProductId;
private TableColumn<Product, Integer> colProductId;
@FXML
private TableColumn<?, ?> colProductName;
private TableColumn<Product, String> colProductName;
@FXML
private TableColumn<?, ?> colProductPrice;
private TableColumn<Product, Double> colProductPrice;
@FXML
private TableColumn<?, ?> colProductSKU;
private TableColumn<Product, String> colProductSKU;
@FXML
private TableView<?> tvProducts;
private TableView<Product> tvProducts;
@FXML
private TextField txtSearch;
//data declaration
private ObservableList<Product> data = FXCollections.observableArrayList(); //empty
/**
* Set up the table view for fees and display it when starting up
*/
@FXML
void initialize() {
//set up table columns
colProductId.setCellValueFactory(new PropertyValueFactory<Product,Integer>("prodId"));
colProductName.setCellValueFactory(new PropertyValueFactory<Product,String>("prodName"));
colProductSKU.setCellValueFactory(new PropertyValueFactory<Product,String>("prodSku"));
colProductPrice.setCellValueFactory(new PropertyValueFactory<Product,Double>("prodPrice"));
colProductCategory.setCellValueFactory(new PropertyValueFactory<Product,Integer>("categoryId"));
colProductDesc.setCellValueFactory(new PropertyValueFactory<Product,String>("prodDesc"));
displayProducts();
//TODO MUST DISPLAY CATEGORY NAME INSTEAD OF ID
}
/**
* Display the products to table view
*/
private void displayProducts(){
//Erase old content
data.clear();
//get Products from database
try{
data = ProductDB.getProducts();
} catch (SQLException e) {
throw new RuntimeException(e);
}
//put data in the table
tvProducts.setItems(data);
}
@FXML
void btnAddClicked(ActionEvent event) {

View File

@@ -1,11 +1,18 @@
package org.example.petshopdesktop.controllers;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import org.example.petshopdesktop.database.SupplierDB;
import org.example.petshopdesktop.models.Supplier;
import java.sql.SQLException;
public class SupplierController {
@@ -19,26 +26,54 @@ public class SupplierController {
private Button btnEdit;
@FXML
private TableColumn<?, ?> colContactPerson;
private TableColumn<Supplier, String> colContactPerson;
@FXML
private TableColumn<?, ?> colSupplierEmail;
private TableColumn<Supplier, String> colSupplierEmail;
@FXML
private TableColumn<?, ?> colSupplierId;
private TableColumn<Supplier, Integer> colSupplierId;
@FXML
private TableColumn<?, ?> colSupplierName;
private TableColumn<Supplier, String> colSupplierName;
@FXML
private TableColumn<?, ?> colSupplierPhone;
private TableColumn<Supplier, String> colSupplierPhone;
@FXML
private TableView<?> tvSuppliers;
private TableView<Supplier> tvSuppliers;
@FXML
private TextField txtSearch;
private ObservableList<Supplier> data = FXCollections.observableArrayList();
@FXML
void initialize(){
colSupplierId.setCellValueFactory(new PropertyValueFactory<Supplier, Integer>("supId"));
colSupplierName.setCellValueFactory(new PropertyValueFactory<Supplier, String>("supCompany"));
colContactPerson.setCellValueFactory(new PropertyValueFactory<Supplier, String>("supContactFirstName"));
colSupplierEmail.setCellValueFactory(new PropertyValueFactory<Supplier, String>("supEmail"));
colSupplierPhone.setCellValueFactory(new PropertyValueFactory<Supplier, String>("supPhone"));
displaySupplier();
//TODO MUST DISPLAY FULL NAME INSTEAD OF FIRST NAME ALSO ADD COMMENTS LATER
}
private void displaySupplier(){
data.clear();
try{
data = SupplierDB.getSuppliers();
} catch (SQLException e) {
throw new RuntimeException(e);
}
tvSuppliers.setItems(data);
}
@FXML
void btnAddClicked(ActionEvent event) {

View File

@@ -0,0 +1,45 @@
package org.example.petshopdesktop.database;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
public class ConnectionDB {
/**
* Method to try and connect to the database sing cnnection.properties located in the
* root of C drive
* @return Connection to the database
*/
public static Connection getConnection(){
String url = "";
String user = "";
String password = "";
try{
//Read connection.properties file
FileInputStream fis = new FileInputStream("c:\\connectionpetstore.properties"); //location of connection can be changed here
Properties prop = new Properties();
prop.load(fis);
url = prop.getProperty("url");
user = prop.getProperty("user");
password = prop.getProperty("password");
}
catch(IOException e){
throw new RuntimeException("Problem with reading connection info: "+e.getMessage());
}
Connection conn = null;
try{
//try to get connection with the data taken from connection.properties
conn = DriverManager.getConnection(url,user,password);
return conn;
}
catch (SQLException e) {
throw new RuntimeException("Problem with database connection: "+e.getMessage());
}
}
}

View File

@@ -0,0 +1,42 @@
package org.example.petshopdesktop.database;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import org.example.petshopdesktop.models.Product;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;
/**
* A class containing all the methods relating to CRUD on Products table
*/
public class ProductDB {
public static ObservableList<Product> getProducts() throws SQLException{
//Connect to the database
ObservableList<Product> products = FXCollections.observableArrayList();
Connection conn = ConnectionDB.getConnection();
//Execute Query
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM product");
//While there is still data add products to the list
while(rs.next()){
Product product = new Product(
rs.getInt(1),
rs.getString(2),
rs.getString(3),
rs.getDouble(4),
rs.getInt(5),
rs.getString(6));
products.add(product);
}
//close connection and return products
conn.close();
return products;
}
}

View File

@@ -0,0 +1,38 @@
package org.example.petshopdesktop.database;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import org.example.petshopdesktop.models.Product;
import org.example.petshopdesktop.models.Supplier;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class SupplierDB {
public static ObservableList<Supplier> getSuppliers() throws SQLException {
//Connect to the database
ObservableList<Supplier> suppliers = FXCollections.observableArrayList();
Connection conn = ConnectionDB.getConnection();
//Execute Query
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM supplier");
//While there is still data add products to the list
while(rs.next()){
Supplier supplier = new Supplier(
rs.getInt(1),
rs.getString(2),
rs.getString(3),
rs.getString(4),
rs.getString(5),
rs.getString(6));
suppliers.add(supplier);
}
conn.close();
return suppliers;
}
}

View File

@@ -0,0 +1,97 @@
package org.example.petshopdesktop.models;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
public class Product {
public SimpleIntegerProperty prodId;
public SimpleStringProperty prodName;
public SimpleStringProperty prodSku;
public SimpleDoubleProperty prodPrice;
public SimpleIntegerProperty categoryId;
public SimpleStringProperty prodDesc;
//constructor
public Product(int prodId, String prodName, String prodSku, double prodPrice, int categoryId, String prodDesc) {
this.prodId = new SimpleIntegerProperty(prodId);
this.prodName = new SimpleStringProperty(prodName);
this.prodSku = new SimpleStringProperty(prodSku);
this.prodPrice = new SimpleDoubleProperty(prodPrice);
this.categoryId = new SimpleIntegerProperty(categoryId);
this.prodDesc = new SimpleStringProperty(prodDesc);
}
//getter and setters
public int getProdId() {
return prodId.get();
}
public SimpleIntegerProperty prodIdProperty() {
return prodId;
}
public void setProdId(int prodId) {
this.prodId.set(prodId);
}
public String getProdName() {
return prodName.get();
}
public SimpleStringProperty prodNameProperty() {
return prodName;
}
public void setProdName(String prodName) {
this.prodName.set(prodName);
}
public String getProdSku() {
return prodSku.get();
}
public SimpleStringProperty prodSkuProperty() {
return prodSku;
}
public void setProdSku(String prodSku) {
this.prodSku.set(prodSku);
}
public double getProdPrice() {
return prodPrice.get();
}
public SimpleDoubleProperty prodPriceProperty() {
return prodPrice;
}
public void setProdPrice(double prodPrice) {
this.prodPrice.set(prodPrice);
}
public int getCategoryId() {
return categoryId.get();
}
public SimpleIntegerProperty categoryIdProperty() {
return categoryId;
}
public void setCategoryId(int categoryId) {
this.categoryId.set(categoryId);
}
public String getProdDesc() {
return prodDesc.get();
}
public SimpleStringProperty prodDescProperty() {
return prodDesc;
}
public void setProdDesc(String prodDesc) {
this.prodDesc.set(prodDesc);
}
}

View File

@@ -0,0 +1,96 @@
package org.example.petshopdesktop.models;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
public class Supplier {
SimpleIntegerProperty supId;
SimpleStringProperty supCompany;
SimpleStringProperty supContactFirstName;
SimpleStringProperty supContactLastName;
SimpleStringProperty supEmail;
SimpleStringProperty supPhone;
//constructor
public Supplier(int supId, String supCompany, String supContactFirstName, String supContactLastName, String supEmail, String supPhone) {
this.supId = new SimpleIntegerProperty(supId);
this.supCompany = new SimpleStringProperty(supCompany);
this.supContactFirstName = new SimpleStringProperty(supContactFirstName);
this.supContactLastName = new SimpleStringProperty(supContactLastName);
this.supEmail = new SimpleStringProperty(supEmail);
this.supPhone = new SimpleStringProperty(supPhone);
}
//getter and setter
public int getSupId() {
return supId.get();
}
public SimpleIntegerProperty supIdProperty() {
return supId;
}
public void setSupId(int supId) {
this.supId.set(supId);
}
public String getSupCompany() {
return supCompany.get();
}
public SimpleStringProperty supCompanyProperty() {
return supCompany;
}
public void setSupCompany(String supCompany) {
this.supCompany.set(supCompany);
}
public String getSupContactFirstName() {
return supContactFirstName.get();
}
public SimpleStringProperty supContactFirstNameProperty() {
return supContactFirstName;
}
public void setSupContactFirstName(String supContactFirstName) {
this.supContactFirstName.set(supContactFirstName);
}
public String getSupContactLastName() {
return supContactLastName.get();
}
public SimpleStringProperty supContactLastNameProperty() {
return supContactLastName;
}
public void setSupContactLastName(String supContactLastName) {
this.supContactLastName.set(supContactLastName);
}
public String getSupEmail() {
return supEmail.get();
}
public SimpleStringProperty supEmailProperty() {
return supEmail;
}
public void setSupEmail(String supEmail) {
this.supEmail.set(supEmail);
}
public String getSupPhone() {
return supPhone.get();
}
public SimpleStringProperty supPhoneProperty() {
return supPhone;
}
public void setSupPhone(String supPhone) {
this.supPhone.set(supPhone);
}
}