diff --git a/.idea/compiler.xml b/.idea/compiler.xml
index f006a55..bce688f 100644
--- a/.idea/compiler.xml
+++ b/.idea/compiler.xml
@@ -6,7 +6,9 @@
+
+
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/runConfigurations.xml b/.idea/runConfigurations.xml
new file mode 100644
index 0000000..797acea
--- /dev/null
+++ b/.idea/runConfigurations.xml
@@ -0,0 +1,10 @@
+
+
+
+
+
+
\ 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..3762cfe 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
- jdbcdao
+ org.example
+ intro-to-jdbc
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/Repo.java b/src/main/java/daos/Repo.java
new file mode 100644
index 0000000..ee9d0c2
--- /dev/null
+++ b/src/main/java/daos/Repo.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 Repo {
+ 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/VehicleRepository.java b/src/main/java/daos/VehicleRepository.java
new file mode 100644
index 0000000..f0674eb
--- /dev/null
+++ b/src/main/java/daos/VehicleRepository.java
@@ -0,0 +1,105 @@
+package daos;
+
+import models.Vehicle;
+
+import java.sql.Connection;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * @author git-leon
+ * @version 1.0.0
+ * @date 8/4/21 3:05 PM
+ */
+public class VehicleRepository implements Repo {
+ private Connection connection;
+
+ public VehicleRepository(Connection connection) {
+ this.connection = connection;
+ }
+
+ @Override
+ public Connection getConnection() {
+ return connection;
+ }
+
+ public void create(Vehicle vehicle) {
+ executeStatement(String.format(new StringBuilder()
+ .append("INSERT INTO model.car(")
+ .append("id, make, model, year, color, vin) ")
+ .append("VALUES (%s, '%s', '%s', %s, '%s', '%s');")
+ .toString(),
+ vehicle.getId(),
+ vehicle.getMake(),
+ vehicle.getModel(),
+ vehicle.getYear(),
+ vehicle.getColor(),
+ vehicle.getVin()));
+ }
+
+ public List readAll() {
+ ResultSet resultSet = executeQuery("SELECT * FROM model.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 Vehicle(
+ Long.parseLong(id),
+ make,
+ model,
+ Integer.parseInt(year),
+ color,
+ vin));
+ }
+ } catch (SQLException throwables) {
+ throw new RuntimeException(throwables);
+ }
+ return list;
+ }
+
+ public Vehicle findById(Long vehicleId) {
+ return readAll()
+ .stream()
+ .filter(pokemon -> pokemon.getId().equals(vehicleId))
+ .findAny()
+ .get();
+ }
+
+ public void update(Long id, Vehicle newCar) {
+ 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 primaryId = %s;")
+ .toString(),
+
+ newCar.getMake(),
+ newCar.getModel(),
+ newCar.getYear(),
+ newCar.getColor(),
+ newCar.getVin(),
+ id));
+ }
+
+ public void delete(Long id) {
+ executeStatement(String.format(new StringBuilder()
+ .append("DELETE FROM car ")
+ .append("WHERE primaryId = %s;")
+ .toString(),
+ id));
+ }
+
+ public void delete(Vehicle car) {
+ delete(car.getId());
+ }
+
+}
diff --git a/src/main/java/main/MainApplication.java b/src/main/java/main/MainApplication.java
new file mode 100644
index 0000000..289b559
--- /dev/null
+++ b/src/main/java/main/MainApplication.java
@@ -0,0 +1,114 @@
+package main;
+
+import com.mysql.cj.jdbc.Driver;
+import daos.VehicleRepository;
+import models.Vehicle;
+import java.sql.*;
+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) {
+ SQLConnector.registerJDBCDriver();
+ Connection mysqlDbConnection = SQLConnector.getConnection("mysql");
+ VehicleRepository vehicleRepository = new VehicleRepository(mysqlDbConnection);
+ executeStatement(mysqlDbConnection, "DROP DATABASE IF EXISTS model;");
+ executeStatement(mysqlDbConnection, "CREATE DATABASE IF NOT EXISTS model;");
+ executeStatement(mysqlDbConnection, "USE model;");
+ executeStatement(mysqlDbConnection, new StringBuilder()
+ .append("CREATE TABLE IF NOT EXISTS model.car(")
+ .append("id int auto_increment primary key,")
+ .append("make text not null,")
+ .append("model text not null,")
+ .append("year int null,")
+ .append("color text not null,")
+ .append("vin text not null);")
+ .toString());
+
+ vehicleRepository.create(new Vehicle(1L, "Tesla", "Model X", 2021, "Matte Black", "1FTFW1EF9DKE31717"));
+ vehicleRepository.create(new Vehicle(2L, "Honda", "CR-V", 1999, "Silver", "1C3AN65L65X036242"));
+ vehicleRepository.create(new Vehicle(3L, "Toyota", "Camry", 2010, "Gold", "4T1BE32K35U037372"));
+ vehicleRepository.create(new Vehicle(4L, "Honda", "Accord", 1996, "Burgundy", "5UXWX9C56E0D36665"));
+ vehicleRepository.create(new Vehicle(5L, "Nissan", "Altima", 2012, "Gray", "1G1AK15F177174588"));
+ vehicleRepository.create(new Vehicle(6L, "Tesla", "Model S", 2020, "Red", "1FDKF37G0VEB13318"));
+ System.out.println(vehicleRepository.readAll());
+
+
+ }
+
+ 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);
+ }
+ }
+}
+
+// static Connection getConnection(String dbVendor) {
+// String username = "Manny";
+// 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);
+// }
+// }
+//
+// static void registerJDBCDriver() {
+// // Attempt to register JDBC Driver
+// try {
+// DriverManager.registerDriver(Driver.class.newInstance());
+// } catch (InstantiationException | IllegalAccessException | SQLException e1) {
+// throw new RuntimeException(e1);
+// }
+// }
+// }
diff --git a/src/main/java/main/SQLConnector.java b/src/main/java/main/SQLConnector.java
new file mode 100644
index 0000000..db0a09e
--- /dev/null
+++ b/src/main/java/main/SQLConnector.java
@@ -0,0 +1,31 @@
+package main;
+
+import com.mysql.cj.jdbc.Driver;
+import java.sql.*;
+
+public class SQLConnector {
+ public static Connection getConnection(String dbVendor) {
+ String username = "Manny";
+ 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);
+ }
+ }
+
+ static void registerJDBCDriver() {
+ // Attempt to register JDBC Driver
+ try {
+ DriverManager.registerDriver(Driver.class.newInstance());
+ } catch (InstantiationException | IllegalAccessException | SQLException e1) {
+ throw new RuntimeException(e1);
+ }
+ }
+}
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/Vehicle.java b/src/main/java/models/Vehicle.java
new file mode 100644
index 0000000..f99aaca
--- /dev/null
+++ b/src/main/java/models/Vehicle.java
@@ -0,0 +1,96 @@
+package models;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+
+/**
+ * @author git-leon
+ * @version 1.0.0
+ * @date 8/4/21 3:03 PM
+ */
+public class Vehicle {
+ private Long id;
+ private String make;
+ private String model;
+ private Integer year;
+ private String color;
+ private String vin;
+
+ public Vehicle() {
+ }
+
+ public Vehicle(Long id, String make, String model, Integer year, String color, String vin) {
+ this.id = id;
+ this.make = make;
+ this.model = model;
+ this.year = year;
+ this.color = color;
+ this.vin = vin;
+ }
+
+ public Long getId() {
+ return id;
+ }
+
+ public void setId(Long 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 Integer getYear() {
+ return year;
+ }
+
+ public void setYear(Integer year) {
+ this.year = year;
+ }
+
+ public String getColor() {
+ return color;
+ }
+
+ public void setColor(String color) {
+ this.color = color;
+ }
+
+ public String getVin() {
+ return vin;
+ }
+
+ public void setVin(String vin) {
+ this.vin = vin;
+ }
+
+ @Override
+ public String toString() {
+ try {
+ return new ObjectMapper().writeValueAsString(this);
+ } catch (JsonProcessingException e) {
+
+ return "Vehicle{" +
+ "id=" + id +
+ ", make='" + make + '\'' +
+ ", model=" + model +
+ ", year=" + year +
+ ", color=" + color +
+ ", vin=" + vin +
+ '}';
+ }
+
+ }
+}
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/RepoTest.java b/src/test/java/daos/RepoTest.java
new file mode 100644
index 0000000..0cdb228
--- /dev/null
+++ b/src/test/java/daos/RepoTest.java
@@ -0,0 +1,43 @@
+package daos;
+
+import models.Vehicle;
+import org.junit.Assert;
+import org.junit.Test;
+
+import java.sql.Connection;
+
+public class RepoTest {
+ @Test
+ public void testCreate() {
+ // Given
+ Vehicle car = new Vehicle(7L, "Tesla", "Model X", 2021, "Matte Black", "1FTFW1EF9DKE31717");
+ String expected = car.toString();
+
+ // When
+ Connection connection = main.SQLConnector.getConnection("mysql");
+ VehicleRepository vehicleRepository = new VehicleRepository(connection);
+ vehicleRepository.create(car);
+ Vehicle car1 = vehicleRepository.findById(7L);
+ String actual = car1.toString();
+
+ // Then
+ Assert.assertEquals(expected, actual);
+ vehicleRepository.delete(7L);
+ }
+
+ @Test
+ public void testFindAll() {
+
+ }
+
+ @Test
+ public void testUpdate() {
+
+ }
+
+ @Test
+ public void testDelete() {
+
+ }
+
+}
diff --git a/target/classes/daos/Repo.class b/target/classes/daos/Repo.class
new file mode 100644
index 0000000..97016f8
Binary files /dev/null and b/target/classes/daos/Repo.class differ
diff --git a/target/classes/daos/VehicleRepository.class b/target/classes/daos/VehicleRepository.class
new file mode 100644
index 0000000..34b8d6a
Binary files /dev/null and b/target/classes/daos/VehicleRepository.class differ
diff --git a/target/classes/main/MainApplication.class b/target/classes/main/MainApplication.class
new file mode 100644
index 0000000..1dbcd56
Binary files /dev/null and b/target/classes/main/MainApplication.class differ
diff --git a/target/classes/main/SQLConnector.class b/target/classes/main/SQLConnector.class
new file mode 100644
index 0000000..7a7346c
Binary files /dev/null and b/target/classes/main/SQLConnector.class differ
diff --git a/target/classes/models/Vehicle.class b/target/classes/models/Vehicle.class
new file mode 100644
index 0000000..e304721
Binary files /dev/null and b/target/classes/models/Vehicle.class differ
diff --git a/target/test-classes/daos/RepoTest.class b/target/test-classes/daos/RepoTest.class
new file mode 100644
index 0000000..f38e221
Binary files /dev/null and b/target/test-classes/daos/RepoTest.class differ