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/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..902662c 100644 --- a/pom.xml +++ b/pom.xml @@ -14,5 +14,14 @@ 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..eed69aa --- /dev/null +++ b/src/main/java/daos/CarDTO.java @@ -0,0 +1,87 @@ +package daos; + +public class CarDTO implements DTO { + Integer carId; + String make; + String model; + Integer year; + String color; + String vin; + + public CarDTO() { + } + + public CarDTO(String make, String model, Integer year, String color, String vin) { + this.make = make; + this.model = model; + this.year = year; + this.color = color; + this.vin = vin; + } + + public CarDTO(Integer carId, String make, String model, Integer year, String color, String vin) { + this.carId = carId; + this.make = make; + this.model = model; + this.year = year; + this.color = color; + this.vin = vin; + } + + public Integer getCarId() { + return carId; + } + + public void setCarId(Integer carId) { + this.carId = carId; + } + + 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; + } + + public Integer getId() { + return null; + } + + @Override + public String toString() { + return String.format("Car ID: %d, Make: %s, Model: %s, Year: %d, Color: %s, VIN: %s", carId, make, model, year, color, vin); + } +} diff --git a/src/main/java/daos/ConnectToDB.java b/src/main/java/daos/ConnectToDB.java new file mode 100644 index 0000000..9d4f98e --- /dev/null +++ b/src/main/java/daos/ConnectToDB.java @@ -0,0 +1,33 @@ +package daos; + +import com.mysql.cj.jdbc.Driver; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; + +public class ConnectToDB { + public static final String URL = "jdbc:mysql://localhost:3306/DavesDB"; + public static final String USER = "root"; + public static final String PASS = "ZipCode5.2"; + +/** + * 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); + } +} + + /** + * Test Connection + */ + public static void main(String[] args) { + Connection connection = ConnectToDB.getConnection(); + } +} diff --git a/src/main/java/daos/DAO.java b/src/main/java/daos/DAO.java new file mode 100644 index 0000000..eb2a426 --- /dev/null +++ b/src/main/java/daos/DAO.java @@ -0,0 +1,17 @@ +package daos; + +import java.util.List; + +public interface DAO { + + T findById(Integer id); + + List findAll(); + + String update (T dto); + + String create (T dto); + + Boolean delete (Integer id); + +} diff --git a/src/main/java/daos/DAOConcrete.java b/src/main/java/daos/DAOConcrete.java new file mode 100644 index 0000000..a657596 --- /dev/null +++ b/src/main/java/daos/DAOConcrete.java @@ -0,0 +1,119 @@ +package daos; + +import java.sql.*; +import java.util.ArrayList; +import java.util.List; + +public class DAOConcrete implements DAO { //This class does CRUD operations + + public CarDTO findById(Integer id) { + Connection connection = ConnectToDB.getConnection(); + + try { + Statement stmt = connection.createStatement(); + ResultSet rs = stmt.executeQuery("Select * from car where carid=" + id); + if (rs.next()){ + return extractUserFromResultSet(rs); + } + } catch (SQLException e) { + e.printStackTrace(); + } + return null; + } + + public List findAll() { + Connection connection = ConnectToDB.getConnection(); + + try { + Statement stmt = connection.createStatement(); + ResultSet rs = stmt.executeQuery("Select * from car"); + List carsList = new ArrayList(); + + while (rs.next()){ + + CarDTO car = extractUserFromResultSet(rs); + carsList.add(car); + } + return carsList; + } catch (SQLException e) { + e.printStackTrace(); + } + return null; + } + + public String update(CarDTO dto) { + Connection connection = ConnectToDB.getConnection(); + + try { + PreparedStatement ps = connection.prepareStatement("update car set make=?, model=?, yearbuilt=?, color =?, vin=? where carid=?"); + + ps.setString(1, dto.getMake()); + ps.setString(2, dto.getModel()); + ps.setInt(3, dto.getYear()); + ps.setString(4, dto.getColor()); + ps.setString(5, dto.getVin()); + ps.setInt(6, dto.getCarId()); + int i = ps.executeUpdate(); + + if (i == 1) { + return dto.toString(); + } + } catch (SQLException e) { + e.printStackTrace(); + } + return ""; + } + + public String create (CarDTO dto) { + Connection connection = ConnectToDB.getConnection(); + + try { + PreparedStatement ps = connection.prepareStatement("Insert into car values(?, ?, ?, ?, ?, ?)"); + ps.setInt(1, dto.getCarId()); + ps.setString(2, dto.getMake()); + ps.setString(3, dto.getModel()); + ps.setInt(4, dto.getYear()); + ps.setString(5, dto.getColor()); + ps.setString(6, dto.getVin()); + int i = ps.executeUpdate(); + + if (i == 1) { + return dto.toString(); + //return dto; + } + + } catch (SQLException e) { + e.printStackTrace(); + } + return ""; + } + + public Boolean delete(Integer id) { + Connection connection = ConnectToDB.getConnection(); + + try{ + Statement stmt = connection.createStatement(); + int i = stmt.executeUpdate(("delete from car where carid=" + id)); + + if(i >= 1) { + return true; + } + } catch (SQLException e) { + e.printStackTrace(); + } + return false; + } + + private CarDTO extractUserFromResultSet (ResultSet rs) throws SQLException { + CarDTO myCar = new CarDTO(); + + myCar.setCarId(rs.getInt("carid")); + myCar.setMake((rs.getString("make"))); + myCar.setModel((rs.getString("model"))); + myCar.setYear(rs.getInt("yearbuilt")); + myCar.setColor((rs.getString("color"))); + myCar.setVin((rs.getString("vin"))); + + return myCar; + } +} 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..1d4a81a --- /dev/null +++ b/src/main/java/daos/DTO.java @@ -0,0 +1,5 @@ +package daos; + +public interface DTO { + Integer getId (); +} 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/CarDTOTest.java b/src/test/java/daos/CarDTOTest.java new file mode 100644 index 0000000..1d068af --- /dev/null +++ b/src/test/java/daos/CarDTOTest.java @@ -0,0 +1,57 @@ +package daos; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class CarDTOTest { + DAOConcrete daoConcrete; + CarDTO car1, car2, car3; + + @Before + public void setUp() throws Exception { + daoConcrete = new DAOConcrete(); + car1 = new CarDTO(6, "Oldsmobile", "Cutlass",1987, "Black", "1987OC"); + car2 = new CarDTO(5, "Pontiac", "Firebird", 1981, "Yellow", "1981PF"); + car3 = new CarDTO(2, "Pontiac", "Lemans", 1969, "Gold", "1969PL"); + } + + @Test + public void testFindById () { + String expected = "Car ID: 6, Make: Oldsmobile, Model: Cutlass, Year: 1987, Color: Black, VIN: 1987OC"; + String actual = daoConcrete.findById(6).toString(); + Assert.assertEquals(expected, actual); + } + + @Test + public void testFindAll () { + Integer expected = 6; + Integer actual = daoConcrete.findAll().size(); + Assert.assertEquals(expected, actual); + } + + + @Test + public void testCreate () { + String expected = "Car ID: 7, Make: Toyota, Model: Echo, Year: 2001, Color: Green, VIN: 2001TE"; + CarDTO carToCreate = new CarDTO(7, "Toyota", "Echo", 2001, "Green", "2001TE"); + String actual = daoConcrete.create(carToCreate); + Assert.assertEquals(expected, actual); + } + + @Test + public void testUpdate () { + String expected = "Car ID: 7, Make: Toyota, Model: Echo, Year: 2001, Color: Green Patina, VIN: 2001TE"; + CarDTO carToModify = new CarDTO(7, "Toyota", "Echo", 2001, "Green Patina", "2001TE"); + String actual = daoConcrete.update(carToModify); + Assert.assertEquals(expected, actual); + } + + @Test + public void testDelete () { + Boolean expected = true; + Boolean actual = daoConcrete.delete(7); + Assert.assertEquals(expected, actual); + } + +} diff --git a/src/test/java/daos/ConnectToDBTest.java b/src/test/java/daos/ConnectToDBTest.java new file mode 100644 index 0000000..00af3ff --- /dev/null +++ b/src/test/java/daos/ConnectToDBTest.java @@ -0,0 +1,17 @@ +package daos; + +import org.junit.Assert; +import org.junit.Test; + +import java.sql.Connection; +import java.sql.SQLException; + +public class ConnectToDBTest { + + + @Test + public void getConnection() throws SQLException { + Connection connect = ConnectToDB.getConnection(); + Assert.assertFalse(connect.isClosed()); + } +} diff --git a/src/test/java/daos/DELETEME.txt b/src/test/java/daos/DELETEME.txt deleted file mode 100644 index e69de29..0000000