From fbd82af857cd7eb896fbfb7dde582586aed89856 Mon Sep 17 00:00:00 2001
From: Donell Shamuyarira <40654981@napier.ac.uk>
Date: Fri, 10 Oct 2025 13:48:00 +0100
Subject: [PATCH 1/9] ADDING DB AND DOCKERFILE DB
---
.gitmodules | 3 +++
Dockerfile | 3 ++-
db/Dockerfile | 13 +++++++++++++
db/test_db | 1 +
4 files changed, 19 insertions(+), 1 deletion(-)
create mode 100644 .gitmodules
create mode 100644 db/Dockerfile
create mode 160000 db/test_db
diff --git a/.gitmodules b/.gitmodules
new file mode 100644
index 0000000..68efcd5
--- /dev/null
+++ b/.gitmodules
@@ -0,0 +1,3 @@
+[submodule "db/test_db"]
+ path = db/test_db
+ url = https://github.com/datacharmer/test_db
diff --git a/Dockerfile b/Dockerfile
index f60cfbf..5ce2201 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -1,4 +1,5 @@
FROM openjdk:latest
COPY ./target/seMethods-0.1.0-2-jar-with-dependencies.jar /tmp
WORKDIR /tmp
-ENTRYPOINT ["java", "-jar", "seMethods-0.1.0-2-jar-with-dependencies.jar"]
\ No newline at end of file
+ENTRYPOINT ["java", "-jar", "seMethods-0.1.0-2-jar-with-dependencies.jar"]
+
diff --git a/db/Dockerfile b/db/Dockerfile
new file mode 100644
index 0000000..dc27539
--- /dev/null
+++ b/db/Dockerfile
@@ -0,0 +1,13 @@
+# Use the latest MySQL image
+FROM mysql/mysql-server:latest
+# Set the working directory
+WORKDIR /tmp
+# Copy all the files to the working directory of the container
+COPY test_db/*.sql /tmp/
+COPY test_db/*.dump /tmp/
+# Copy the main SQL file to docker-entrypoint-initdb.d.
+# Scripts and SQL files in this folder are executed on container startup.
+# This is specific to MySQL.
+COPY test_db/employees.sql /docker-entrypoint-initdb.d
+# Set the root password
+ENV MYSQL_ROOT_PASSWORD example
\ No newline at end of file
diff --git a/db/test_db b/db/test_db
new file mode 160000
index 0000000..3c7fa05
--- /dev/null
+++ b/db/test_db
@@ -0,0 +1 @@
+Subproject commit 3c7fa05e04b4c339d91a43b7029a210212d48e6c
From 44918aa1ed5bd2096f9b3e4b69efc7a9961031bf Mon Sep 17 00:00:00 2001
From: Don Sham <40654981@napier.ac.uk>
Date: Mon, 13 Oct 2025 18:37:49 +0100
Subject: [PATCH 2/9] Update main.yml
---
.github/workflows/main.yml | 20 +++++++++-----------
1 file changed, 9 insertions(+), 11 deletions(-)
diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml
index ead8076..cf1835a 100644
--- a/.github/workflows/main.yml
+++ b/.github/workflows/main.yml
@@ -8,16 +8,14 @@ jobs:
steps:
- name: Checkout
uses: actions/checkout@v4
- - name: Set up JDK 17
- uses: actions/setup-java@v2
with:
- java-version: '17'
+ submodules: recursive
+ - name: Set up JDK 11
+ uses: actions/setup-java@v4
+ with:
+ java-version: '11'
distribution: 'adopt'
- - name: Compile with Maven
- run: mvn compile
- - name: Build Docker Image
- run: docker build -t devopsimage .
- - name: Run image
- run: docker run --name devopscontainer -d devopsimage
- - name: view logs
- run: docker logs devopscontainer
+ - name: Build with Maven
+ run: mvn package
+ - name: Run docker compose
+ run: docker compose up --abort-on-container-exit
From e3025ae79c67f0075c09da965722e8aa6f2b587a Mon Sep 17 00:00:00 2001
From: Donell Shamuyarira <40654981@napier.ac.uk>
Date: Mon, 13 Oct 2025 18:51:43 +0100
Subject: [PATCH 3/9] file updates
---
docker-compose.yml | 16 +++++++
pom.xml | 6 +--
src/main/java/com/napier/sem/App.java | 68 +++++++++++++++++----------
3 files changed, 63 insertions(+), 27 deletions(-)
create mode 100644 docker-compose.yml
diff --git a/docker-compose.yml b/docker-compose.yml
new file mode 100644
index 0000000..fb2fe50
--- /dev/null
+++ b/docker-compose.yml
@@ -0,0 +1,16 @@
+version: '3'
+services:
+ app:
+ build: .
+ depends_on:
+ - db
+
+ db:
+ build: db/.
+ restart: always
+ ports:
+ - "3306:3306" # use 3306:3306; 33060 not needed
+ environment:
+ MYSQL_ROOT_PASSWORD: example
+ MYSQL_DATABASE: employees
+ MYSQL_ROOT_HOST: '%'
\ No newline at end of file
diff --git a/pom.xml b/pom.xml
index d930189..ee5e73b 100644
--- a/pom.xml
+++ b/pom.xml
@@ -10,9 +10,9 @@
- org.mongodb
- mongodb-driver
- 3.6.4
+ mysql
+ mysql-connector-java
+ 8.0.18
diff --git a/src/main/java/com/napier/sem/App.java b/src/main/java/com/napier/sem/App.java
index a911232..69b2606 100644
--- a/src/main/java/com/napier/sem/App.java
+++ b/src/main/java/com/napier/sem/App.java
@@ -1,30 +1,50 @@
package com.napier.sem;
-import com.mongodb.MongoClient;
-import com.mongodb.client.MongoDatabase;
-import com.mongodb.client.MongoCollection;
-import org.bson.Document;
+import java.sql.*;
-public class App
-{
- public static void main(String[] args)
- {
- // Connect to MongoDB on local system - we're using port 27000
- MongoClient mongoClient = new MongoClient("mongo-dbserver");
- // Get a database - will create when we use it
- MongoDatabase database = mongoClient.getDatabase("mydb");
- // Get a collection from the database
- MongoCollection collection = database.getCollection("test");
- // Create a document to store
- Document doc = new Document("name", "Kevin Sim")
- .append("class", "DevOps")
- .append("year", "2024")
- .append("result", new Document("CW", 95).append("EX", 85));
- // Add document to collection
- collection.insertOne(doc);
+public class App {
- // Check document in collection
- Document myDoc = collection.find().first();
- System.out.println(myDoc.toJson());
+ public static void main(String[] args) {
+
+ try {
+ // Load Database driver
+ Class.forName("com.mysql.cj.jdbc.Driver");
+ } catch (ClassNotFoundException e) {
+ System.out.println("Could not load SQL driver");
+ System.exit(-1);
+ }
+
+ // Connection to the database
+ Connection con = null;
+ int retries = 100;
+ for (int i = 0; i < retries; ++i) {
+ System.out.println("Connecting to database...");
+ try {
+ // Wait a bit for db to start
+ Thread.sleep(30000);
+ // Connect to database
+ con = DriverManager.getConnection("jdbc:mysql://db:3306/employees?allowPublicKeyRetrieval=true&useSSL=false", "root", "example");
+ System.out.println("Successfully connected");
+ // Wait a bit
+ Thread.sleep(10000);
+ // Exit for loop
+ break;
+ } catch (SQLException sqle) {
+ System.out.println("Failed to connect to database attempt " + Integer.toString(i));
+ System.out.println(sqle.getMessage());
+ } catch (InterruptedException ie) {
+ System.out.println("Thread interrupted? Should not happen.");
+ }
+ }
+
+ if (con != null) {
+ try {
+ // Close connection
+ con.close();
+ } catch (Exception e) {
+ System.out.println("Error closing connection to database");
+ }
+ }
}
+
}
From ca65f2f8a7e7ebbbddd3637d72c598157f720d5a Mon Sep 17 00:00:00 2001
From: Don Sham <40654981@napier.ac.uk>
Date: Tue, 14 Oct 2025 16:32:29 +0100
Subject: [PATCH 4/9] Update main.yml
---
.github/workflows/main.yml | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml
index cf1835a..4eef16c 100644
--- a/.github/workflows/main.yml
+++ b/.github/workflows/main.yml
@@ -18,4 +18,5 @@ jobs:
- name: Build with Maven
run: mvn package
- name: Run docker compose
- run: docker compose up --abort-on-container-exit
+ working directory: ./seMethods
+ run: docker compose up --abort-on-container-exit --build
From d4b140cc7b656d4c70e3948cf1927e26101b2ad8 Mon Sep 17 00:00:00 2001
From: Don Sham <40654981@napier.ac.uk>
Date: Tue, 14 Oct 2025 16:34:14 +0100
Subject: [PATCH 5/9] Update main.yml
---
.github/workflows/main.yml | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml
index 4eef16c..028121f 100644
--- a/.github/workflows/main.yml
+++ b/.github/workflows/main.yml
@@ -18,5 +18,5 @@ jobs:
- name: Build with Maven
run: mvn package
- name: Run docker compose
- working directory: ./seMethods
+ working-directory: ./seMethods
run: docker compose up --abort-on-container-exit --build
From 5922fb08e2629d0059cdffc178afa80104c87765 Mon Sep 17 00:00:00 2001
From: Don Sham <40654981@napier.ac.uk>
Date: Tue, 14 Oct 2025 16:43:23 +0100
Subject: [PATCH 6/9] Update main.yml
---
.github/workflows/main.yml | 1 -
1 file changed, 1 deletion(-)
diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml
index 028121f..2430c52 100644
--- a/.github/workflows/main.yml
+++ b/.github/workflows/main.yml
@@ -18,5 +18,4 @@ jobs:
- name: Build with Maven
run: mvn package
- name: Run docker compose
- working-directory: ./seMethods
run: docker compose up --abort-on-container-exit --build
From 91dd176a4b799517eeeff99b2ba90506e0ac0458 Mon Sep 17 00:00:00 2001
From: Donell Shamuyarira <40654981@napier.ac.uk>
Date: Tue, 14 Oct 2025 19:41:36 +0100
Subject: [PATCH 7/9] ADDING CONNECT AND DISCONNECT FUNCTIONALITY
---
src/main/java/com/napier/sem/App.java | 17 ++++++++++++++++-
1 file changed, 16 insertions(+), 1 deletion(-)
diff --git a/src/main/java/com/napier/sem/App.java b/src/main/java/com/napier/sem/App.java
index 69b2606..5b6cc50 100644
--- a/src/main/java/com/napier/sem/App.java
+++ b/src/main/java/com/napier/sem/App.java
@@ -4,8 +4,23 @@
public class App {
- public static void main(String[] args) {
+ public static void main(String[] args)
+ {
+ // Create new Application
+ App a = new App();
+ // Connect to database
+ a.connect();
+
+ // Disconnect from database
+ a.disconnect();
+ }
+ private Connection con = null;
+
+ public void connect() { /* load driver + DriverManager.getConnection(...) */ }
+
+ public void disconnect() { /* close con if not null */ }
+ {
try {
// Load Database driver
Class.forName("com.mysql.cj.jdbc.Driver");
From 98c77a134c4c3999ba8ce1ea9c8f718f54f27319 Mon Sep 17 00:00:00 2001
From: Donell Shamuyarira <40654981@napier.ac.uk>
Date: Tue, 14 Oct 2025 20:12:07 +0100
Subject: [PATCH 8/9] EMPLOYEE CLASS
---
src/main/java/com/napier/sem/App.java | 71 ++++++++++++++--------
src/main/java/com/napier/sem/Employee.java | 42 +++++++++++++
src/main/java/com/napier/sem/Main.java | 17 ++++++
3 files changed, 104 insertions(+), 26 deletions(-)
create mode 100644 src/main/java/com/napier/sem/Employee.java
create mode 100644 src/main/java/com/napier/sem/Main.java
diff --git a/src/main/java/com/napier/sem/App.java b/src/main/java/com/napier/sem/App.java
index 5b6cc50..35dc4f9 100644
--- a/src/main/java/com/napier/sem/App.java
+++ b/src/main/java/com/napier/sem/App.java
@@ -3,24 +3,41 @@
import java.sql.*;
public class App {
+ // Hold the connection for this instance
+ private Connection con = null;
- public static void main(String[] args)
- {
- // Create new Application
- App a = new App();
+ public static void main(String[] args) {
+ // Create new Application
+ App a = new App();
- // Connect to database
- a.connect();
+ // Connect to database
+ a.connect();
- // Disconnect from database
- a.disconnect();
- }
- private Connection con = null;
+ a.disconnect();
+ }
+ public Employee getEmployee(int id) {
+ try {
+ Statement stmt = con.createStatement();
+ String query =
+ "SELECT emp_no, first_name, last_name " +
+ "FROM employees " +
+ "WHERE emp_no = " + id;
+ ResultSet rs = stmt.executeQuery(query);
+
+ if (!rs.next()) return null;
- public void connect() { /* load driver + DriverManager.getConnection(...) */ }
+ Employee e = new Employee();
+ e.emp_no = rs.getInt("emp_no");
+ e.first_name = rs.getString("first_name");
+ e.last_name = rs.getString("last_name");
+ return e;
+ } catch (SQLException ex) {
+ System.out.println(ex.getMessage());
+ return null;
+ }
+ }
- public void disconnect() { /* close con if not null */ }
- {
+ public void connect() {
try {
// Load Database driver
Class.forName("com.mysql.cj.jdbc.Driver");
@@ -29,37 +46,39 @@ public void disconnect() { /* close con if not null */ }
System.exit(-1);
}
- // Connection to the database
- Connection con = null;
int retries = 100;
for (int i = 0; i < retries; ++i) {
System.out.println("Connecting to database...");
try {
// Wait a bit for db to start
Thread.sleep(30000);
- // Connect to database
- con = DriverManager.getConnection("jdbc:mysql://db:3306/employees?allowPublicKeyRetrieval=true&useSSL=false", "root", "example");
+
+ // Connect to database — NOTE: assign to the FIELD, not a local var
+ con = DriverManager.getConnection(
+ "jdbc:mysql://db:3306/employees?allowPublicKeyRetrieval=true&useSSL=false",
+ "root",
+ "example"
+ );
+
System.out.println("Successfully connected");
- // Wait a bit
- Thread.sleep(10000);
- // Exit for loop
- break;
+ return; // success
} catch (SQLException sqle) {
- System.out.println("Failed to connect to database attempt " + Integer.toString(i));
+ System.out.println("Failed to connect to database attempt " + i);
System.out.println(sqle.getMessage());
} catch (InterruptedException ie) {
System.out.println("Thread interrupted? Should not happen.");
}
}
+ }
+ public void disconnect() {
if (con != null) {
try {
- // Close connection
con.close();
- } catch (Exception e) {
+ System.out.println("Disconnected");
+ } catch (SQLException e) {
System.out.println("Error closing connection to database");
}
}
}
-
-}
+}
\ No newline at end of file
diff --git a/src/main/java/com/napier/sem/Employee.java b/src/main/java/com/napier/sem/Employee.java
new file mode 100644
index 0000000..eff1b9b
--- /dev/null
+++ b/src/main/java/com/napier/sem/Employee.java
@@ -0,0 +1,42 @@
+package com.napier.sem;
+
+/**
+ * Represents an employee
+ */
+public class Employee
+{
+ /**
+ * Employee number
+ */
+ public int emp_no;
+
+ /**
+ * Employee's first name
+ */
+ public String first_name;
+
+ /**
+ * Employee's last name
+ */
+ public String last_name;
+
+ /**
+ * Employee's job title
+ */
+ public String title;
+
+ /**
+ * Employee's salary
+ */
+ public int salary;
+
+ /**
+ * Employee's current department
+ */
+ public String dept_name;
+
+ /**
+ * Employee's manager
+ */
+ public String manager;
+}
\ No newline at end of file
diff --git a/src/main/java/com/napier/sem/Main.java b/src/main/java/com/napier/sem/Main.java
new file mode 100644
index 0000000..76c4c6a
--- /dev/null
+++ b/src/main/java/com/napier/sem/Main.java
@@ -0,0 +1,17 @@
+package com.napier.sem;
+
+//TIP To Run code, press or
+// click the icon in the gutter.
+public class Main {
+ public static void main(String[] args) {
+ //TIP Press with your caret at the highlighted text
+ // to see how IntelliJ IDEA suggests fixing it.
+ System.out.printf("Hello and welcome!");
+
+ for (int i = 1; i <= 5; i++) {
+ //TIP Press to start debugging your code. We have set one breakpoint
+ // for you, but you can always add more by pressing .
+ System.out.println("i = " + i);
+ }
+ }
+}
\ No newline at end of file
From bcd87abc8d35dabf278fa07de7b1bd0a0b14868e Mon Sep 17 00:00:00 2001
From: Don Sham <40654981@napier.ac.uk>
Date: Wed, 15 Oct 2025 01:21:55 +0100
Subject: [PATCH 9/9] Locating DB dockerfile
---
.github/workflows/main.yml | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml
index 2430c52..14a3202 100644
--- a/.github/workflows/main.yml
+++ b/.github/workflows/main.yml
@@ -18,4 +18,4 @@ jobs:
- name: Build with Maven
run: mvn package
- name: Run docker compose
- run: docker compose up --abort-on-container-exit --build
+ run: docker compose -f semGROUP4/docker-compose.yml up --abort-on-container-exit --build