diff --git a/.idea/compiler.xml b/.idea/compiler.xml
index f006a55..5a76f28 100644
--- a/.idea/compiler.xml
+++ b/.idea/compiler.xml
@@ -6,6 +6,7 @@
+
diff --git a/.idea/jarRepositories.xml b/.idea/jarRepositories.xml
new file mode 100644
index 0000000..712ab9d
--- /dev/null
+++ b/.idea/jarRepositories.xml
@@ -0,0 +1,20 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/vcs.xml b/.idea/vcs.xml
new file mode 100644
index 0000000..35eb1dd
--- /dev/null
+++ b/.idea/vcs.xml
@@ -0,0 +1,6 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/pom.xml b/pom.xml
index d97c43d..5a25858 100644
--- a/pom.xml
+++ b/pom.xml
@@ -4,15 +4,40 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
- com.zipcoder.lab
+ org.example
jdbcdao
1.0-SNAPSHOT
+
+
+
+ org.apache.maven.plugins
+ maven-compiler-plugin
+ 3.6.0
+
+ 1.8
+ 1.8
+
+
+
+
-
-
- junit
- junit
- 4.10
-
-
+
+
+
+ com.fasterxml.jackson.core
+ jackson-databind
+ 2.12.3
+
+
+ mysql
+ mysql-connector-java
+ 8.0.18
+
+
+ junit
+ junit
+ RELEASE
+ test
+
+
\ No newline at end of file
diff --git a/src/main/java/daos/DELETEME.txt b/src/main/java/daos/DELETEME.txt
deleted file mode 100644
index e69de29..0000000
diff --git a/src/main/java/daos/PotionRepository.java b/src/main/java/daos/PotionRepository.java
new file mode 100644
index 0000000..d27abe7
--- /dev/null
+++ b/src/main/java/daos/PotionRepository.java
@@ -0,0 +1,93 @@
+package daos;
+
+import models.Potion;
+
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.ArrayList;
+import java.util.List;
+
+public class PotionRepository implements Repository{
+
+ private Connection connection;
+
+ public PotionRepository(Connection connection) {
+ this.connection = connection;
+ }
+ public Connection getConnection() {
+ return connection;
+ }
+
+ public void create(Potion potion) {
+ executeStatement(String.format(new StringBuilder()
+ .append("INSERT INTO potions.potionsTable(")
+ .append("id, name, ingredient1, ingredient2, effect)")
+ .append("VALUES (%s, '%s', '%s', '%s', '%s');")
+ .toString(),
+ potion.getId(),
+ potion.getName(),
+ potion.getIngredient1(),
+ potion.getIngredient2(),
+ potion.getEffect()));
+ }
+
+ public List findAll() {
+ ResultSet resultSet = executeQuery("SELECT * FROM potions.potionsTable;");
+ List list = new ArrayList<>();
+ try {
+ while (resultSet.next()) {
+ String id = resultSet.getString(1);
+ String name = resultSet.getString(2);
+ String ingredient1 = resultSet.getString(3);
+ String ingredient2 = resultSet.getString(4);
+ String effect = resultSet.getString(5);
+ list.add(new Potion(
+ Long.parseLong(id),
+ name,
+ ingredient1,
+ ingredient2,
+ effect));
+
+ }
+ } catch (SQLException throwables) {
+ throw new RuntimeException(throwables);
+ }
+ return list;
+ }
+
+ public Potion findById(Long id) {
+ return findAll()
+ .stream()
+ .filter(potion -> potion.getId().equals(id))
+ .findAny()
+ .get();
+ }
+
+ public void update(Long id, Potion newPotionData) {
+ executeStatement(String.format(new StringBuilder()
+ .append("UPDATE potions.potionsTable ")
+ .append("SET name = '%s', ingredient1 = '%s', ingredient2 = '%s', effect = '%s' WHERE id = %s;")
+ .toString(),
+ newPotionData.getName(),
+ newPotionData.getIngredient1(),
+ newPotionData.getIngredient2(),
+ newPotionData.getEffect(),
+ id));
+ }
+
+ public void delete(Long id) {
+ Potion potion = findById(id);
+ executeStatement(String.format(new StringBuilder()
+ .append("DELETE FROM potions.potionsTable WHERE id = %s")
+ .toString(),
+ id));
+ System.out.printf("%s has been deleted", potion.toString());
+ }
+
+ public void delete(Potion potion) {
+ Long id = potion.getId();
+ delete(id);
+ }
+}
diff --git a/src/main/java/daos/Repository.java b/src/main/java/daos/Repository.java
new file mode 100644
index 0000000..7671c5b
--- /dev/null
+++ b/src/main/java/daos/Repository.java
@@ -0,0 +1,44 @@
+package daos;
+
+import java.sql.Connection;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
+
+/**
+ * @author git-leon
+ * @version 1.0.0
+ * @date 8/4/21 3:15 PM
+ */
+public interface Repository {
+
+ default void executeStatement(String sqlStatement) {
+ try {
+ Statement statement = getScrollableStatement();
+ statement.execute(sqlStatement);
+ } catch (SQLException e) {
+ throw new Error(e);
+ }
+ }
+
+ default Statement getScrollableStatement() {
+ int resultSetType = ResultSet.TYPE_SCROLL_INSENSITIVE;
+ int resultSetConcurrency = ResultSet.CONCUR_READ_ONLY;
+ try { // scrollable statements can be iterated more than once without closing
+ return getConnection().createStatement(resultSetType, resultSetConcurrency);
+ } catch (SQLException e) {
+ throw new Error(e);
+ }
+ }
+
+ default ResultSet executeQuery(String sqlQuery) {
+ try {
+ Statement statement = getScrollableStatement();
+ return statement.executeQuery(sqlQuery);
+ } catch (SQLException e) {
+ throw new Error(e);
+ }
+ }
+
+ Connection getConnection();
+}
diff --git a/src/main/java/main/MainApplication.java b/src/main/java/main/MainApplication.java
new file mode 100644
index 0000000..064785b
--- /dev/null
+++ b/src/main/java/main/MainApplication.java
@@ -0,0 +1,85 @@
+package main;
+
+import daos.PotionRepository;
+import models.Potion;
+
+import java.sql.Connection;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.util.StringJoiner;
+
+/**
+ * @author git-leon
+ * @version 1.0.0
+ * @date 8/2/21 9:49 AM
+ */
+public class MainApplication {
+
+ public static void main(String[] args) {
+ SQLConnect.registerJDBCDriver();
+ Connection mysqlDbConnection = SQLConnect.getConnection("mysql");
+ PotionRepository pokemonRepository = new PotionRepository(mysqlDbConnection);
+ executeStatement(mysqlDbConnection, "DROP DATABASE IF EXISTS potions;");
+ executeStatement(mysqlDbConnection, "CREATE DATABASE IF NOT EXISTS potions;");
+ executeStatement(mysqlDbConnection, "USE potions;");
+ executeStatement(mysqlDbConnection, new StringBuilder()
+ .append("CREATE TABLE IF NOT EXISTS potions.potionsTable(")
+ .append("id int auto_increment primary key,")
+ .append("name text not null,")
+ .append("ingredient1 text not null,")
+ .append("ingredient2 text not null,")
+ .append("effect text not null);")
+ .toString());
+
+ pokemonRepository.create(new Potion(1L, "Restore Health", "blue mountain flower", "daedra heart", "health regen"));
+ pokemonRepository.create(new Potion(2L, "Restore Magicka", "briar heart", "red mountain flower", "magicka regen"));
+ System.out.println(pokemonRepository.findAll());
+
+ }
+
+ static ResultSet executeQuery(Connection connection, String sqlQuery) {
+ try {
+ Statement statement = getScrollableStatement(connection);
+ return statement.executeQuery(sqlQuery);
+ } catch (SQLException e) {
+ throw new Error(e);
+ }
+ }
+
+ static void printResults(ResultSet resultSet) {
+ try {
+ for (int rowNumber = 0; resultSet.next(); rowNumber++) {
+ String firstColumnData = resultSet.getString(1);
+ String secondColumnData = resultSet.getString(2);
+ String thirdColumnData = resultSet.getString(3);
+ System.out.println(new StringJoiner("\n")
+ .add("Row number = " + rowNumber)
+ .add("First Column = " + firstColumnData)
+ .add("Second Column = " + secondColumnData)
+ .add("Third column = " + thirdColumnData));
+ }
+ } catch (SQLException e) {
+ throw new Error(e);
+ }
+ }
+
+ static void executeStatement(Connection connection, String sqlStatement) {
+ try {
+ Statement statement = getScrollableStatement(connection);
+ statement.execute(sqlStatement);
+ } catch (SQLException e) {
+ throw new Error(e);
+ }
+ }
+
+ static Statement getScrollableStatement(Connection connection) {
+ int resultSetType = ResultSet.TYPE_SCROLL_INSENSITIVE;
+ int resultSetConcurrency = ResultSet.CONCUR_READ_ONLY;
+ try { // scrollable statements can be iterated more than once without closing
+ return connection.createStatement(resultSetType, resultSetConcurrency);
+ } catch (SQLException e) {
+ throw new Error(e);
+ }
+ }
+}
\ No newline at end of file
diff --git a/src/main/java/main/SQLConnect.java b/src/main/java/main/SQLConnect.java
new file mode 100644
index 0000000..c5748d4
--- /dev/null
+++ b/src/main/java/main/SQLConnect.java
@@ -0,0 +1,34 @@
+package main;
+
+import com.mysql.cj.jdbc.Driver;
+
+import java.sql.*;
+import java.util.StringJoiner;
+
+public class SQLConnect {
+
+ static void registerJDBCDriver() {
+ // Attempt to register JDBC Driver
+ try {
+ DriverManager.registerDriver(Driver.class.newInstance());
+ } catch (InstantiationException | IllegalAccessException | SQLException e1) {
+ throw new RuntimeException(e1);
+ }
+ }
+
+ public static Connection getConnection(String dbVendor) {
+ String username = "jen";
+ String password = "zipcode0";
+ String url = new StringBuilder()
+ .append("jdbc:")
+ .append(dbVendor)
+ .append("://127.0.0.1/")
+ .append("?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC")
+ .toString();
+ try {
+ return DriverManager.getConnection(url, username, password);
+ } catch (SQLException e) {
+ throw new Error(e);
+ }
+ }
+}
\ No newline at end of file
diff --git a/src/main/java/models/DELETEME.txt b/src/main/java/models/DELETEME.txt
deleted file mode 100644
index e69de29..0000000
diff --git a/src/main/java/models/Potion.java b/src/main/java/models/Potion.java
new file mode 100644
index 0000000..e090d99
--- /dev/null
+++ b/src/main/java/models/Potion.java
@@ -0,0 +1,74 @@
+package models;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+
+public class Potion {
+
+ private Long id;
+ private String name;
+ private String ingredient1;
+ private String ingredient2;
+ private String effect;
+
+ public Potion() {}
+
+ public Potion(Long id, String name, String ingredient1, String ingredient2, String effect) {
+ this.id = id;
+ this.name = name;
+ this.ingredient1 = ingredient1;
+ this.ingredient2 = ingredient2;
+ this.effect = effect;
+ }
+
+ public Long getId() {
+ return id;
+ }
+
+ public void setId(Long id) {
+ this.id = id;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ public String getIngredient1() {
+ return ingredient1;
+ }
+
+ public void setIngredient1(String ingredient1) {
+ this.ingredient1 = ingredient1;
+ }
+
+ public String getIngredient2() {
+ return ingredient2;
+ }
+
+ public void setIngredient2(String ingredient2) {
+ this.ingredient2 = ingredient2;
+ }
+
+ public String getEffect() {
+ return effect;
+ }
+
+ public void setEffect(String effect) {
+ this.effect = effect;
+ }
+
+ @Override
+ public String toString() {
+ return "Potion {" +
+ "ID = " + id +
+ ", Name = " + name +
+ ", First Ingredient = " + ingredient1 +
+ ", Second Ingredient = " + ingredient2 +
+ ", Effects = " + effect +
+ "}";
+ }
+}
diff --git a/src/test/java/daos/DELETEME.txt b/src/test/java/daos/DELETEME.txt
deleted file mode 100644
index e69de29..0000000
diff --git a/src/test/java/daos/PotionRepositoryTest.java b/src/test/java/daos/PotionRepositoryTest.java
new file mode 100644
index 0000000..4351639
--- /dev/null
+++ b/src/test/java/daos/PotionRepositoryTest.java
@@ -0,0 +1,123 @@
+package daos;
+
+import models.Potion;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.sql.Connection;
+import java.util.ArrayList;
+import java.util.List;
+
+
+public class PotionRepositoryTest {
+
+
+ @Test
+ public void testCreate() {
+ //given
+ Potion potion = new Potion(1L, "Restore Health", "blue mountain flower", "daedra heart", "health regen");
+ String expected = potion.toString();
+ //when
+ Connection connection = main.SQLConnect.getConnection("mysql");
+ PotionRepository potionRepository = new PotionRepository(connection);
+ potionRepository.create(potion);
+ Potion potion1 = potionRepository.findById(1L);
+ String actual = potion1.toString();
+ //then
+ Assert.assertEquals(expected, actual);
+ potionRepository.delete(1L);
+ }
+
+ @Test
+ public void testFindAll() {
+ //given
+ Potion potion1 = new Potion(1L, "Restore Health", "blue mountain flower", "daedra heart", "health regen");
+ Potion potion2 = new Potion(2L, "Restore Magicka", "briar heart", "red mountain flower", "magicka regen");
+ List list = new ArrayList<>();
+ list.add(potion1);
+ list.add(potion2);
+ String expected = list.toString();
+ //when
+ Connection connection = main.SQLConnect.getConnection("mysql");
+ PotionRepository potionRepository = new PotionRepository(connection);
+ potionRepository.create(potion1);
+ potionRepository.create(potion2);
+ String actual = potionRepository.findAll().toString();
+ //then
+ Assert.assertEquals(expected, actual);
+ potionRepository.delete(1L);
+ potionRepository.delete(2L);
+ }
+
+ @Test
+ public void testFindById() {
+ //given
+ Potion potion = new Potion(1L, "Restore Health", "blue mountain flower", "daedra heart", "health regen");
+ String expected = potion.toString();
+ //when
+ Connection connection = main.SQLConnect.getConnection("mysql");
+ PotionRepository potionRepository = new PotionRepository(connection);
+ potionRepository.create(potion);
+ Potion potion1 = potionRepository.findById(1L);
+ String actual = potion1.toString();
+ //then
+ Assert.assertEquals(expected, actual);
+ potionRepository.delete(1L);
+
+ }
+
+ @Test
+ public void testUpdate() {
+ //given
+ Potion potion = new Potion(1L, "Restore Health", "blue mountain flower", "daedra heart", "health regen");
+ Potion newPotion = new Potion(1L, "Restore Magicka", "briar heart", "red mountain flower", "magicka regen");
+ String expected = newPotion.toString();
+ //when
+ Connection connection = main.SQLConnect.getConnection("mysql");
+ PotionRepository potionRepository = new PotionRepository(connection);
+ potionRepository.create(potion);
+ potionRepository.update(1L, newPotion);
+ Potion potion1 = potionRepository.findById(1L);
+ String actual = potion1.toString();
+ //then
+ Assert.assertEquals(expected, actual);
+ potionRepository.delete(1L);
+ }
+
+ @Test
+ public void testDeleteByID() {
+ //given
+ Potion potion1 = new Potion(1L, "Restore Health", "blue mountain flower", "daedra heart", "health regen");
+ Potion potion2 = new Potion(2L, "Restore Magicka", "briar heart", "red mountain flower", "magicka regen");
+ int expected = 1;
+ //when
+ Connection connection = main.SQLConnect.getConnection("mysql");
+ PotionRepository potionRepository = new PotionRepository(connection);
+ potionRepository.create(potion1);
+ potionRepository.create(potion2);
+ potionRepository.delete(2L);
+ int actual = potionRepository.findAll().size();
+ //then
+ Assert.assertEquals(expected, actual);
+ potionRepository.delete(1L);
+ }
+
+ @Test
+ public void testDeleteByObject() {
+ //given
+ Potion potion1 = new Potion(1L, "Restore Health", "blue mountain flower", "daedra heart", "health regen");
+ Potion potion2 = new Potion(2L, "Restore Magicka", "briar heart", "red mountain flower", "magicka regen");
+ int expected = 1;
+ //when
+ Connection connection = main.SQLConnect.getConnection("mysql");
+ PotionRepository potionRepository = new PotionRepository(connection);
+ potionRepository.create(potion1);
+ potionRepository.create(potion2);
+ potionRepository.delete(potion2);
+ int actual = potionRepository.findAll().size();
+ //then
+ Assert.assertEquals(expected, actual);
+ potionRepository.delete(1L);
+ }
+}
diff --git a/src/test/java/models/DELETEME.txt b/src/test/java/models/DELETEME.txt
deleted file mode 100644
index e69de29..0000000
diff --git a/src/test/java/models/PotionTest.java b/src/test/java/models/PotionTest.java
new file mode 100644
index 0000000..e5a315d
--- /dev/null
+++ b/src/test/java/models/PotionTest.java
@@ -0,0 +1,158 @@
+package models;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+
+public class PotionTest {
+
+ @Test
+ public void testConstructor() {
+ //given
+ Long expectedId = 1L;
+ String expectedName = "Restore Magicka";
+ String expectedIngredient1 = "briar heart";
+ String expectedIngredient2 = "red mountain flower";
+ String expectedEffect = "magicka regen";
+ //when
+ Potion potion = new Potion(expectedId, expectedName, expectedIngredient1, expectedIngredient2, expectedEffect);
+ Long actualID = potion.getId();
+ String actualName = potion.getName();
+ String actualIngredient1 = potion.getIngredient1();
+ String actualIngredient2 = potion.getIngredient2();
+ String actualEffect = potion.getEffect();
+ //then
+ Assert.assertEquals(expectedId, actualID);
+ Assert.assertEquals(expectedName, actualName);
+ Assert.assertEquals(expectedIngredient1, actualIngredient1);
+ Assert.assertEquals(expectedIngredient2, actualIngredient2);
+ Assert.assertEquals(expectedEffect, actualEffect);
+
+ }
+
+ @Test
+ public void testGetId() {
+ //given
+ Long expected = 3L;
+ //when
+ Potion potion = new Potion(expected, "Restore Magicka", "briar heart", "red mountain flower", "magicka regen");
+ Long actual = potion.getId();
+ //then
+ Assert.assertEquals(expected, actual);
+ }
+
+ @Test
+ public void testSetId() {
+ //given
+ Long expected = 2L;
+ //when
+ Potion potion = new Potion();
+ potion.setId(expected);
+ Long actual = potion.getId();
+ //then
+ Assert.assertEquals(expected, actual);
+ }
+
+ @Test
+ public void testGetName() {
+ //given
+ String expected = "Restore Magicka";
+ //when
+ Potion potion = new Potion(1L, expected, "briar heart", "red mountain flower", "magicka regen");
+ String actual = potion.getName();
+ //then
+ Assert.assertEquals(expected, actual);
+ }
+
+ @Test
+ public void testSetName() {
+ //given
+ String expected = "Restore Magicka";
+ //when
+ Potion potion = new Potion();
+ potion.setName(expected);
+ String actual = potion.getName();
+ //then
+ Assert.assertEquals(expected, actual);
+ }
+
+ @Test
+ public void testGetIngredient1() {
+ //given
+ String expected = "briar heart";
+ //when
+ Potion potion = new Potion(1L, "Restore Magicka", expected, "red mountain flower", "magicka regen");
+ String actual = potion.getIngredient1();
+ //then
+ Assert.assertEquals(expected, actual);
+ }
+
+ @Test
+ public void testSetIngredient1() {
+ //given
+ String expected = "briar heart";
+ //when
+ Potion potion = new Potion();
+ potion.setIngredient1(expected);
+ String actual = potion.getIngredient1();
+ //then
+ Assert.assertEquals(expected, actual);
+ }
+
+ @Test
+ public void testGetIngredient2() {
+ //given
+ String expected = "red mountain flower";
+ //when
+ Potion potion = new Potion(1L, "Restore Magicka", "briar heart", expected, "magicka regen");
+ String actual = potion.getIngredient2();
+ //then
+ Assert.assertEquals(expected, actual);
+ }
+
+ @Test
+ public void testSetIngredient2() {
+ //given
+ String expected = "red mountain flower";
+ //when
+ Potion potion = new Potion();
+ potion.setIngredient2(expected);
+ String actual = potion.getIngredient2();
+ //then
+ Assert.assertEquals(expected, actual);
+ }
+
+ @Test
+ public void testGetEffect() {
+ //given
+ String expected = "magicka regen";
+ //when
+ Potion potion = new Potion(1L, "Restore Magicka", "briar heart", "red mountain flower", expected);
+ String actual = potion.getEffect();
+ //then
+ Assert.assertEquals(expected, actual);
+ }
+
+ @Test
+ public void testSetEffect() {
+ //given
+ String expected = "magicka regen";
+ //when
+ Potion potion = new Potion();
+ potion.setEffect(expected);
+ String actual = potion.getEffect();
+ //then
+ Assert.assertEquals(expected, actual);
+ }
+
+ @Test
+ public void testToString() {
+ //given
+ String expected = "Potion {ID = 1, Name = Restore Magicka, First Ingredient = briar heart, Second Ingredient = red mountain flower, Effects = magicka regen}";
+ //when
+ Potion potion = new Potion(1L, "Restore Magicka", "briar heart", "red mountain flower", "magicka regen");
+ String actual = potion.toString();
+ //then
+ Assert.assertEquals(expected, actual);
+ }
+}
\ No newline at end of file
diff --git a/target/classes/daos/PotionRepository.class b/target/classes/daos/PotionRepository.class
new file mode 100644
index 0000000..e688909
Binary files /dev/null and b/target/classes/daos/PotionRepository.class differ
diff --git a/target/classes/daos/Repository.class b/target/classes/daos/Repository.class
new file mode 100644
index 0000000..33e27b4
Binary files /dev/null and b/target/classes/daos/Repository.class differ
diff --git a/target/classes/main/MainApplication.class b/target/classes/main/MainApplication.class
new file mode 100644
index 0000000..fa1bd0d
Binary files /dev/null and b/target/classes/main/MainApplication.class differ
diff --git a/target/classes/main/SQLConnect.class b/target/classes/main/SQLConnect.class
new file mode 100644
index 0000000..d5d2bbc
Binary files /dev/null and b/target/classes/main/SQLConnect.class differ
diff --git a/target/classes/models/Potion.class b/target/classes/models/Potion.class
new file mode 100644
index 0000000..7d4db25
Binary files /dev/null and b/target/classes/models/Potion.class differ
diff --git a/target/test-classes/daos/PotionRepositoryTest.class b/target/test-classes/daos/PotionRepositoryTest.class
new file mode 100644
index 0000000..dd32fa3
Binary files /dev/null and b/target/test-classes/daos/PotionRepositoryTest.class differ
diff --git a/target/test-classes/models/PotionTest.class b/target/test-classes/models/PotionTest.class
new file mode 100644
index 0000000..3290931
Binary files /dev/null and b/target/test-classes/models/PotionTest.class differ