diff --git a/.idea/compiler.xml b/.idea/compiler.xml
index f006a55..160fa1b 100644
--- a/.idea/compiler.xml
+++ b/.idea/compiler.xml
@@ -6,9 +6,15 @@
+
+
+
+
\ No newline at end of file
diff --git a/.idea/encodings.xml b/.idea/encodings.xml
index ada92a5..860a155 100644
--- a/.idea/encodings.xml
+++ b/.idea/encodings.xml
@@ -1,6 +1,7 @@
-
+
+
\ No newline at end of file
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..63fd6bf 100644
--- a/pom.xml
+++ b/pom.xml
@@ -4,15 +4,41 @@
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
- jdbcdao
+ org.example
+ intro-to-jdbc
1.0-SNAPSHOT
+
+
+
+ org.apache.maven.plugins
+ maven-compiler-plugin
+ 3.6.0
+
+ 1.8
+ 1.8
+
+
+
+
+
+
+
+
+ com.fasterxml.jackson.core
+ jackson-databind
+ 2.12.3
+
+
+ mysql
+ mysql-connector-java
+ 8.0.18
+
+
+ junit
+ junit
+ 4.12
+ test
+
+
+
-
-
- junit
- junit
- 4.10
-
-
-
\ No newline at end of file
diff --git a/src/main/java/ConnectionFactory.java b/src/main/java/ConnectionFactory.java
new file mode 100644
index 0000000..e145505
--- /dev/null
+++ b/src/main/java/ConnectionFactory.java
@@ -0,0 +1,59 @@
+import com.mysql.cj.jdbc.exceptions.SQLError;
+import com.mysql.cj.jdbc.Driver;
+import java.sql.*;
+
+public class ConnectionFactory {
+//
+// public static void main(String[] args) {
+// getConnection();
+//
+// }
+//
+// public static final String URL = "jdbc:mysql://localhost:3306/zcwdblab?serverTimezone=UTC";
+// public static final String USER = "nick";
+// public static final String PASS = "password";
+//
+// public static Connection getConnection() {
+// try {
+// DriverManager.registerDriver(new Driver());
+// return DriverManager.getConnection(URL, USER, PASS);
+// } catch (SQLException e) {
+// throw new RuntimeException("ERROR : Can't connect to the database", e);
+// }
+// }
+
+
+
+
+
+
+
+ static void registerJDBCDriver() {
+ // Attempt to register JDBC Driver
+ try {
+ DriverManager.registerDriver(Driver.class.newInstance());
+ } catch (InstantiationException | IllegalAccessException | SQLException e1) {
+ throw new Error();
+ }
+ }
+
+ 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);
+ }
+ }
+
+ static void executeStatement(Connection connection, String sqlStatement) {
+ try {
+ Statement statement = getScrollableStatement(connection);
+ statement.execute(sqlStatement);
+ connection.commit();
+ } catch (SQLException e) {
+ throw new Error(e);
+ }
+ }
+}
diff --git a/src/main/java/MainApplication.java b/src/main/java/MainApplication.java
new file mode 100644
index 0000000..4b53f1f
--- /dev/null
+++ b/src/main/java/MainApplication.java
@@ -0,0 +1,115 @@
+
+import com.mysql.cj.jdbc.Driver;
+import daos.CarRepo;
+import models.Car;
+
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
+
+/**
+ * @author git-leon
+ * @version 1.0.0
+ * @date 8/2/21 9:49 AM
+ */
+public class MainApplication {
+
+ public static void main(String[] args) {
+ registerJDBCDriver();
+ Connection mysqlDbConnection = getConnection("mysql");
+ CarRepo carRepo = new CarRepo<>(mysqlDbConnection);
+ executeStatement(mysqlDbConnection, "DROP DATABASE IF EXISTS zcwdblabs;");
+ executeStatement(mysqlDbConnection, "CREATE DATABASE IF NOT EXISTS zcwdblabs;");
+ executeStatement(mysqlDbConnection, "USE zcwdblabs;");
+ executeStatement(mysqlDbConnection, new StringBuilder()
+ .append("CREATE TABLE IF NOT EXISTS car(")
+ .append("id int auto_increment primary key,")
+ .append("make varchar(255) not null,")
+ .append("model varchar(255) not null,")
+ .append("year int not null,")
+ .append("color varchar(255),")
+ .append("vin int not null);")
+ .toString());
+
+ carRepo.create(new Car(4, "Ford", "Ranger", 1999, "White", 491883));
+ carRepo.create(new Car(14, "jeep", "wrangler", 2017, "rhinogrey", 52513));
+ carRepo.update(14, new Car("Klondike", "Bar", 2018, "wigglenuts", 1451235));
+ System.out.println(carRepo.findAll());
+
+// executeStatement(mysqlDbConnection, new StringBuilder()
+// .append("INSERT INTO zcwdblabs.car(")
+// .append("id, name, primary_type, secondary_type) ")
+// .append("VALUES (12, 'Ivysaur', 3, 7);")
+// .toString());
+//
+// executeStatement(mysqlDbConnection, new StringBuilder()
+// .append("INSERT INTO zcwdblabs.car(")
+// .append("id, name, primary_type, secondary_type) ")
+// .append("VALUES (13, 'Ivysaurr', 3, 7);")
+// .toString());
+
+// String getPokemonTable = "SELECT * FROM zcwdblabs.car;";
+// ResultSet resultSet = executeQuery(mysqlDbConnection, getPokemonTable);
+// printResults(resultSet);
+//
+// String showPokemonTable = "SHOW DATABASES;";
+// resultSet = executeQuery(mysqlDbConnection, showPokemonTable);
+// printResults(resultSet);
+ }
+
+ 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 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);
+ }
+ }
+
+ static Connection getConnection(String dbVendor) {
+ String username = "nick";
+ String password = "password";
+ 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);
+ }
+ }
+
+ static void registerJDBCDriver() {
+ // Attempt to register JDBC Driver
+ try {
+ DriverManager.registerDriver(Driver.class.newInstance());
+ } catch (InstantiationException | IllegalAccessException | SQLException e1) {
+ throw new RuntimeException(e1);
+ }
+ }
+
+}
\ No newline at end of file
diff --git a/src/main/java/daos/CarRepo.java b/src/main/java/daos/CarRepo.java
new file mode 100644
index 0000000..93ba05f
--- /dev/null
+++ b/src/main/java/daos/CarRepo.java
@@ -0,0 +1,95 @@
+package daos;
+
+import models.Car;
+
+import java.sql.Connection;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.ArrayList;
+import java.util.List;
+
+public class CarRepo implements DAO, ConnectionInterface {
+
+ private Connection connection;
+
+ public CarRepo(Connection connection) {
+ this.connection = connection;
+ }
+
+ public T findById(int id) {
+ return (T) findAll()
+ .stream()
+ .filter(car -> car.getId() == (id))
+ .findAny()
+ .get();
+ }
+
+ public List findAll() {
+ ResultSet resultSet = executeQuery("SELECT * FROM zcwdblabs.car;");
+ List list = new ArrayList<>();
+ try {
+ while (resultSet.next()) {
+ String id = resultSet.getString(1);
+ String make = resultSet.getString(2);
+ String model = resultSet.getString(3);
+ String year = resultSet.getString(4);
+ String color = resultSet.getString(5);
+ String vin = resultSet.getString(6);
+ list.add(new Car(
+ Integer.parseInt(id),
+ make,
+ model,
+ Integer.parseInt(year),
+ color,
+ Integer.parseInt(vin)));
+ }
+ } catch (SQLException throwables) {
+ throw new RuntimeException(throwables);
+ }
+ return list;
+ }
+
+ public void update(int carId, Car dataToUpdate) {
+ executeStatement(String.format(new StringBuilder()
+ .append("UPDATE car SET make = '%s',")
+ .append("model = '%s',")
+ .append("year = %s,")
+ .append("color = '%s',")
+ .append("vin = %s ")
+ .append("WHERE id = %s;")
+ .toString(),
+ dataToUpdate.getMake(),
+ dataToUpdate.getModel(),
+ dataToUpdate.getYear(),
+ dataToUpdate.getColor(),
+ dataToUpdate.getVin(),
+ carId));
+ }
+
+ public void create(T dataToCreate) {
+ executeStatement(String.format(new StringBuilder()
+ .append("INSERT INTO car(")
+ .append("id, make, model, year, color, vin) ")
+ .append("VALUES (%s, '%s', '%s', %s, '%s', %s);")
+ .toString(),
+ dataToCreate.getId(),
+ dataToCreate.getMake(),
+ dataToCreate.getModel(),
+ dataToCreate.getYear(),
+ dataToCreate.getColor(),
+ dataToCreate.getVin()));
+ }
+
+ public void delete(int id) {
+ executeStatement(String.format(new StringBuilder()
+ .append("DELETE FROM zcwdblabs.car WHERE id = %s")
+ .toString(),
+ id));
+ }
+
+
+ @Override
+ public Connection getConnection() {
+ return connection;
+ }
+}
diff --git a/src/main/java/daos/ConnectionInterface.java b/src/main/java/daos/ConnectionInterface.java
new file mode 100644
index 0000000..fa2dd6a
--- /dev/null
+++ b/src/main/java/daos/ConnectionInterface.java
@@ -0,0 +1,39 @@
+package daos;
+
+import java.sql.Connection;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
+
+public interface ConnectionInterface {
+ 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/daos/DAO.java b/src/main/java/daos/DAO.java
new file mode 100644
index 0000000..1930700
--- /dev/null
+++ b/src/main/java/daos/DAO.java
@@ -0,0 +1,18 @@
+package daos;
+
+import models.Car;
+
+import java.util.List;
+
+public interface DAO {
+
+ public T findById(int id);
+
+ public List findAll();
+
+ public void update(int carId, Car dataToUpdate);
+
+ public void create(T dataToCreate);
+
+ public void delete(int id);
+}
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/DTO.java b/src/main/java/daos/DTO.java
new file mode 100644
index 0000000..01c8a78
--- /dev/null
+++ b/src/main/java/daos/DTO.java
@@ -0,0 +1,6 @@
+package daos;
+
+public interface DTO {
+
+ int getId();
+}
diff --git a/src/main/java/daos/DataTransferObject.java b/src/main/java/daos/DataTransferObject.java
new file mode 100644
index 0000000..2207c4b
--- /dev/null
+++ b/src/main/java/daos/DataTransferObject.java
@@ -0,0 +1,8 @@
+package daos;
+
+public class DataTransferObject implements DTO {
+
+ public int getId() {
+ return 0;
+ }
+}
diff --git a/src/main/java/models/Car.java b/src/main/java/models/Car.java
new file mode 100644
index 0000000..ba4664a
--- /dev/null
+++ b/src/main/java/models/Car.java
@@ -0,0 +1,101 @@
+package models;
+
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+
+public class Car {
+
+ private int id;
+ private String make;
+ private String model;
+ private int year;
+ private String color;
+ private int vin;
+
+
+ //null construct
+ public Car() {
+ }
+
+ // construct w/o ID just for updating
+ public Car(String make, String model, int year, String color, int vin) {
+ this.make = make;
+ this.model = model;
+ this.year = year;
+ this.color = color;
+ this.vin = vin;
+ }
+
+ //full construct
+ public Car(int id, String make, String model, int year, String color, int vin) {
+ this.id = id;
+ this.make = make;
+ this.model = model;
+ this.year = year;
+ this.color = color;
+ this.vin = vin;
+ }
+
+
+
+ public int getId() {
+ return id;
+ }
+
+ public void setId(int id) {
+ this.id = id;
+ }
+
+ public String getMake() {
+ return make;
+ }
+
+ public void setMake(String make) {
+ this.make = make;
+ }
+
+ public String getModel() {
+ return model;
+ }
+
+ public void setModel(String model) {
+ this.model = model;
+ }
+
+ public int getYear() {
+ return year;
+ }
+
+ public void setYear(int year) {
+ this.year = year;
+ }
+
+ public String getColor() {
+ return color;
+ }
+
+ public void setColor(String color) {
+ this.color = color;
+ }
+
+ public int getVin() {
+ return vin;
+ }
+
+ public void setVin(int vin) {
+ this.vin = vin;
+ }
+
+ @Override
+ public String toString() {
+ return "Car{" +
+ "id=" + id +
+ ", make='" + make + '\'' +
+ ", model='" + model + '\'' +
+ ", year=" + year +
+ ", color='" + color + '\'' +
+ ", vin=" + vin +
+ '}';
+ }
+}
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/test/java/daos/CarRepoTest.java b/src/test/java/daos/CarRepoTest.java
new file mode 100644
index 0000000..b6964ca
--- /dev/null
+++ b/src/test/java/daos/CarRepoTest.java
@@ -0,0 +1,33 @@
+package daos;
+
+import models.Car;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.sql.Connection;
+
+public class CarRepoTest implements ConnectionInterface {
+
+ private Connection connection;
+
+ @Test
+ public void createDOATest () {
+// CarRepo carRepo = new CarRepo();
+// Integer expected = 2;
+// Car car = new Car();
+// Car car1 = new Car();
+// Car car2 = new Car();
+// carRepo.create(car);
+// carRepo.create(car1);
+// carRepo.create(car2);
+// Integer actual = carRepo.findAll().size();
+//
+// Assert.assertEquals(expected, actual);
+ }
+
+ @Override
+ public Connection getConnection() {
+ return connection;
+ }
+}
diff --git a/src/test/java/models/CarTest.java b/src/test/java/models/CarTest.java
new file mode 100644
index 0000000..e35ec68
--- /dev/null
+++ b/src/test/java/models/CarTest.java
@@ -0,0 +1,60 @@
+package models;
+
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+public class CarTest {
+
+
+ @Test
+ public void nullaryConstructorTest() {
+ Car car = new Car();
+ Integer expected = 0;
+
+ Integer actual = car.getId();
+
+ Assert.assertEquals(expected, actual);
+ }
+
+ @Test
+ public void constructorWithoutIDTest() {
+ Car car = new Car("'LilBabyMobile'", "asdf",
+ 41, "ca", 14);
+ String expectedMake = "'LilBabyMobile'";
+ Integer expectedId = 0;
+
+ String actualMake = car.getMake();
+ Integer actualId = car.getId();
+
+ Assert.assertEquals(expectedMake, actualMake);
+ Assert.assertEquals(expectedId, actualId);
+ }
+
+ @Test
+ public void fullConstructorTest() {
+ Car car = new Car(10, "Toyoda", "primeski",
+ 12, "yellow", 123 );
+ Integer expectedId = 10;
+ String expectedMake = "Toyoda";
+ String expectedModel = "primeski";
+ Integer expectedYear = 12;
+ String expectedColor = "yellow";
+ Integer expectedVin = 123;
+
+ Integer actualId = car.getId();
+ String actualMake = car.getMake();
+ String actualModel = car.getModel();
+ Integer actualYear = car.getYear();
+ String actualColor = car.getColor();
+ Integer actualVin = car.getVin();
+
+ Assert.assertEquals(expectedId, actualId);
+ Assert.assertEquals(expectedMake, actualMake);
+ Assert.assertEquals(expectedModel, actualModel);
+ Assert.assertEquals(expectedYear, actualYear);
+ Assert.assertEquals(expectedColor, actualColor);
+ Assert.assertEquals(expectedVin, actualVin);
+ }
+}