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..4e40a0a 100644
--- a/pom.xml
+++ b/pom.xml
@@ -4,15 +4,39 @@
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
-
-
- junit
- junit
- 4.10
-
-
+
+
+
+ org.apache.maven.plugins
+ maven-compiler-plugin
+ 3.6.0
+
+ 1.8
+ 1.8
+
+
+
+
+
+
+
+ mysql
+ mysql-connector-java
+ 8.0.18
+
+
+ com.fasterxml.jackson.core
+ jackson-core
+ 2.13.0-rc1
+
+
+ com.fasterxml.jackson.core
+ jackson-databind
+ 2.13.0-rc1
+
+
\ 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..e61efcf
--- /dev/null
+++ b/src/main/java/daos/CarRepo.java
@@ -0,0 +1,106 @@
+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;
+
+/**
+ * @author git-leon
+ * @version 1.0.0
+ * @date 8/4/21 3:05 PM
+ */
+public class CarRepo implements Repo {
+ private Connection connection;
+
+ public CarRepo(Connection connection) {
+ this.connection = connection;
+ }
+
+ @Override
+ public Connection getConnection() {
+ return connection;
+ }
+
+ public void create(Car car) {
+ executeStatement(String.format(new StringBuilder()
+ .append("INSERT INTO daolab.vehicle(")
+ .append("primaryId, make, model, color, vtype, mpg) ")
+ .append("VALUES (%s, '%s', '%s', '%s', '%s', %s);")
+ .toString(),
+ car.getId(),
+ car.getMake(),
+ car.getModel(),
+ car.getColor(),
+ car.getvType(),
+ car.getMpg()));
+ }
+
+ public List readAll() {
+ ResultSet resultSet = executeQuery("SELECT * FROM daolab.vehicle;");
+ List list = new ArrayList<>();
+ try {
+ while (resultSet.next()) {
+ String id = resultSet.getString(1);
+ String make = resultSet.getString(2);
+ String model = resultSet.getString(3);
+ String color = resultSet.getString(4);
+ String vtype = resultSet.getString(5);
+ String mpg = resultSet.getString(6);
+ list.add(new Car(
+ Long.parseLong(id),
+ make,
+ model,
+ color,
+ vtype,
+ Integer.parseInt(mpg)));
+ }
+ } catch (SQLException throwables) {
+ throw new RuntimeException(throwables);
+ }
+ return list;
+ }
+
+ public Car read(Long primaryId) {
+ return readAll()
+ .stream()
+ .filter(car -> car.getId().equals(primaryId))
+ .findAny()
+ .get();
+ }
+
+ public void update(Long id, Car newCarData) {
+ executeStatement(String.format(new StringBuilder()
+ .append("UPDATE vehicle SET make= '%s',")
+ .append("model = '%s',")
+ .append("color = '%s',")
+ .append("vtype = '%s',")
+ .append("mpg = %s ")
+ .append("WHERE primaryId = %s;")
+ .toString(),
+
+ newCarData.getMake(),
+ newCarData.getModel(),
+ newCarData.getColor(),
+ newCarData.getvType(),
+ newCarData.getMpg(),
+ id));
+ }
+
+ public void delete(Long id) {
+ executeStatement(String.format(new StringBuilder()
+ .append("DELETE FROM vehicle ")
+ .append("WHERE primaryId = %s;")
+ .toString(),
+ id
+ ));
+ }
+
+ public void delete(Car car) {
+ delete(car.getId());
+ }
+
+}
\ 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/Main.java b/src/main/java/daos/Main.java
new file mode 100644
index 0000000..847dafd
--- /dev/null
+++ b/src/main/java/daos/Main.java
@@ -0,0 +1,116 @@
+package daos;
+
+import com.mysql.cj.jdbc.Driver;
+import models.Car;
+
+import java.sql.Connection;
+import java.sql.DriverManager;
+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
+ */
+class MainApplication {
+
+ public static void main(String[] args) {
+ registerJDBCDriver();
+ Connection mysqlDbConnection = getConnection("mysql");
+ CarRepo carRepo = new CarRepo(mysqlDbConnection);
+ executeStatement(mysqlDbConnection, "DROP DATABASE IF EXISTS daolab;");
+ executeStatement(mysqlDbConnection, "CREATE DATABASE IF NOT EXISTS daolab;");
+ executeStatement(mysqlDbConnection, "USE daolab;");
+ executeStatement(mysqlDbConnection, new StringBuilder()
+ .append("CREATE TABLE IF NOT EXISTS daolab.vehicle(")
+ .append("primaryId int auto_increment primary key,")
+ .append("make text not null,")
+ .append("model text not null,")
+ .append("color text not null,")
+ .append("vtype text not null,")
+ .append("mpg int not null);")
+ .toString());
+
+ Car car = new Car(12L, "Nissan", "Maxima", "Charcoal", "Car", 40 );
+ carRepo.create(car);
+ carRepo.create(new Car(13L, "Ford", "F150", "Blue", "Truck", 22 ));
+ carRepo.update(12L, new Car("Ford", "F150", "Blue", "Truck", 10 ));
+ carRepo.delete(car);
+ System.out.println(carRepo.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 = "carl";
+ String password = "carlpass";
+ 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/Repo.java b/src/main/java/daos/Repo.java
new file mode 100644
index 0000000..9149b37
--- /dev/null
+++ b/src/main/java/daos/Repo.java
@@ -0,0 +1,43 @@
+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();
+}
\ No newline at end of file
diff --git a/src/main/java/models/Car.java b/src/main/java/models/Car.java
new file mode 100644
index 0000000..c8bfb65
--- /dev/null
+++ b/src/main/java/models/Car.java
@@ -0,0 +1,103 @@
+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 Car {
+ private Long id;
+ private String make;
+ private String model;
+ private String color;
+ private String vType;
+ private int mpg;
+
+ public Car() {
+ }
+
+ public Car(String make, String model, String color, String vType, int mpg) {
+ this.id = null;
+ this.make = make;
+ this.model = model;
+ this.color = color;
+ this.vType = vType;
+ this.mpg = mpg;
+ }
+
+ public Car(Long id, String make, String model, String color, String vType, int mpg) {
+ this.id = id;
+ this.make = make;
+ this.model = model;
+ this.color = color;
+ this.vType = vType;
+ this.mpg = mpg;
+ }
+
+ 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 String getColor() {
+ return color;
+ }
+
+ public void setColor(String color) {
+ this.color = color;
+ }
+
+ public String getvType() {
+ return vType;
+ }
+
+ public void setvType(String vType) {
+ this.vType = vType;
+ }
+
+ public int getMpg() {
+ return mpg;
+ }
+
+ public void setMpg(int mpg) {
+ this.mpg = mpg;
+ }
+
+ @Override
+ public String toString() {
+ try {
+ return new ObjectMapper().writeValueAsString(this);
+ } catch (JsonProcessingException e) {
+ return "Pokemon{" +
+ "id=" + id +
+ ", Make='" + make + '\'' +
+ ", Model=" + model +
+ ", Color=" + color +
+ ", VehicleType= " + vType +
+ ", Mpg= " + mpg +
+ '}';
+ }
+ }
+}
\ No newline at end of file
diff --git a/target/classes/daos/Carepo.class b/target/classes/daos/Carepo.class
new file mode 100644
index 0000000..3424cc7
Binary files /dev/null and b/target/classes/daos/Carepo.class differ
diff --git a/target/classes/daos/Carepository.class b/target/classes/daos/Carepository.class
new file mode 100644
index 0000000..bb3e8bd
Binary files /dev/null and b/target/classes/daos/Carepository.class differ
diff --git a/target/classes/daos/Main.class b/target/classes/daos/Main.class
new file mode 100644
index 0000000..2bd002d
Binary files /dev/null and b/target/classes/daos/Main.class differ
diff --git a/target/classes/models/Car.class b/target/classes/models/Car.class
new file mode 100644
index 0000000..e3fc9d2
Binary files /dev/null and b/target/classes/models/Car.class differ