diff --git a/.idea/compiler.xml b/.idea/compiler.xml index f006a55..9067f44 100644 --- a/.idea/compiler.xml +++ b/.idea/compiler.xml @@ -7,6 +7,7 @@ + diff --git a/.idea/dataSources.xml b/.idea/dataSources.xml new file mode 100644 index 0000000..118e7a0 --- /dev/null +++ b/.idea/dataSources.xml @@ -0,0 +1,33 @@ + + + + + mysql.8 + true + com.mysql.cj.jdbc.Driver + jdbc:mysql://localhost:3306 + + + + + + + + + + + mysql.8 + true + com.mysql.cj.jdbc.Driver + jdbc:mysql://localhost:3306 + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/sqldialects.xml b/.idea/sqldialects.xml new file mode 100644 index 0000000..63f176e --- /dev/null +++ b/.idea/sqldialects.xml @@ -0,0 +1,6 @@ + + + + + + \ 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..5f575f6 100644 --- a/pom.xml +++ b/pom.xml @@ -14,5 +14,11 @@ junit 4.10 + + mysql + mysql-connector-java + 8.0.18 + + \ No newline at end of file diff --git a/src/main/java/daos/CarDTO.java b/src/main/java/daos/CarDTO.java new file mode 100644 index 0000000..45c6f25 --- /dev/null +++ b/src/main/java/daos/CarDTO.java @@ -0,0 +1,7 @@ +package daos; + +public class CarDTO implements DTO { + public int getid() { + return 0; + } +} diff --git a/src/main/java/daos/CarDao.java b/src/main/java/daos/CarDao.java new file mode 100644 index 0000000..c2f2be5 --- /dev/null +++ b/src/main/java/daos/CarDao.java @@ -0,0 +1,114 @@ +package daos; + +import com.sun.jdi.connect.Connector; +import models.Car; + +import java.sql.*; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +public class CarDao implements Dao { + + + public Car findCarById(int id) { + Connection connection = ConnectionFactory.getConnection(); + try { + Statement stmt = connection.createStatement(); + ResultSet rs = stmt.executeQuery("SELECT * FROM Car2 WHERE id=" + id); + if (rs.next()) { + return extractCarFromResultSet(rs); + } + } catch (SQLException ex) { + ex.printStackTrace(); + } + return null; + } + + + public List findAll() { + Connection connection = ConnectionFactory.getConnection(); + try { + Statement stmt = connection.createStatement(); + ResultSet rs = stmt.executeQuery("SELECT * FROM Car2"); + List cars = new ArrayList(); + while (rs.next()) { + Car car = extractCarFromResultSet(rs); + cars.add(car); + } + return cars; + } catch (SQLException ex) { + ex.printStackTrace(); + } + return null; + } + + public Car create(Car car) { + Connection connection = ConnectionFactory.getConnection(); + try { + PreparedStatement ps = connection.prepareStatement("INSERT INTO Car2 VALUES ( ?, ?, ?, ?, ?, ?)"); + ps.setInt(1, car.getId()); + ps.setString(2, car.getMake()); + ps.setString(3, car.getModel()); + ps.setInt(4, car.getYear()); + ps.setString(5,car.getVin()); + ps.setString(6,car.getColor()); + int i = ps.executeUpdate(); + if(i == 1) { + return car; + } + } catch (SQLException ex) { + ex.printStackTrace(); + } + return null; + } + + + public Car update(Car car) { + Connection connection = ConnectionFactory.getConnection(); + try { + PreparedStatement ps = connection.prepareStatement("UPDATE Car2 SET make=?, model=?, year=?, vin=?, color=? WHERE id=?"); + + ps.setString(1, car.getMake()); + ps.setString(2, car.getModel()); + ps.setInt(3, car.getYear()); + ps.setString(4,car.getVin()); + ps.setString(5,car.getColor()); + ps.setInt(6, car.getId()); + int i = ps.executeUpdate(); + if(i == 1) { + return car; + } + } catch (SQLException ex) { + ex.printStackTrace(); + } + return null; + } + + + public void delete(int id) { + Connection connection = ConnectionFactory.getConnection(); + try { + Statement stmt = connection.createStatement(); + int i = stmt.executeUpdate("DELETE FROM Car2 WHERE id=" + id); + + + } catch (SQLException ex) { + ex.printStackTrace(); + } + + } + + + private Car extractCarFromResultSet(ResultSet rs) throws SQLException { + Car car = new Car(); + car.setId(rs.getInt("id")); + car.setMake(rs.getString("make")); + car.setModel(rs.getString("model")); + car.setColor(rs.getString("color")); + car.setYear(rs.getInt("year")); + car.setVin(rs.getString("vin")); + return car; + } +} diff --git a/src/main/java/daos/ConnectionFactory.java b/src/main/java/daos/ConnectionFactory.java new file mode 100644 index 0000000..47edd23 --- /dev/null +++ b/src/main/java/daos/ConnectionFactory.java @@ -0,0 +1,30 @@ +package daos; + + + +import com.mysql.cj.jdbc.Driver; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; + +public class ConnectionFactory { + public static final String URL = "jdbc:mysql://localhost:3306/cars"; + public static final String USER = "root"; + public static final String PASS = "zipcoder78"; + /** + * Get a connection to database + * @return Connection object + */ + public static Connection getConnection() { + try { + DriverManager.registerDriver(new Driver()); + return DriverManager.getConnection(URL, USER, PASS); + } catch (SQLException ex) { + throw new RuntimeException("Error connecting to the database", ex); + } + } + public static void main(String[] args) { + Connection connection = ConnectionFactory.getConnection(); + } +} diff --git a/src/main/java/daos/DTO.java b/src/main/java/daos/DTO.java new file mode 100644 index 0000000..dc55796 --- /dev/null +++ b/src/main/java/daos/DTO.java @@ -0,0 +1,5 @@ +package daos; + +public interface DTO { + int getid(); +} diff --git a/src/main/java/daos/Dao.java b/src/main/java/daos/Dao.java new file mode 100644 index 0000000..e0b7d7d --- /dev/null +++ b/src/main/java/daos/Dao.java @@ -0,0 +1,14 @@ +package daos; + +import com.sun.org.apache.xpath.internal.operations.Bool; +import models.Car; + +import java.util.List; + +public interface Dao { + T findCarById(int id); + List findAll(); + T update(Car dto); + T create(Car dto); + void delete (int id); +} diff --git a/src/main/java/daos/Main.java b/src/main/java/daos/Main.java new file mode 100644 index 0000000..e69de29 diff --git a/src/main/java/models/Car.java b/src/main/java/models/Car.java new file mode 100644 index 0000000..4f43aaf --- /dev/null +++ b/src/main/java/models/Car.java @@ -0,0 +1,72 @@ +package models; + +public class Car { + private int id; + private String make; + private String model; + private int year; + private String color; + private String vin; + + + public Car() { + + } + + public Car(int id, String make, String model, int year, String color, String 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 String getVin() { + return vin; + } + + public void setVin(String vin) { + this.vin = vin; + } +} diff --git a/src/test/java/daos/CarDaoTest.java b/src/test/java/daos/CarDaoTest.java new file mode 100644 index 0000000..231cfc2 --- /dev/null +++ b/src/test/java/daos/CarDaoTest.java @@ -0,0 +1,73 @@ +package daos; + +import models.Car; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.List; + +import static org.junit.Assert.*; + +public class CarDaoTest { + private CarDao carDao; + + @Before + public void setUp() throws Exception { + this.carDao = new CarDao(); + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void findCarByIdTest() { + String expectedVin ="WAUVT68E33A130589"; + String actualVin = carDao.findCarById(1).getVin(); + Assert.assertEquals(expectedVin,actualVin); + } + + @Test + public void findAllTest() { + List cars = carDao.findAll(); + Integer actual = cars.size(); + Integer expected = 10; + Assert.assertEquals(expected,actual); + } + + @Test + public void createTest() { + + Car car = new Car (11, "Chevy", "Cobalt", 2017, "Silver", "1LMN70HJI55677Y15"); + Car car2 = carDao.create(car); + int actual = car2.getId(); + int expected = 11; + carDao.delete(11); + Assert.assertEquals(expected,actual); + } + + @Test + public void updateTest() { + Car car = carDao.findCarById(2); + car.setColor("Brown"); + Car car2 = carDao.update(car); + String actual = car2.getColor(); + String expected = "Brown"; + Assert.assertEquals(expected,actual); + } + + @Test + public void deleteTest() { + Car car = new Car (12, "Chevy", "Cobalt", 2017, "Silver", "1LMN70HJI55677Y15"); + Car car2 = carDao.create(car); + Listcars=carDao.findAll(); + int actual = cars.size(); + carDao.delete(12); + int expected = carDao.findAll().size(); + Assert.assertEquals(expected, actual-1); + + } +} \ No newline at end of file diff --git a/target/classes/META-INF/jdbcdao.kotlin_module b/target/classes/META-INF/jdbcdao.kotlin_module new file mode 100644 index 0000000..2983af7 Binary files /dev/null and b/target/classes/META-INF/jdbcdao.kotlin_module differ diff --git a/target/classes/daos/CarDTO.class b/target/classes/daos/CarDTO.class new file mode 100644 index 0000000..3d65c61 Binary files /dev/null and b/target/classes/daos/CarDTO.class differ diff --git a/target/classes/daos/CarDao.class b/target/classes/daos/CarDao.class new file mode 100644 index 0000000..e66981c Binary files /dev/null and b/target/classes/daos/CarDao.class differ diff --git a/target/classes/daos/ConnectionFactory.class b/target/classes/daos/ConnectionFactory.class new file mode 100644 index 0000000..254e4ef Binary files /dev/null and b/target/classes/daos/ConnectionFactory.class differ diff --git a/target/classes/daos/DTO.class b/target/classes/daos/DTO.class new file mode 100644 index 0000000..d615ee5 Binary files /dev/null and b/target/classes/daos/DTO.class differ diff --git a/target/classes/daos/Dao.class b/target/classes/daos/Dao.class new file mode 100644 index 0000000..34c39a2 Binary files /dev/null and b/target/classes/daos/Dao.class differ diff --git a/target/classes/models/Car.class b/target/classes/models/Car.class new file mode 100644 index 0000000..7f8126c Binary files /dev/null and b/target/classes/models/Car.class differ diff --git a/target/test-classes/daos/CarDaoTest.class b/target/test-classes/daos/CarDaoTest.class new file mode 100644 index 0000000..84f0605 Binary files /dev/null and b/target/test-classes/daos/CarDaoTest.class differ