Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 33 additions & 0 deletions .idea/dataSources.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/sqldialects.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,11 @@
<artifactId>junit</artifactId>
<version>4.10</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.18</version>
</dependency>

</dependencies>
</project>
7 changes: 7 additions & 0 deletions src/main/java/daos/CarDTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package daos;

public class CarDTO implements DTO {
public int getid() {
return 0;
}
}
114 changes: 114 additions & 0 deletions src/main/java/daos/CarDao.java
Original file line number Diff line number Diff line change
@@ -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<Car> {


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<Car> findAll() {
Connection connection = ConnectionFactory.getConnection();
try {
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM Car2");
List cars = new ArrayList<Car>();
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;
}
}
30 changes: 30 additions & 0 deletions src/main/java/daos/ConnectionFactory.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
5 changes: 5 additions & 0 deletions src/main/java/daos/DTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package daos;

public interface DTO {
int getid();
}
14 changes: 14 additions & 0 deletions src/main/java/daos/Dao.java
Original file line number Diff line number Diff line change
@@ -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> {
T findCarById(int id);
List<T> findAll();
T update(Car dto);
T create(Car dto);
void delete (int id);
}
Empty file added src/main/java/daos/Main.java
Empty file.
72 changes: 72 additions & 0 deletions src/main/java/models/Car.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
73 changes: 73 additions & 0 deletions src/test/java/daos/CarDaoTest.java
Original file line number Diff line number Diff line change
@@ -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<Car> 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);
List<Car>cars=carDao.findAll();
int actual = cars.size();
carDao.delete(12);
int expected = carDao.findAll().size();
Assert.assertEquals(expected, actual-1);

}
}
Binary file added target/classes/META-INF/jdbcdao.kotlin_module
Binary file not shown.
Binary file added target/classes/daos/CarDTO.class
Binary file not shown.
Binary file added target/classes/daos/CarDao.class
Binary file not shown.
Binary file added target/classes/daos/ConnectionFactory.class
Binary file not shown.
Binary file added target/classes/daos/DTO.class
Binary file not shown.
Binary file added target/classes/daos/Dao.class
Binary file not shown.
Binary file added target/classes/models/Car.class
Binary file not shown.
Binary file added target/test-classes/daos/CarDaoTest.class
Binary file not shown.