Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 9 additions & 11 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "db/test_db"]
path = db/test_db
url = https://github.com/datacharmer/test_db
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -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"]
ENTRYPOINT ["java", "-jar", "seMethods-0.1.0-2-jar-with-dependencies.jar"]

13 changes: 13 additions & 0 deletions db/Dockerfile
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions db/test_db
Submodule test_db added at 3c7fa0
16 changes: 16 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -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: '%'
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@

<dependencies>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver</artifactId>
<version>3.6.4</version>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.18</version>
</dependency>
</dependencies>

Expand Down
108 changes: 81 additions & 27 deletions src/main/java/com/napier/sem/App.java
Original file line number Diff line number Diff line change
@@ -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<Document> 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");
}
}
}
}
}
42 changes: 42 additions & 0 deletions src/main/java/com/napier/sem/Employee.java
Original file line number Diff line number Diff line change
@@ -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;
}
17 changes: 17 additions & 0 deletions src/main/java/com/napier/sem/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.napier.sem;

//TIP To <b>Run</b> code, press <shortcut actionId="Run"/> or
// click the <icon src="AllIcons.Actions.Execute"/> icon in the gutter.
public class Main {
public static void main(String[] args) {
//TIP Press <shortcut actionId="ShowIntentionActions"/> 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 <shortcut actionId="Debug"/> to start debugging your code. We have set one <icon src="AllIcons.Debugger.Db_set_breakpoint"/> breakpoint
// for you, but you can always add more by pressing <shortcut actionId="ToggleLineBreakpoint"/>.
System.out.println("i = " + i);
}
}
}
Loading