Added connection to database
- displayed products and suppliers to table but still incomplete
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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) {
|
||||
|
||||
|
||||
@@ -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) {
|
||||
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
97
src/main/java/org/example/petshopdesktop/models/Product.java
Normal file
97
src/main/java/org/example/petshopdesktop/models/Product.java
Normal 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);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user