diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index ead8076..14a3202 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 -f semGROUP4/docker-compose.yml up --abort-on-container-exit --build 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 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..35dc4f9 100644 --- a/src/main/java/com/napier/sem/App.java +++ b/src/main/java/com/napier/sem/App.java @@ -1,30 +1,84 @@ package com.napier.sem; -import com.mongodb.MongoClient; -import com.mongodb.client.MongoDatabase; -import com.mongodb.client.MongoCollection; -import org.bson.Document; - -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); - - // Check document in collection - Document myDoc = collection.find().first(); - System.out.println(myDoc.toJson()); +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(); + + // Connect to database + a.connect(); + + 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; + + 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 connect() { + 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); + } + + 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 — 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"); + return; // success + } catch (SQLException sqle) { + 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 { + con.close(); + 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