From 348bc67a6ed9ef06afcf19d8698c2700f57e5f14 Mon Sep 17 00:00:00 2001
From: Luigi Mota <94766364+Loogibot@users.noreply.github.com>
Date: Thu, 20 Feb 2025 17:31:43 +0000
Subject: [PATCH 01/13] from jotterbot
---
my-app/.mvn/jvm.config | 0
my-app/.mvn/maven.config | 0
my-app/pom.xml | 90 +++++++++++++++++++
.../src/main/java/com/loogibot/lil/App.java | 10 +++
.../test/java/com/loogibot/lil/AppTest.java | 19 ++++
5 files changed, 119 insertions(+)
create mode 100644 my-app/.mvn/jvm.config
create mode 100644 my-app/.mvn/maven.config
create mode 100644 my-app/pom.xml
create mode 100644 my-app/src/main/java/com/loogibot/lil/App.java
create mode 100644 my-app/src/test/java/com/loogibot/lil/AppTest.java
diff --git a/my-app/.mvn/jvm.config b/my-app/.mvn/jvm.config
new file mode 100644
index 0000000..e69de29
diff --git a/my-app/.mvn/maven.config b/my-app/.mvn/maven.config
new file mode 100644
index 0000000..e69de29
diff --git a/my-app/pom.xml b/my-app/pom.xml
new file mode 100644
index 0000000..7b03a54
--- /dev/null
+++ b/my-app/pom.xml
@@ -0,0 +1,90 @@
+
+
+ 4.0.0
+
+ com.loogibot.lil
+ my-app
+ 1.0-SNAPSHOT
+
+ my-app
+
+ http://www.example.com
+
+
+ UTF-8
+ 17
+
+
+
+
+
+ org.junit
+ junit-bom
+ 5.11.0
+ pom
+ import
+
+
+
+
+
+
+ org.junit.jupiter
+ junit-jupiter-api
+ test
+
+
+
+ org.junit.jupiter
+ junit-jupiter-params
+ test
+
+
+
+
+
+
+
+
+ maven-clean-plugin
+ 3.4.0
+
+
+
+ maven-resources-plugin
+ 3.3.1
+
+
+ maven-compiler-plugin
+ 3.13.0
+
+
+ maven-surefire-plugin
+ 3.3.0
+
+
+ maven-jar-plugin
+ 3.4.2
+
+
+ maven-install-plugin
+ 3.1.2
+
+
+ maven-deploy-plugin
+ 3.1.2
+
+
+
+ maven-site-plugin
+ 3.12.1
+
+
+ maven-project-info-reports-plugin
+ 3.6.1
+
+
+
+
+
diff --git a/my-app/src/main/java/com/loogibot/lil/App.java b/my-app/src/main/java/com/loogibot/lil/App.java
new file mode 100644
index 0000000..b9b7a1e
--- /dev/null
+++ b/my-app/src/main/java/com/loogibot/lil/App.java
@@ -0,0 +1,10 @@
+package com.loogibot.lil;
+
+/**
+ * Hello world!
+ */
+public class App {
+ public static void main(String[] args) {
+ System.out.println("Hello World!");
+ }
+}
diff --git a/my-app/src/test/java/com/loogibot/lil/AppTest.java b/my-app/src/test/java/com/loogibot/lil/AppTest.java
new file mode 100644
index 0000000..2e8ce86
--- /dev/null
+++ b/my-app/src/test/java/com/loogibot/lil/AppTest.java
@@ -0,0 +1,19 @@
+package com.loogibot.lil;
+
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import org.junit.jupiter.api.Test;
+
+/**
+ * Unit test for simple App.
+ */
+public class AppTest {
+
+ /**
+ * Rigorous Test :-)
+ */
+ @Test
+ public void shouldAnswerWithTrue() {
+ assertTrue(true);
+ }
+}
From bb22c4c63e7ea756da0c491974362c329e480543 Mon Sep 17 00:00:00 2001
From: Luigi Mota
Date: Thu, 20 Feb 2025 18:54:00 +0000
Subject: [PATCH 02/13] set up the databaseutils with exception handling
---
my-app/pom.xml | 5 +++
.../loogibot/lil/data/util/DatabaseUtils.java | 36 +++++++++++++++++++
2 files changed, 41 insertions(+)
create mode 100644 my-app/src/main/java/com/loogibot/lil/data/util/DatabaseUtils.java
diff --git a/my-app/pom.xml b/my-app/pom.xml
index 7b03a54..a9f6c33 100644
--- a/my-app/pom.xml
+++ b/my-app/pom.xml
@@ -34,6 +34,11 @@
junit-jupiter-api
test
+
+ org.postgresql
+ postgresql
+ 42.7.4
+
org.junit.jupiter
diff --git a/my-app/src/main/java/com/loogibot/lil/data/util/DatabaseUtils.java b/my-app/src/main/java/com/loogibot/lil/data/util/DatabaseUtils.java
new file mode 100644
index 0000000..86776c4
--- /dev/null
+++ b/my-app/src/main/java/com/loogibot/lil/data/util/DatabaseUtils.java
@@ -0,0 +1,36 @@
+package com.loogibot.lil.data.util;
+
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.SQLException;
+import java.util.logging.Logger;
+
+public class DatabaseUtils {
+ private static final String URL = "jdbc:postresql://localhost:5432/localdb";
+ private static final String USERNAME = "localdbuser";
+ private static final String PASSWORD = "P0shW3ird!";
+ private static final Logger LOGGER = Logger.getLogger(DatabaseUtils.class.getName());
+ private static final String exceptionFormat = "exception in %s, message: %s, code: %s";
+ private static Connection connection;
+
+ public static Connection getConnection() {
+ if(connection == null){
+ synchronized(DatabaseUtils.class){
+ if(connection == null){
+ try{
+ connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
+ } catch(SQLException e) {
+ handleSqlException("DatabaseUtils.getConnection", e, LOGGER);
+ // System.out.println(exceptionFormat);
+ }
+ }
+ }
+ }
+ return connection;
+ }
+
+ public static void handleSqlException(String method, SQLException e, Logger log){
+ log.warning(String.format(exceptionFormat,method, e.getMessage(), e.getErrorCode()));
+ throw new RuntimeException();
+ }
+}
From 7cdf39a6a6aade87ac555dd4da4cd06da2cbc38b Mon Sep 17 00:00:00 2001
From: Luigi Mota
Date: Thu, 20 Feb 2025 19:37:36 +0000
Subject: [PATCH 03/13] set up the dao interface , service dao with all of the
methods with getall implemented , fixed the url for databaseUtils, and
accessed the test data in App.java main
---
.../src/main/java/com/loogibot/lil/App.java | 12 ++-
.../java/com/loogibot/lil/data/dao/Dao.java | 12 +++
.../com/loogibot/lil/data/dao/ServiceDao.java | 75 +++++++++++++++++++
.../com/loogibot/lil/data/entity/Service.java | 35 +++++++++
.../loogibot/lil/data/util/DatabaseUtils.java | 2 +-
5 files changed, 134 insertions(+), 2 deletions(-)
create mode 100644 my-app/src/main/java/com/loogibot/lil/data/dao/Dao.java
create mode 100644 my-app/src/main/java/com/loogibot/lil/data/dao/ServiceDao.java
create mode 100644 my-app/src/main/java/com/loogibot/lil/data/entity/Service.java
diff --git a/my-app/src/main/java/com/loogibot/lil/App.java b/my-app/src/main/java/com/loogibot/lil/App.java
index b9b7a1e..ef5a091 100644
--- a/my-app/src/main/java/com/loogibot/lil/App.java
+++ b/my-app/src/main/java/com/loogibot/lil/App.java
@@ -1,10 +1,20 @@
package com.loogibot.lil;
+import java.util.List;
+
+import com.loogibot.lil.data.dao.ServiceDao;
+import com.loogibot.lil.data.entity.Service;
+
/**
* Hello world!
*/
public class App {
public static void main(String[] args) {
- System.out.println("Hello World!");
+ ServiceDao serviceDao = new ServiceDao();
+ List services = serviceDao.getAll();
+ System.out.println("**** SERVICES ****");
+ System.out.println("\n *** GET_ALL ***");
+ services.forEach(System.out::println);
+
}
}
diff --git a/my-app/src/main/java/com/loogibot/lil/data/dao/Dao.java b/my-app/src/main/java/com/loogibot/lil/data/dao/Dao.java
new file mode 100644
index 0000000..6780e22
--- /dev/null
+++ b/my-app/src/main/java/com/loogibot/lil/data/dao/Dao.java
@@ -0,0 +1,12 @@
+package com.loogibot.lil.data.dao;
+import java.util.UUID;
+import java.util.Optional;
+import java.util.List;
+
+public interface Dao {
+ List getAll();
+ T create(T entity);
+ Optional getOne(Id id);
+ T update(T entity);
+ void delete(Id id);
+}
diff --git a/my-app/src/main/java/com/loogibot/lil/data/dao/ServiceDao.java b/my-app/src/main/java/com/loogibot/lil/data/dao/ServiceDao.java
new file mode 100644
index 0000000..ae8853d
--- /dev/null
+++ b/my-app/src/main/java/com/loogibot/lil/data/dao/ServiceDao.java
@@ -0,0 +1,75 @@
+package com.loogibot.lil.data.dao;
+
+import java.sql.Statement;
+import java.sql.Connection;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Optional;
+import java.util.UUID;
+import java.util.logging.Logger;
+
+import javax.xml.crypto.Data;
+
+import com.loogibot.lil.data.entity.Service;
+import com.loogibot.lil.data.util.DatabaseUtils;
+
+public class ServiceDao implements Dao {
+ private static final Logger LOGGER = Logger.getLogger(ServiceDao.class.getName());
+ private static final String GET_ALL = "select service_id, name, price from wisdom.services";
+
+ @Override
+ public List getAll() {
+ // TODO Auto-generated method stub
+ List services = new ArrayList<>();
+ Connection connection = DatabaseUtils.getConnection();
+
+ try(Statement statement = connection.createStatement()){
+ // make sure its not beans.Statement, use sql.Statement
+ ResultSet rs = statement.executeQuery(GET_ALL);
+ services = this.processResultSet(rs);
+ } catch (SQLException e) {
+ // TODO: handle exception
+ DatabaseUtils.handleSqlException("ServiceDao.getAll", e, LOGGER);
+ }
+ return services;
+ }
+
+ @Override
+ public Service create(Service entity) {
+ // TODO Auto-generated method stub
+ throw new UnsupportedOperationException("Unimplemented method 'create'");
+ }
+
+ @Override
+ public Optional getOne(UUID id) {
+ // TODO Auto-generated method stub
+ throw new UnsupportedOperationException("Unimplemented method 'getOne'");
+ }
+
+ @Override
+ public Service update(Service entity) {
+ // TODO Auto-generated method stub
+ throw new UnsupportedOperationException("Unimplemented method 'update'");
+ }
+
+ @Override
+ public void delete(UUID id) {
+ // TODO Auto-generated method stub
+ throw new UnsupportedOperationException("Unimplemented method 'delete'");
+ }
+
+ private List processResultSet(ResultSet rs) throws SQLException{
+ List services = new ArrayList<>();
+ while(rs.next()){
+ Service service = new Service();
+ service.setServiceId((UUID)rs.getObject("service_id"));
+ service.setName(rs.getString("name"));
+ service.setPrice(rs.getBigDecimal("price"));
+ services.add(service);
+ }
+ return services;
+ }
+
+}
diff --git a/my-app/src/main/java/com/loogibot/lil/data/entity/Service.java b/my-app/src/main/java/com/loogibot/lil/data/entity/Service.java
new file mode 100644
index 0000000..dcc826e
--- /dev/null
+++ b/my-app/src/main/java/com/loogibot/lil/data/entity/Service.java
@@ -0,0 +1,35 @@
+package com.loogibot.lil.data.entity;
+
+import java.math.BigDecimal;
+import java.util.UUID;
+
+public class Service {
+ private UUID serviceId;
+ private String name;
+ private BigDecimal price;
+
+ public UUID getServiceId() {
+ return serviceId;
+ }
+ public void setServiceId(UUID serviceId) {
+ this.serviceId = serviceId;
+ }
+ public String getName() {
+ return name;
+ }
+ public void setName(String name) {
+ this.name = name;
+ }
+ public BigDecimal getPrice() {
+ return price;
+ }
+ public void setPrice(BigDecimal price) {
+ this.price = price;
+ }
+
+ @Override
+ public String toString(){
+ return "Service [serviceId=" + serviceId + ", name=" + name + ", price=" + price + "]";
+ }
+
+}
diff --git a/my-app/src/main/java/com/loogibot/lil/data/util/DatabaseUtils.java b/my-app/src/main/java/com/loogibot/lil/data/util/DatabaseUtils.java
index 86776c4..8398015 100644
--- a/my-app/src/main/java/com/loogibot/lil/data/util/DatabaseUtils.java
+++ b/my-app/src/main/java/com/loogibot/lil/data/util/DatabaseUtils.java
@@ -6,7 +6,7 @@
import java.util.logging.Logger;
public class DatabaseUtils {
- private static final String URL = "jdbc:postresql://localhost:5432/localdb";
+ private static final String URL = "jdbc:postgresql://localhost:5432/localdb";
private static final String USERNAME = "localdbuser";
private static final String PASSWORD = "P0shW3ird!";
private static final Logger LOGGER = Logger.getLogger(DatabaseUtils.class.getName());
From ba8ce4171cdca85a1bafde9d477f1d01654fe85a Mon Sep 17 00:00:00 2001
From: Luigi Mota
Date: Thu, 20 Feb 2025 19:50:12 +0000
Subject: [PATCH 04/13] getOne implemented and tested
---
my-app/src/main/java/com/loogibot/lil/App.java | 3 +++
.../com/loogibot/lil/data/dao/ServiceDao.java | 17 ++++++++++++++++-
2 files changed, 19 insertions(+), 1 deletion(-)
diff --git a/my-app/src/main/java/com/loogibot/lil/App.java b/my-app/src/main/java/com/loogibot/lil/App.java
index ef5a091..4ce8d36 100644
--- a/my-app/src/main/java/com/loogibot/lil/App.java
+++ b/my-app/src/main/java/com/loogibot/lil/App.java
@@ -1,6 +1,7 @@
package com.loogibot.lil;
import java.util.List;
+import java.util.Optional;
import com.loogibot.lil.data.dao.ServiceDao;
import com.loogibot.lil.data.entity.Service;
@@ -16,5 +17,7 @@ public static void main(String[] args) {
System.out.println("\n *** GET_ALL ***");
services.forEach(System.out::println);
+ Optional service = serviceDao.getOne(services.get(0).getServiceId());
+ System.out.println("\n *** GET ONE ***\n" + service.get());
}
}
diff --git a/my-app/src/main/java/com/loogibot/lil/data/dao/ServiceDao.java b/my-app/src/main/java/com/loogibot/lil/data/dao/ServiceDao.java
index ae8853d..c9121ee 100644
--- a/my-app/src/main/java/com/loogibot/lil/data/dao/ServiceDao.java
+++ b/my-app/src/main/java/com/loogibot/lil/data/dao/ServiceDao.java
@@ -2,6 +2,7 @@
import java.sql.Statement;
import java.sql.Connection;
+import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
@@ -18,6 +19,8 @@
public class ServiceDao implements Dao {
private static final Logger LOGGER = Logger.getLogger(ServiceDao.class.getName());
private static final String GET_ALL = "select service_id, name, price from wisdom.services";
+ private static final String GET_BY_ID = "select service_id, name, price from wisdom.services where service_id = ?";
+
@Override
public List getAll() {
@@ -45,7 +48,19 @@ public Service create(Service entity) {
@Override
public Optional getOne(UUID id) {
// TODO Auto-generated method stub
- throw new UnsupportedOperationException("Unimplemented method 'getOne'");
+ try(PreparedStatement statement = DatabaseUtils.getConnection().prepareStatement(GET_BY_ID)){
+ statement.setObject(1, id);
+ ResultSet rs = statement.executeQuery();
+ List services = this.processResultSet(rs);
+ if(services.isEmpty()){
+ return Optional.empty();
+ }
+ return Optional.of(services.get(0));
+ } catch (SQLException e) {
+ // TODO: handle exception
+ DatabaseUtils.handleSqlException("ServiceDao.getOne", e, LOGGER);
+ }
+ return Optional.empty();
}
@Override
From cde01561a9562698c7f415187177810e128e373d Mon Sep 17 00:00:00 2001
From: Luigi Mota
Date: Fri, 21 Feb 2025 04:22:29 +0000
Subject: [PATCH 05/13] set up the create method of the service dao, note the
try within the try for the connection and statement objects
---
.../src/main/java/com/loogibot/lil/App.java | 8 ++++
.../com/loogibot/lil/data/dao/ServiceDao.java | 43 ++++++++++++++-----
2 files changed, 41 insertions(+), 10 deletions(-)
diff --git a/my-app/src/main/java/com/loogibot/lil/App.java b/my-app/src/main/java/com/loogibot/lil/App.java
index 4ce8d36..74fedb5 100644
--- a/my-app/src/main/java/com/loogibot/lil/App.java
+++ b/my-app/src/main/java/com/loogibot/lil/App.java
@@ -1,5 +1,6 @@
package com.loogibot.lil;
+import java.math.BigDecimal;
import java.util.List;
import java.util.Optional;
@@ -19,5 +20,12 @@ public static void main(String[] args) {
Optional service = serviceDao.getOne(services.get(0).getServiceId());
System.out.println("\n *** GET ONE ***\n" + service.get());
+
+ Service newService = new Service();
+ newService.setName("FooBarBaz" + System.currentTimeMillis());
+ newService.setPrice(new BigDecimal(4.35));
+ newService = serviceDao.create(newService);
+ System.out.println("\n *** CREATE ***\n" + newService);
+
}
}
diff --git a/my-app/src/main/java/com/loogibot/lil/data/dao/ServiceDao.java b/my-app/src/main/java/com/loogibot/lil/data/dao/ServiceDao.java
index c9121ee..ab3ddd6 100644
--- a/my-app/src/main/java/com/loogibot/lil/data/dao/ServiceDao.java
+++ b/my-app/src/main/java/com/loogibot/lil/data/dao/ServiceDao.java
@@ -16,19 +16,19 @@
import com.loogibot.lil.data.entity.Service;
import com.loogibot.lil.data.util.DatabaseUtils;
-public class ServiceDao implements Dao {
+public class ServiceDao implements Dao {
private static final Logger LOGGER = Logger.getLogger(ServiceDao.class.getName());
private static final String GET_ALL = "select service_id, name, price from wisdom.services";
private static final String GET_BY_ID = "select service_id, name, price from wisdom.services where service_id = ?";
-
+ private static final String CREATE = "insert into wisdom.services (service_id, name, price) values (?,?,?)";
@Override
public List getAll() {
// TODO Auto-generated method stub
List services = new ArrayList<>();
Connection connection = DatabaseUtils.getConnection();
-
- try(Statement statement = connection.createStatement()){
+
+ try (Statement statement = connection.createStatement()) {
// make sure its not beans.Statement, use sql.Statement
ResultSet rs = statement.executeQuery(GET_ALL);
services = this.processResultSet(rs);
@@ -42,17 +42,40 @@ public List getAll() {
@Override
public Service create(Service entity) {
// TODO Auto-generated method stub
- throw new UnsupportedOperationException("Unimplemented method 'create'");
+ UUID serviceId = UUID.randomUUID();
+ Connection connection = DatabaseUtils.getConnection();
+ try {
+ connection.setAutoCommit(false);
+ PreparedStatement statement = connection.prepareStatement(CREATE);
+ statement.setObject(1, serviceId);
+ statement.setString(2, entity.getName());
+ statement.setBigDecimal(3, entity.getPrice());
+ statement.execute();
+ connection.commit();
+ statement.close();
+ } catch (SQLException e) {
+ try{
+ connection.rollback();
+ } catch (SQLException sqle) {
+ DatabaseUtils.handleSqlException("ServiceDao.create.rollback", sqle, LOGGER);
+ }
+ DatabaseUtils.handleSqlException("ServiceDao.create", e, LOGGER);
+ }
+ Optional service = this.getOne(serviceId);
+ if (!service.isPresent()){
+ return null;
+ }
+ return service.get();
}
@Override
public Optional getOne(UUID id) {
// TODO Auto-generated method stub
- try(PreparedStatement statement = DatabaseUtils.getConnection().prepareStatement(GET_BY_ID)){
+ try (PreparedStatement statement = DatabaseUtils.getConnection().prepareStatement(GET_BY_ID)) {
statement.setObject(1, id);
ResultSet rs = statement.executeQuery();
List services = this.processResultSet(rs);
- if(services.isEmpty()){
+ if (services.isEmpty()) {
return Optional.empty();
}
return Optional.of(services.get(0));
@@ -75,11 +98,11 @@ public void delete(UUID id) {
throw new UnsupportedOperationException("Unimplemented method 'delete'");
}
- private List processResultSet(ResultSet rs) throws SQLException{
+ private List processResultSet(ResultSet rs) throws SQLException {
List services = new ArrayList<>();
- while(rs.next()){
+ while (rs.next()) {
Service service = new Service();
- service.setServiceId((UUID)rs.getObject("service_id"));
+ service.setServiceId((UUID) rs.getObject("service_id"));
service.setName(rs.getString("name"));
service.setPrice(rs.getBigDecimal("price"));
services.add(service);
From 5a55dd2364e117608111fc2dbc8106be6727eb6f Mon Sep 17 00:00:00 2001
From: Luigi Mota
Date: Fri, 21 Feb 2025 15:42:30 +0000
Subject: [PATCH 06/13] added update method in service dao
---
.../src/main/java/com/loogibot/lil/App.java | 3 +++
.../com/loogibot/lil/data/dao/ServiceDao.java | 22 ++++++++++++++++++-
2 files changed, 24 insertions(+), 1 deletion(-)
diff --git a/my-app/src/main/java/com/loogibot/lil/App.java b/my-app/src/main/java/com/loogibot/lil/App.java
index 74fedb5..6f28220 100644
--- a/my-app/src/main/java/com/loogibot/lil/App.java
+++ b/my-app/src/main/java/com/loogibot/lil/App.java
@@ -27,5 +27,8 @@ public static void main(String[] args) {
newService = serviceDao.create(newService);
System.out.println("\n *** CREATE ***\n" + newService);
+ newService.setPrice(new BigDecimal(13.45));
+ newService = serviceDao.update(newService);
+ System.out.println("\n *** UPDATE *** \n" + newService);
}
}
diff --git a/my-app/src/main/java/com/loogibot/lil/data/dao/ServiceDao.java b/my-app/src/main/java/com/loogibot/lil/data/dao/ServiceDao.java
index ab3ddd6..65d9552 100644
--- a/my-app/src/main/java/com/loogibot/lil/data/dao/ServiceDao.java
+++ b/my-app/src/main/java/com/loogibot/lil/data/dao/ServiceDao.java
@@ -21,6 +21,7 @@ public class ServiceDao implements Dao {
private static final String GET_ALL = "select service_id, name, price from wisdom.services";
private static final String GET_BY_ID = "select service_id, name, price from wisdom.services where service_id = ?";
private static final String CREATE = "insert into wisdom.services (service_id, name, price) values (?,?,?)";
+ private static final String UPDATE = "update wisdom.services set name = ?, price = ? where service_id = ?";
@Override
public List getAll() {
@@ -89,7 +90,26 @@ public Optional getOne(UUID id) {
@Override
public Service update(Service entity) {
// TODO Auto-generated method stub
- throw new UnsupportedOperationException("Unimplemented method 'update'");
+ Connection connection = DatabaseUtils.getConnection();
+ try {
+ connection.setAutoCommit(false);
+ PreparedStatement statement = connection.prepareStatement(UPDATE);
+ statement.setString(1, entity.getName());
+ statement.setBigDecimal(2, entity.getPrice());
+ statement.setObject(3, entity.getServiceId());
+ statement.execute();
+ connection.commit();
+ statement.close();
+ } catch (SQLException e) {
+ try {
+ connection.rollback();
+ } catch (SQLException sqle) {
+ DatabaseUtils.handleSqlException("ServiceDao.update.rollback", sqle, LOGGER);
+ }
+ DatabaseUtils.handleSqlException("ServiceDao.update", e, LOGGER);
+ }
+
+ return this.getOne(entity.getServiceId()).get();
}
@Override
From c7e56d4dc2889de3f7ce642bdb110c8b9b42b4e5 Mon Sep 17 00:00:00 2001
From: Luigi Mota
Date: Fri, 21 Feb 2025 15:54:39 +0000
Subject: [PATCH 07/13] delete method added, though I'll need to try this
again. I notice with this project, I don't know enough of the connection,
service and postgresql functions to fully understand how its working, notably
their respective methods and where/how to use them, unlike Android Room DB I
am much more familiar with the DAO system
---
.../src/main/java/com/loogibot/lil/App.java | 3 +++
.../com/loogibot/lil/data/dao/ServiceDao.java | 23 ++++++++++++++++---
2 files changed, 23 insertions(+), 3 deletions(-)
diff --git a/my-app/src/main/java/com/loogibot/lil/App.java b/my-app/src/main/java/com/loogibot/lil/App.java
index 6f28220..22f000b 100644
--- a/my-app/src/main/java/com/loogibot/lil/App.java
+++ b/my-app/src/main/java/com/loogibot/lil/App.java
@@ -30,5 +30,8 @@ public static void main(String[] args) {
newService.setPrice(new BigDecimal(13.45));
newService = serviceDao.update(newService);
System.out.println("\n *** UPDATE *** \n" + newService);
+
+ serviceDao.delete(newService.getServiceId());
+ System.out.println("\n *** DELETE *** \n");
}
}
diff --git a/my-app/src/main/java/com/loogibot/lil/data/dao/ServiceDao.java b/my-app/src/main/java/com/loogibot/lil/data/dao/ServiceDao.java
index 65d9552..f2c16f5 100644
--- a/my-app/src/main/java/com/loogibot/lil/data/dao/ServiceDao.java
+++ b/my-app/src/main/java/com/loogibot/lil/data/dao/ServiceDao.java
@@ -22,6 +22,7 @@ public class ServiceDao implements Dao {
private static final String GET_BY_ID = "select service_id, name, price from wisdom.services where service_id = ?";
private static final String CREATE = "insert into wisdom.services (service_id, name, price) values (?,?,?)";
private static final String UPDATE = "update wisdom.services set name = ?, price = ? where service_id = ?";
+ private static final String DELETE = "delete from wisdom.services where service_id = ?";
@Override
public List getAll() {
@@ -55,7 +56,7 @@ public Service create(Service entity) {
connection.commit();
statement.close();
} catch (SQLException e) {
- try{
+ try {
connection.rollback();
} catch (SQLException sqle) {
DatabaseUtils.handleSqlException("ServiceDao.create.rollback", sqle, LOGGER);
@@ -63,7 +64,7 @@ public Service create(Service entity) {
DatabaseUtils.handleSqlException("ServiceDao.create", e, LOGGER);
}
Optional service = this.getOne(serviceId);
- if (!service.isPresent()){
+ if (!service.isPresent()) {
return null;
}
return service.get();
@@ -115,7 +116,23 @@ public Service update(Service entity) {
@Override
public void delete(UUID id) {
// TODO Auto-generated method stub
- throw new UnsupportedOperationException("Unimplemented method 'delete'");
+ Connection connection = DatabaseUtils.getConnection();
+
+ try {
+ connection.setAutoCommit(false);
+ PreparedStatement statement = connection.prepareStatement(DELETE);
+ statement.setObject(1, id);
+ statement.execute();
+ connection.commit();
+ statement.close();
+ } catch (SQLException e) {
+ try {
+ connection.rollback();
+ } catch (SQLException sqle) {
+ DatabaseUtils.handleSqlException("ServiceDao.delete.rollback", sqle, LOGGER);
+ }
+ DatabaseUtils.handleSqlException("ServiceDao.delete", e, LOGGER);
+ }
}
private List processResultSet(ResultSet rs) throws SQLException {
From aec1a05bc4366084e61c095ae7889fc296658ef2 Mon Sep 17 00:00:00 2001
From: Luigi Mota
Date: Fri, 21 Feb 2025 16:35:06 +0000
Subject: [PATCH 08/13] adding Customer and CustomerDao, trying to test it out,
might need to mess with the database itself to get this working
---
.../src/main/java/com/loogibot/lil/App.java | 26 ++++
.../loogibot/lil/data/dao/CustomerDao.java | 138 ++++++++++++++++++
.../com/loogibot/lil/data/dao/ServiceDao.java | 1 -
.../loogibot/lil/data/entity/Customer.java | 26 ++++
4 files changed, 190 insertions(+), 1 deletion(-)
create mode 100644 my-app/src/main/java/com/loogibot/lil/data/dao/CustomerDao.java
create mode 100644 my-app/src/main/java/com/loogibot/lil/data/entity/Customer.java
diff --git a/my-app/src/main/java/com/loogibot/lil/App.java b/my-app/src/main/java/com/loogibot/lil/App.java
index 22f000b..f385db4 100644
--- a/my-app/src/main/java/com/loogibot/lil/App.java
+++ b/my-app/src/main/java/com/loogibot/lil/App.java
@@ -4,7 +4,9 @@
import java.util.List;
import java.util.Optional;
+import com.loogibot.lil.data.dao.CustomerDao;
import com.loogibot.lil.data.dao.ServiceDao;
+import com.loogibot.lil.data.entity.Customer;
import com.loogibot.lil.data.entity.Service;
/**
@@ -33,5 +35,29 @@ public static void main(String[] args) {
serviceDao.delete(newService.getServiceId());
System.out.println("\n *** DELETE *** \n");
+
+ //
+
+ CustomerDao customerDao = new CustomerDao();
+ List customers = customerDao.getAll();
+ System.out.println("**** SERVICES ****");
+ System.out.println("\n *** GET_ALL ***");
+ customers.forEach(System.out::println);
+
+ Optional customer = customerDao.getOne(customers.get(0).getCustomerId());
+ System.out.println("\n *** GET ONE ***\n" + service.get());
+
+ Customer newCustomer = new Customer();
+ newCustomer.setName("JoeyCastillo" + System.currentTimeMillis());
+
+ newCustomer = customerDao.create(newCustomer);
+ System.out.println("\n *** CREATE ***\n" + newCustomer);
+
+ newCustomer = customerDao.update(newCustomer);
+ System.out.println("\n *** UPDATE *** \n" + newCustomer);
+
+ customerDao.delete(newCustomer.getCustomerId());
+ System.out.println("\n *** DELETE *** \n");
+
}
}
diff --git a/my-app/src/main/java/com/loogibot/lil/data/dao/CustomerDao.java b/my-app/src/main/java/com/loogibot/lil/data/dao/CustomerDao.java
new file mode 100644
index 0000000..1809563
--- /dev/null
+++ b/my-app/src/main/java/com/loogibot/lil/data/dao/CustomerDao.java
@@ -0,0 +1,138 @@
+package com.loogibot.lil.data.dao;
+
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Optional;
+import java.util.UUID;
+import java.util.logging.Logger;
+
+import com.loogibot.lil.data.entity.Customer;
+import com.loogibot.lil.data.util.DatabaseUtils;
+
+public class CustomerDao implements Dao {
+
+ private static final Logger LOGGER = Logger.getLogger(CustomerDao.class.getName());
+ private static final String GET_ALL = "select customer_id, name wisdom.services";
+ private static final String GET_BY_ID = "select customer_id, name from wisdom.services where customer_id = ?";
+ private static final String CREATE = "insert into wisdom.services (customer_id, name) values (?,?)";
+ private static final String UPDATE = "update wisdom.services set name = ?, where customer_id = ?";
+ private static final String DELETE = "delete from wisdom.services where customer_id = ?";
+
+ @Override
+ public List getAll() {
+ List customers = new ArrayList<>();
+ Connection connection = DatabaseUtils.getConnection();
+ try (Statement statement = connection.createStatement()) {
+ ResultSet rs = statement.executeQuery(GET_ALL);
+ customers = this.processResultSet(rs);
+ } catch (SQLException e) {
+ DatabaseUtils.handleSqlException("CustomerDao.getAll", e, LOGGER);
+ }
+ return customers;
+ }
+
+ @Override
+ public Customer create(Customer entity) {
+ UUID customerId = UUID.randomUUID();
+ Connection connection = DatabaseUtils.getConnection();
+ try {
+ connection.setAutoCommit(false);
+ PreparedStatement statement = connection.prepareStatement(CREATE);
+ statement.setObject(1, customerId);
+ statement.setString(2, entity.getName());
+ statement.execute();
+ connection.commit();
+ statement.close();
+ } catch (SQLException e) {
+ try {
+ connection.rollback();
+ } catch (SQLException sqle) {
+ DatabaseUtils.handleSqlException("CustomerDao.create.rollback", sqle, LOGGER);
+ }
+ DatabaseUtils.handleSqlException("CustomerDao.create", e, LOGGER);
+ }
+ Optional customer = this.getOne(customerId);
+ if (!customer.isPresent()) {
+ return null;
+ }
+ return customer.get();
+ }
+
+ @Override
+ public Optional getOne(UUID id) {
+ try (PreparedStatement statement = DatabaseUtils.getConnection().prepareStatement(GET_BY_ID)) {
+ statement.setObject(1, id);
+ ResultSet rs = statement.executeQuery();
+ List customers = this.processResultSet(rs);
+ if (customers.isEmpty()) {
+ return Optional.empty();
+ }
+ return Optional.of(customers.get(0));
+ } catch (SQLException e) {
+ // TODO: handle exception
+ DatabaseUtils.handleSqlException("CustomerDao.getOne", e, LOGGER);
+ }
+ return Optional.empty();
+ }
+
+ @Override
+ public Customer update(Customer entity) {
+ Connection connection = DatabaseUtils.getConnection();
+ try {
+ connection.setAutoCommit(false);
+ PreparedStatement statement = connection.prepareStatement(UPDATE);
+ statement.setString(1, entity.getName());
+ statement.setObject(3, entity.getCustomerId());
+ statement.execute();
+ connection.commit();
+ statement.close();
+ } catch (SQLException e) {
+ try {
+ connection.rollback();
+ } catch (SQLException sqle) {
+ DatabaseUtils.handleSqlException("CustomerDao.update.rollback", sqle, LOGGER);
+ }
+ DatabaseUtils.handleSqlException("CustomerDao.update", e, LOGGER);
+ }
+ return this.getOne(entity.getCustomerId()).get();
+ }
+
+ @Override
+ public void delete(UUID id) {
+ Connection connection = DatabaseUtils.getConnection();
+
+ try {
+ connection.setAutoCommit(false);
+ PreparedStatement statement = connection.prepareStatement(DELETE);
+ statement.setObject(1, id);
+ statement.execute();
+ connection.commit();
+ statement.close();
+ } catch (SQLException e) {
+ try {
+ connection.rollback();
+ } catch (SQLException sqle) {
+ DatabaseUtils.handleSqlException("CustomerDao.delete.rollback", sqle, LOGGER);
+ }
+ DatabaseUtils.handleSqlException("CustomerDao.delete", e, LOGGER);
+ }
+ }
+
+ private List processResultSet(ResultSet rs) throws SQLException {
+ List customers = new ArrayList<>();
+ while (rs.next()) {
+ Customer customer = new Customer();
+ customer.setCustomerId((UUID) rs.getObject("service_id"));
+ customer.setName(rs.getString("name"));
+
+ customers.add(customer);
+ }
+ return customers;
+ }
+
+}
diff --git a/my-app/src/main/java/com/loogibot/lil/data/dao/ServiceDao.java b/my-app/src/main/java/com/loogibot/lil/data/dao/ServiceDao.java
index f2c16f5..d9c2d2d 100644
--- a/my-app/src/main/java/com/loogibot/lil/data/dao/ServiceDao.java
+++ b/my-app/src/main/java/com/loogibot/lil/data/dao/ServiceDao.java
@@ -109,7 +109,6 @@ public Service update(Service entity) {
}
DatabaseUtils.handleSqlException("ServiceDao.update", e, LOGGER);
}
-
return this.getOne(entity.getServiceId()).get();
}
diff --git a/my-app/src/main/java/com/loogibot/lil/data/entity/Customer.java b/my-app/src/main/java/com/loogibot/lil/data/entity/Customer.java
new file mode 100644
index 0000000..3c1110a
--- /dev/null
+++ b/my-app/src/main/java/com/loogibot/lil/data/entity/Customer.java
@@ -0,0 +1,26 @@
+package com.loogibot.lil.data.entity;
+
+import java.util.UUID;
+
+public class Customer {
+ private UUID customerId;
+ private String name;
+
+ public UUID getCustomerId() {
+ return customerId;
+ }
+ public void setCustomerId(UUID serviceId) {
+ this.customerId = serviceId;
+ }
+ public String getName() {
+ return name;
+ }
+ public void setName(String name) {
+ this.name = name;
+ }
+ @Override
+ public String toString() {
+ return "Customer [serviceId=" + customerId + ", name=" + name + "]";
+ }
+
+}
From 2b9a4bac4476a1c8f422e2941b9bf4e976312a3d Mon Sep 17 00:00:00 2001
From: Luigi Mota
Date: Sun, 23 Feb 2025 16:34:17 +0000
Subject: [PATCH 09/13] on my way to fixing the Customer class to match the
ACTUAL table in the sql
---
.../loogibot/lil/data/dao/CustomerDao.java | 19 ++++++---
.../loogibot/lil/data/entity/Customer.java | 42 +++++++++++++++----
2 files changed, 46 insertions(+), 15 deletions(-)
diff --git a/my-app/src/main/java/com/loogibot/lil/data/dao/CustomerDao.java b/my-app/src/main/java/com/loogibot/lil/data/dao/CustomerDao.java
index 1809563..f59d993 100644
--- a/my-app/src/main/java/com/loogibot/lil/data/dao/CustomerDao.java
+++ b/my-app/src/main/java/com/loogibot/lil/data/dao/CustomerDao.java
@@ -44,7 +44,10 @@ public Customer create(Customer entity) {
connection.setAutoCommit(false);
PreparedStatement statement = connection.prepareStatement(CREATE);
statement.setObject(1, customerId);
- statement.setString(2, entity.getName());
+ statement.setString(2, entity.getFirst_name());
+ statement.setString(3, entity.getLast_name());
+ statement.setString(4, entity.getEmail());
+ statement.setString(5, entity.getPhone());
statement.execute();
connection.commit();
statement.close();
@@ -86,8 +89,11 @@ public Customer update(Customer entity) {
try {
connection.setAutoCommit(false);
PreparedStatement statement = connection.prepareStatement(UPDATE);
- statement.setString(1, entity.getName());
- statement.setObject(3, entity.getCustomerId());
+ statement.setObject(1, entity.getCustomerId());
+ statement.setString(2, entity.getFirst_name());
+ statement.setString(3, entity.getLast_name());
+ statement.setString(4, entity.getEmail());
+ statement.setString(5, entity.getPhone());
statement.execute();
connection.commit();
statement.close();
@@ -128,11 +134,12 @@ private List processResultSet(ResultSet rs) throws SQLException {
while (rs.next()) {
Customer customer = new Customer();
customer.setCustomerId((UUID) rs.getObject("service_id"));
- customer.setName(rs.getString("name"));
-
+ customer.setFirst_name(rs.getString("first_name"));
+ customer.setLast_name(rs.getString("last_name"));
+ customer.setEmail(rs.getString("email"));
+ customer.setPhone(rs.getString("phone"));
customers.add(customer);
}
return customers;
}
-
}
diff --git a/my-app/src/main/java/com/loogibot/lil/data/entity/Customer.java b/my-app/src/main/java/com/loogibot/lil/data/entity/Customer.java
index 3c1110a..9a55cef 100644
--- a/my-app/src/main/java/com/loogibot/lil/data/entity/Customer.java
+++ b/my-app/src/main/java/com/loogibot/lil/data/entity/Customer.java
@@ -4,23 +4,47 @@
public class Customer {
private UUID customerId;
- private String name;
-
+ private String first_name;
+ private String last_name;
+ private String email;
+ private String phone;
+
+ public String getFirst_name() {
+ return first_name;
+ }
+ public void setFirst_name(String first_name) {
+ this.first_name = first_name;
+ }
+ public String getLast_name() {
+ return last_name;
+ }
+ public void setLast_name(String last_name) {
+ this.last_name = last_name;
+ }
+ public String getEmail() {
+ return email;
+ }
+ public void setEmail(String email) {
+ this.email = email;
+ }
+ public String getPhone() {
+ return phone;
+ }
+ public void setPhone(String phone) {
+ this.phone = phone;
+ }
public UUID getCustomerId() {
return customerId;
}
public void setCustomerId(UUID serviceId) {
this.customerId = serviceId;
}
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
+
@Override
public String toString() {
- return "Customer [serviceId=" + customerId + ", name=" + name + "]";
+ return "Customer [customerId=" + customerId + ", first_name=" + first_name + ", last_name=" + last_name + ", email="
+ + email + ", phone=" + phone + "]";
}
+
}
From a92c27c55ba706002007e311cd9ed4769073bc21 Mon Sep 17 00:00:00 2001
From: Luigi Mota
Date: Mon, 24 Feb 2025 03:00:19 +0000
Subject: [PATCH 10/13] ok, I got most of it, just also needed to add the
address field in Customer and change the table to wisdom.customers, and
prolly a few other things, but I gget it
---
my-app/src/main/java/com/loogibot/lil/App.java | 5 ++++-
.../java/com/loogibot/lil/data/dao/CustomerDao.java | 10 +++++-----
2 files changed, 9 insertions(+), 6 deletions(-)
diff --git a/my-app/src/main/java/com/loogibot/lil/App.java b/my-app/src/main/java/com/loogibot/lil/App.java
index f385db4..1b808dd 100644
--- a/my-app/src/main/java/com/loogibot/lil/App.java
+++ b/my-app/src/main/java/com/loogibot/lil/App.java
@@ -48,7 +48,10 @@ public static void main(String[] args) {
System.out.println("\n *** GET ONE ***\n" + service.get());
Customer newCustomer = new Customer();
- newCustomer.setName("JoeyCastillo" + System.currentTimeMillis());
+ newCustomer.setFirst_name("Joey" + System.currentTimeMillis());
+ newCustomer.setLast_name("Castillo" + System.currentTimeMillis());
+ newCustomer.setEmail("Joey@Castillo.com" + System.currentTimeMillis());
+ newCustomer.setPhone("5552746" + System.currentTimeMillis());
newCustomer = customerDao.create(newCustomer);
System.out.println("\n *** CREATE ***\n" + newCustomer);
diff --git a/my-app/src/main/java/com/loogibot/lil/data/dao/CustomerDao.java b/my-app/src/main/java/com/loogibot/lil/data/dao/CustomerDao.java
index f59d993..58c7976 100644
--- a/my-app/src/main/java/com/loogibot/lil/data/dao/CustomerDao.java
+++ b/my-app/src/main/java/com/loogibot/lil/data/dao/CustomerDao.java
@@ -17,11 +17,11 @@
public class CustomerDao implements Dao {
private static final Logger LOGGER = Logger.getLogger(CustomerDao.class.getName());
- private static final String GET_ALL = "select customer_id, name wisdom.services";
- private static final String GET_BY_ID = "select customer_id, name from wisdom.services where customer_id = ?";
- private static final String CREATE = "insert into wisdom.services (customer_id, name) values (?,?)";
- private static final String UPDATE = "update wisdom.services set name = ?, where customer_id = ?";
- private static final String DELETE = "delete from wisdom.services where customer_id = ?";
+ private static final String GET_ALL = "select customer_id, first_name wisdom.customers";
+ private static final String GET_BY_ID = "select customer_id, first_name from wisdom.customers where customer_id = ?";
+ private static final String CREATE = "insert into wisdom.customers (customer_id, name) values (?,?)";
+ private static final String UPDATE = "update wisdom.customers set name = ?, where customer_id = ?";
+ private static final String DELETE = "delete from wisdom.customers where customer_id = ?";
@Override
public List getAll() {
From 4ddc21a3c153d1e19c1d2f8074fdcb9cf87ff850 Mon Sep 17 00:00:00 2001
From: Luigi Mota
Date: Mon, 24 Feb 2025 04:57:34 +0000
Subject: [PATCH 11/13] OK, my method implementation needed a bit of clean up,
and I haven't added the address field, BUT I did fix the sql commands,
notably adding email and phone to the GET_ALL, GET_BY_ID, CREATE, UPDATE, and
DELETE strings in CustomerDao. You need to add all of the relevant column
names for each sql command, of which I'm still getting the hang of, it seems
to be specifically a postgresql formatting thing, especially since the mehods
need to have a matching parameter, tho some of them don't need everying like
in getOne but the GET_ALL string has all of the fields from the Customer
class, note still without the address column
---
.../src/main/java/com/loogibot/lil/App.java | 6 ++---
.../loogibot/lil/data/dao/CustomerDao.java | 22 ++++++++++---------
2 files changed, 15 insertions(+), 13 deletions(-)
diff --git a/my-app/src/main/java/com/loogibot/lil/App.java b/my-app/src/main/java/com/loogibot/lil/App.java
index 1b808dd..2468f03 100644
--- a/my-app/src/main/java/com/loogibot/lil/App.java
+++ b/my-app/src/main/java/com/loogibot/lil/App.java
@@ -16,7 +16,7 @@ public class App {
public static void main(String[] args) {
ServiceDao serviceDao = new ServiceDao();
List services = serviceDao.getAll();
- System.out.println("**** SERVICES ****");
+ System.out.println("**** SERVICES BELOW ****");
System.out.println("\n *** GET_ALL ***");
services.forEach(System.out::println);
@@ -40,12 +40,12 @@ public static void main(String[] args) {
CustomerDao customerDao = new CustomerDao();
List customers = customerDao.getAll();
- System.out.println("**** SERVICES ****");
+ System.out.println("**** CUSTOMERS BELOW ****");
System.out.println("\n *** GET_ALL ***");
customers.forEach(System.out::println);
Optional customer = customerDao.getOne(customers.get(0).getCustomerId());
- System.out.println("\n *** GET ONE ***\n" + service.get());
+ System.out.println("\n *** GET ONE ***\n" + customer.get());
Customer newCustomer = new Customer();
newCustomer.setFirst_name("Joey" + System.currentTimeMillis());
diff --git a/my-app/src/main/java/com/loogibot/lil/data/dao/CustomerDao.java b/my-app/src/main/java/com/loogibot/lil/data/dao/CustomerDao.java
index 58c7976..b55d18d 100644
--- a/my-app/src/main/java/com/loogibot/lil/data/dao/CustomerDao.java
+++ b/my-app/src/main/java/com/loogibot/lil/data/dao/CustomerDao.java
@@ -17,10 +17,10 @@
public class CustomerDao implements Dao {
private static final Logger LOGGER = Logger.getLogger(CustomerDao.class.getName());
- private static final String GET_ALL = "select customer_id, first_name wisdom.customers";
- private static final String GET_BY_ID = "select customer_id, first_name from wisdom.customers where customer_id = ?";
- private static final String CREATE = "insert into wisdom.customers (customer_id, name) values (?,?)";
- private static final String UPDATE = "update wisdom.customers set name = ?, where customer_id = ?";
+ private static final String GET_ALL = "select customer_id, first_name, last_name, email, phone from wisdom.customers";
+ private static final String GET_BY_ID = "select customer_id, last_name, first_name, email, phone from wisdom.customers where customer_id = ?";
+ private static final String CREATE = "insert into wisdom.customers (customer_id, last_name, first_name, email, phone) values (?,?,?,?,?)";
+ private static final String UPDATE = "update wisdom.customers set first_name = ?, last_name = ?, email = ?, phone = ? where customer_id = ?";
private static final String DELETE = "delete from wisdom.customers where customer_id = ?";
@Override
@@ -30,6 +30,7 @@ public List getAll() {
try (Statement statement = connection.createStatement()) {
ResultSet rs = statement.executeQuery(GET_ALL);
customers = this.processResultSet(rs);
+ rs.close();
} catch (SQLException e) {
DatabaseUtils.handleSqlException("CustomerDao.getAll", e, LOGGER);
}
@@ -89,11 +90,12 @@ public Customer update(Customer entity) {
try {
connection.setAutoCommit(false);
PreparedStatement statement = connection.prepareStatement(UPDATE);
- statement.setObject(1, entity.getCustomerId());
- statement.setString(2, entity.getFirst_name());
- statement.setString(3, entity.getLast_name());
- statement.setString(4, entity.getEmail());
- statement.setString(5, entity.getPhone());
+ // statement.setObject(1, entity.getCustomerId());
+ statement.setString(1, entity.getFirst_name());
+ statement.setString(2, entity.getLast_name());
+ statement.setString(3, entity.getEmail());
+ statement.setString(4, entity.getPhone());
+ statement.setObject(5, entity.getCustomerId());
statement.execute();
connection.commit();
statement.close();
@@ -133,7 +135,7 @@ private List processResultSet(ResultSet rs) throws SQLException {
List customers = new ArrayList<>();
while (rs.next()) {
Customer customer = new Customer();
- customer.setCustomerId((UUID) rs.getObject("service_id"));
+ customer.setCustomerId((UUID) rs.getObject("customer_id"));
customer.setFirst_name(rs.getString("first_name"));
customer.setLast_name(rs.getString("last_name"));
customer.setEmail(rs.getString("email"));
From af1cddfde6eee800b6b418a44d825a2cf0e3687b Mon Sep 17 00:00:00 2001
From: Luigi Mota
Date: Mon, 24 Feb 2025 05:01:49 +0000
Subject: [PATCH 12/13] started the SimpleProduct class
---
.../lil/data/entity/SimpleProduct.java | 72 +++++++++++++++++++
1 file changed, 72 insertions(+)
create mode 100644 my-app/src/main/java/com/loogibot/lil/data/entity/SimpleProduct.java
diff --git a/my-app/src/main/java/com/loogibot/lil/data/entity/SimpleProduct.java b/my-app/src/main/java/com/loogibot/lil/data/entity/SimpleProduct.java
new file mode 100644
index 0000000..c14cb93
--- /dev/null
+++ b/my-app/src/main/java/com/loogibot/lil/data/entity/SimpleProduct.java
@@ -0,0 +1,72 @@
+package com.loogibot.lil.data.entity;
+
+import java.math.BigDecimal;
+import java.util.UUID;
+
+public class SimpleProduct {
+ private UUID productId;
+ private String name;
+ private BigDecimal price;
+ private UUID vendorId;
+ private String vendorName;
+ private String contact;
+ private String phoneNumber;
+ private String email;
+ private String address;
+
+ public UUID getProductId() {
+ return productId;
+ }
+ public void setProductId(UUID productId) {
+ this.productId = productId;
+ }
+ public String getName() {
+ return name;
+ }
+ public void setName(String name) {
+ this.name = name;
+ }
+ public BigDecimal getPrice() {
+ return price;
+ }
+ public void setPrice(BigDecimal price) {
+ this.price = price;
+ }
+ public UUID getVendorId() {
+ return vendorId;
+ }
+ public void setVendorId(UUID vendorId) {
+ this.vendorId = vendorId;
+ }
+ public String getVendorName() {
+ return vendorName;
+ }
+ public void setVendorName(String vendorName) {
+ this.vendorName = vendorName;
+ }
+ public String getContact() {
+ return contact;
+ }
+ public void setContact(String contact) {
+ this.contact = contact;
+ }
+ public String getPhoneNumber() {
+ return phoneNumber;
+ }
+ public void setPhoneNumber(String phoneNumber) {
+ this.phoneNumber = phoneNumber;
+ }
+ public String getEmail() {
+ return email;
+ }
+ public void setEmail(String email) {
+ this.email = email;
+ }
+ public String getAddress() {
+ return address;
+ }
+ public void setAddress(String address) {
+ this.address = address;
+ }
+
+}
From 7a05e1547b620423fbe39d46d102428204349efb Mon Sep 17 00:00:00 2001
From: Luigi Mota
Date: Sat, 1 Mar 2025 15:13:27 +0000
Subject: [PATCH 13/13] i finished the course, passed the test and earned the
certificate
---
.../src/main/java/com/loogibot/lil/App.java | 18 ++++++++-
.../loogibot/lil/data/dao/CustomerDao.java | 18 +++++++++
.../com/loogibot/lil/data/dao/ServiceDao.java | 15 +++++++
.../lil/data/dao/SimpleProductDao.java | 40 +++++++++++++++++++
4 files changed, 90 insertions(+), 1 deletion(-)
create mode 100644 my-app/src/main/java/com/loogibot/lil/data/dao/SimpleProductDao.java
diff --git a/my-app/src/main/java/com/loogibot/lil/App.java b/my-app/src/main/java/com/loogibot/lil/App.java
index 2468f03..75021c3 100644
--- a/my-app/src/main/java/com/loogibot/lil/App.java
+++ b/my-app/src/main/java/com/loogibot/lil/App.java
@@ -3,9 +3,11 @@
import java.math.BigDecimal;
import java.util.List;
import java.util.Optional;
+import java.util.UUID;
import com.loogibot.lil.data.dao.CustomerDao;
import com.loogibot.lil.data.dao.ServiceDao;
+import com.loogibot.lil.data.dao.SimpleProductDao;
import com.loogibot.lil.data.entity.Customer;
import com.loogibot.lil.data.entity.Service;
@@ -52,7 +54,7 @@ public static void main(String[] args) {
newCustomer.setLast_name("Castillo" + System.currentTimeMillis());
newCustomer.setEmail("Joey@Castillo.com" + System.currentTimeMillis());
newCustomer.setPhone("5552746" + System.currentTimeMillis());
-
+
newCustomer = customerDao.create(newCustomer);
System.out.println("\n *** CREATE ***\n" + newCustomer);
@@ -62,5 +64,19 @@ public static void main(String[] args) {
customerDao.delete(newCustomer.getCustomerId());
System.out.println("\n *** DELETE *** \n");
+ System.out.println("**** Simple Product BELOW ****");
+ SimpleProductDao spdao = new SimpleProductDao();
+ UUID productId = spdao.createProduct("Zafa", new BigDecimal(83.76), "Jaloo");
+ System.out.println(productId);
+
+ System.out.println("**** LIMIT ****");
+ serviceDao.getAllLimit(2).forEach(System.out::println);
+
+ System.out.println("**** PAGED ****");
+
+ for(int i = 1; i<11; i++){
+ System.out.println("Page numbe: " + i);
+ customerDao.getAllPaged(i, 10).forEach(System.out::println);
+ }
}
}
diff --git a/my-app/src/main/java/com/loogibot/lil/data/dao/CustomerDao.java b/my-app/src/main/java/com/loogibot/lil/data/dao/CustomerDao.java
index b55d18d..e73588a 100644
--- a/my-app/src/main/java/com/loogibot/lil/data/dao/CustomerDao.java
+++ b/my-app/src/main/java/com/loogibot/lil/data/dao/CustomerDao.java
@@ -22,6 +22,24 @@ public class CustomerDao implements Dao {
private static final String CREATE = "insert into wisdom.customers (customer_id, last_name, first_name, email, phone) values (?,?,?,?,?)";
private static final String UPDATE = "update wisdom.customers set first_name = ?, last_name = ?, email = ?, phone = ? where customer_id = ?";
private static final String DELETE = "delete from wisdom.customers where customer_id = ?";
+ private static final String GET_ALL_PAGED = "select customer_id, first_name, last_name, email, phone from wisdom.customers order by last_name, first_name, email, phone LIMIT ? OFFSET ?";
+
+
+ public List getAllPaged(int pageNumber, int limit){
+ List customers = new ArrayList<>();
+ Connection connection = DatabaseUtils.getConnection();
+ int offset = ((pageNumber - 1) * limit);
+ try(PreparedStatement statement = connection.prepareStatement(GET_ALL_PAGED)){
+ statement.setInt(1, limit);
+ statement.setInt(2, offset);
+ ResultSet rs = statement.executeQuery();
+ customers = this.processResultSet(rs);
+ rs.close();
+ } catch (SQLException e){
+ DatabaseUtils.handleSqlException("CustomerDao.getAllPaged", e, LOGGER);
+ }
+ return customers;
+ }
@Override
public List getAll() {
diff --git a/my-app/src/main/java/com/loogibot/lil/data/dao/ServiceDao.java b/my-app/src/main/java/com/loogibot/lil/data/dao/ServiceDao.java
index d9c2d2d..5f488b1 100644
--- a/my-app/src/main/java/com/loogibot/lil/data/dao/ServiceDao.java
+++ b/my-app/src/main/java/com/loogibot/lil/data/dao/ServiceDao.java
@@ -23,6 +23,7 @@ public class ServiceDao implements Dao {
private static final String CREATE = "insert into wisdom.services (service_id, name, price) values (?,?,?)";
private static final String UPDATE = "update wisdom.services set name = ?, price = ? where service_id = ?";
private static final String DELETE = "delete from wisdom.services where service_id = ?";
+ private static final String GET_ALL_LIMIT = "select service_id, name, price from wisdom.services order by name limit ?";
@Override
public List getAll() {
@@ -134,6 +135,20 @@ public void delete(UUID id) {
}
}
+ public List getAllLimit(int limit){
+ List services = new ArrayList<>();
+ Connection connection = DatabaseUtils.getConnection();
+ try(PreparedStatement statement = connection.prepareStatement(GET_ALL_LIMIT)){
+ statement.setInt(1, limit);
+ ResultSet rs = statement.executeQuery();
+ services = this.processResultSet(rs);
+ rs.close();
+ } catch (SQLException e) {
+ DatabaseUtils.handleSqlException("ServiceDao.getAll", null, LOGGER);
+ }
+ return services;
+ }
+
private List processResultSet(ResultSet rs) throws SQLException {
List services = new ArrayList<>();
while (rs.next()) {
diff --git a/my-app/src/main/java/com/loogibot/lil/data/dao/SimpleProductDao.java b/my-app/src/main/java/com/loogibot/lil/data/dao/SimpleProductDao.java
new file mode 100644
index 0000000..11e7f74
--- /dev/null
+++ b/my-app/src/main/java/com/loogibot/lil/data/dao/SimpleProductDao.java
@@ -0,0 +1,40 @@
+package com.loogibot.lil.data.dao;
+
+import java.math.BigDecimal;
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.UUID;
+import java.util.logging.Logger;
+
+import com.loogibot.lil.data.util.DatabaseUtils;
+
+public class SimpleProductDao {
+ private static final Logger LOGGER = Logger.getLogger(SimpleProductDao.class.getName());
+ private static final String CREATE = "select * from createproduct(?,?,?)";
+
+ public UUID createProduct(String name, BigDecimal price, String vendorName) {
+ Connection connection = DatabaseUtils.getConnection();
+ UUID returnVal = null;
+ try {
+ connection.setAutoCommit(false);
+ PreparedStatement statement = connection.prepareStatement(CREATE);
+ statement.setString(1, name);
+ statement.setBigDecimal(2, price);
+ statement.setString(3, vendorName);
+ ResultSet rs = statement.executeQuery();
+ while (rs.next()) {
+ returnVal = (UUID) rs.getObject("createProduct");
+ }
+ } catch (SQLException e) {
+ try {
+ connection.rollback();
+ } catch (SQLException sqle) {
+ DatabaseUtils.handleSqlException("SimpleProductDao.create.rollback", sqle, LOGGER);
+ }
+ DatabaseUtils.handleSqlException("SimpleProductDao.create.rollback", e, LOGGER);
+ }
+ return returnVal;
+ }
+}