diff --git a/.idea/Maven.JDBC-DAO.iml b/.idea/Maven.JDBC-DAO.iml new file mode 100644 index 0000000..78b2cc5 --- /dev/null +++ b/.idea/Maven.JDBC-DAO.iml @@ -0,0 +1,2 @@ + + \ No newline at end of file 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..82e15cc 100644 --- a/pom.xml +++ b/pom.xml @@ -7,12 +7,35 @@ com.zipcoder.lab jdbcdao 1.0-SNAPSHOT + + + + org.apache.maven.plugins + maven-compiler-plugin + + 8 + 8 + + + + + + + + mysql + mysql-connector-java + 8.0.26 + - junit junit 4.10 - + + com.fasterxml.jackson.core + jackson-databind + 2.13.0-rc1 + + \ No newline at end of file diff --git a/src/main/java/MainApplication.java b/src/main/java/MainApplication.java new file mode 100644 index 0000000..ca437bf --- /dev/null +++ b/src/main/java/MainApplication.java @@ -0,0 +1,115 @@ + +import com.mysql.cj.jdbc.Driver; +import daos.CarRepository; +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; + +public class MainApplication { + + public static void main(String[] args) { + registerJDBCDriver(); + Connection mysqlDbConnection = getConnection("mysql"); + CarRepository carRepository = new CarRepository(mysqlDbConnection); + executeStatement(mysqlDbConnection, "DROP DATABASE IF EXISTS carDatabase;"); + executeStatement(mysqlDbConnection, "CREATE DATABASE IF NOT EXISTS carDatabase;"); + executeStatement(mysqlDbConnection, "USE carDatabase;"); + executeStatement(mysqlDbConnection, new StringBuilder() + .append("CREATE TABLE IF NOT EXISTS carDatabase.carTable(") + .append("id int auto_increment primary key,") + .append("make text not null,") + .append("model text not null,") + .append("year int,") + .append("color text,") + .append("vin int);") + .toString()); + + carRepository.create(new Car(1L,"honda", "accord", 1995, "black", 100L)); +// carRepository.create(new Car()); + System.out.println(carRepository.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); + String fourthColumnData = resultSet.getString(4); + String fifthColumnData = resultSet.getString(5); + String sixthColumnData = resultSet.getString(6); + System.out.println(new StringJoiner("\n") + .add("Row number = " + rowNumber) + .add("First Column = " + firstColumnData) + .add("Second Column = " + secondColumnData) + .add("Third column = " + thirdColumnData) + .add("Fourth Column = " + fourthColumnData) + .add("Fifth Column = " + fifthColumnData) + .add("Sixth Column = " + sixthColumnData)); + } + } 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 = "root"; + 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/CarRepository.java b/src/main/java/daos/CarRepository.java new file mode 100644 index 0000000..e4ed8d2 --- /dev/null +++ b/src/main/java/daos/CarRepository.java @@ -0,0 +1,103 @@ +package daos; + +import com.sun.deploy.net.MessageHeader; +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 CarRepository implements Repo { + private Connection connection; + + public CarRepository(Connection connection) { + this.connection = connection; + } + + @Override + public Connection getConnection() { + return connection; + } + + public void create(Car car) { + executeStatement(String.format(new StringBuilder() + .append("INSERT INTO carDatabase.carTable(") + .append("id, make, model, year, color, vin) ") + .append("VALUES (%s, '%s', '%s', %s, '%s', %s);") + .toString(), + car.getId(), + car.getMake(), + car.getModel(), + car.getYear(), + car.getColor(), + car.getVin())); + + } + + public List readAll() { + ResultSet resultSet = executeQuery("SELECT * FROM carDatabase.carTable;"); + 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( + Long.parseLong(id), + make, + model, + Integer.parseInt(year), + color, + Long.parseLong(vin))); + } + } catch (SQLException throwables) { + throw new RuntimeException(throwables); + } + return list; + } + + public Car read(Long Carid) { + return readAll() + .stream() + .filter(car -> car.getId().equals(Carid)) + .findAny() + .get(); + } + + public void update(Long id, Car newPokemonData) { + executeStatement(String.format(new StringBuilder() + .append("UPDATE carTable,") + .append("SET make '%s',") + .append("model '%s',") + .append("year %s,") + .append("color '%s',") + .append("vin %s,") + .append("WHERE id = %s;") + .toString(), + newPokemonData.getMake(), + newPokemonData.getModel(), + newPokemonData.getYear(), + newPokemonData.getColor(), + newPokemonData.getVin(), + id)); + + } + + public void delete(Long id) { + executeStatement(String.format(new StringBuilder() + .append("DELETE FROM carTable WHERE id = %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/Repo.java b/src/main/java/daos/Repo.java new file mode 100644 index 0000000..e71a42f --- /dev/null +++ b/src/main/java/daos/Repo.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 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..5af467a --- /dev/null +++ b/src/main/java/models/Car.java @@ -0,0 +1,84 @@ +package models; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; + +public class Car { + private Long id; + private String make; + private String model; + private Integer year; + private String color; + private Long vin; + + public Car (){}; + + public Car(Long id, String make, String model, Integer year, String color, Long 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 Long getVin() { + return vin; + } + + public void setVin(Long vin) { + this.vin = vin; + } + + @Override + public String toString() { + return "Car{" + + "id=" + id + + ", make='" + make + '\'' + + ", model='" + model + '\'' + + ", year=" + year + + ", color='" + color + '\'' + + ", vin=" + vin + + '}'; + } +} \ 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/test/java/daos/TestClass.java b/src/test/java/daos/TestClass.java new file mode 100644 index 0000000..9d9fc70 --- /dev/null +++ b/src/test/java/daos/TestClass.java @@ -0,0 +1,26 @@ +package daos; + +import org.junit.Test; + +public class TestClass { + @Test + public void readAllTest(){ + + } + @Test + public void readTest(){ + + } + @Test + public void update(){ + + } + @Test + public void deleteById(){ + + } + @Test + public void deleteByObject(){ + + } +} diff --git a/target/classes/MainApplication.class b/target/classes/MainApplication.class new file mode 100644 index 0000000..2104bb6 Binary files /dev/null and b/target/classes/MainApplication.class differ diff --git a/target/classes/daos/CarRepository.class b/target/classes/daos/CarRepository.class new file mode 100644 index 0000000..ea7a802 Binary files /dev/null and b/target/classes/daos/CarRepository.class differ diff --git a/target/classes/daos/Repo.class b/target/classes/daos/Repo.class new file mode 100644 index 0000000..e4870e3 Binary files /dev/null and b/target/classes/daos/Repo.class differ diff --git a/target/classes/models/Car.class b/target/classes/models/Car.class new file mode 100644 index 0000000..105beea Binary files /dev/null and b/target/classes/models/Car.class differ