diff --git a/.idea/compiler.xml b/.idea/compiler.xml index f006a55..c87e502 100644 --- a/.idea/compiler.xml +++ b/.idea/compiler.xml @@ -6,9 +6,11 @@ - + - + + + \ No newline at end of file diff --git a/.idea/dataSources.xml b/.idea/dataSources.xml new file mode 100644 index 0000000..50a0ae1 --- /dev/null +++ b/.idea/dataSources.xml @@ -0,0 +1,19 @@ + + + + + mysql.8 + true + com.mysql.cj.jdbc.Driver + jdbc:mysql://localhost:3306 + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/encodings.xml b/.idea/encodings.xml deleted file mode 100644 index ada92a5..0000000 --- a/.idea/encodings.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml index 803a716..d30d09e 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,6 +1,5 @@ - - - + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..5c77ca2 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ 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/jdbcdao.iml b/jdbcdao.iml new file mode 100644 index 0000000..e42d504 --- /dev/null +++ b/jdbcdao.iml @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml index d97c43d..469b6c0 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/ApplicationRunner.java b/src/main/java/ApplicationRunner.java new file mode 100644 index 0000000..c3a4d3a --- /dev/null +++ b/src/main/java/ApplicationRunner.java @@ -0,0 +1,2 @@ +public class ApplicationRunner { +} diff --git a/src/main/java/CarsDAO.java b/src/main/java/CarsDAO.java new file mode 100644 index 0000000..86cc7f7 --- /dev/null +++ b/src/main/java/CarsDAO.java @@ -0,0 +1,109 @@ +import com.mysql.cj.jdbc.Driver; + +import java.sql.*; +import java.util.ArrayList; +import java.util.List; + +public class CarsDAO implements DAO { + //ConnectionFactory cf = new ConnectionFactory(); + Connection connection = ConnectionFactory.getConnection(); + + + public CarsDTO findById(int id) { + try { + Statement stmt = connection.createStatement(); + ResultSet rs = stmt.executeQuery("SELECT * FROM Cars WHERE id=" + id); + if (rs.next()) { + CarsDTO car = extractUserFromResultSet(rs); + return car; + } + } catch (SQLException ex) { + ex.printStackTrace(); + } + + return null; + } + + + private CarsDTO extractUserFromResultSet(ResultSet rs) throws SQLException { + CarsDTO car = new CarsDTO(); + car.setID(rs.getInt("id")); + car.setMAKE(rs.getString("make")); + car.setMODEL(rs.getString("model")); + car.setYear(rs.getString("year")); + car.setCOLOR(rs.getString("color")); + car.setVIN(rs.getString("vin")); + return car; + } + + + public List findAll() { + + Connection connection = ConnectionFactory.getConnection(); + try { + Statement stmt = connection.createStatement(); + ResultSet rs = stmt.executeQuery("SELECT * FROM Cars"); + List carList = new ArrayList(); + while (rs.next()) { + CarsDTO car = extractUserFromResultSet(rs); + carList.add(car); + } + return carList; + } catch (SQLException ex) { + ex.printStackTrace(); + } + return null; + } + + public CarsDTO update(CarsDTO dto) { + + try { + PreparedStatement ps = connection.prepareStatement("UPDATE Cars SET MAKE=?, MODEL=?, YEAR=?, COLOR=?, VIN=? WHERE ID=?"); + ps.setString(1, dto.getMAKE()); + ps.setString(2, dto.getMODEL()); + ps.setString(3, dto.getYear()); + ps.setString(4, dto.getCOLOR()); + ps.setString(5, dto.getVIN()); + ps.setInt(6, dto.getID()); + int i = ps.executeUpdate(); + if (i == 1) { + return dto; + } + } catch (SQLException ex) { + ex.printStackTrace(); + } + + return null; + } + + public CarsDTO create(CarsDTO dto) { + try { + PreparedStatement ps = connection.prepareStatement("INSERT INTO Cars VALUES (ID=?, MAKE=?, MODEL=?, YEAR=?, COLOR=?, VIN=? "); + ps.setInt(1, dto.getID()); + ps.setString(2, dto.getMAKE()); + ps.setString(3, dto.getMODEL()); + ps.setString(4, dto.getYear()); + ps.setString(5, dto.getCOLOR()); + ps.setString(6, dto.getVIN()); + int i = ps.executeUpdate(); + if (i == 1) { + return dto; + } + } catch (SQLException ex) { + ex.printStackTrace(); + } + + return null; + } + + public void delete(int id) { + try { + Statement stmt = connection.createStatement(); + int i = stmt.executeUpdate("DELETE FROM Cars WHERE id=" + id); + } catch (SQLException ex) { + ex.printStackTrace(); + } + + + } +} diff --git a/src/main/java/CarsDTO.java b/src/main/java/CarsDTO.java new file mode 100644 index 0000000..552e059 --- /dev/null +++ b/src/main/java/CarsDTO.java @@ -0,0 +1,87 @@ + +public class CarsDTO implements DTO { //<------this is Data transfer object class + private Integer ID; + private String MAKE; + private String MODEL; + private String Year; + private String COLOR; + private String VIN; + + public CarsDTO(Integer ID, String MAKE, String MODEL, String year, String COLOR, String VIN) { + this.ID = ID; + this.MAKE = MAKE; + this.MODEL = MODEL; + Year = year; + this.COLOR = COLOR; + this.VIN = VIN; + } + /* + public CarsDTO(String MAKE, String MODEL, String year, String COLOR, String VIN){ + this.MAKE = MAKE; + this.MODEL = MODEL; + Year = year; + this.COLOR = COLOR; + this.VIN = VIN; + } + + */ + + public CarsDTO(){ + } + + public Integer getID() { + return ID; + } + + public String getMAKE() { + return MAKE; + } + + public String getMODEL() { + return MODEL; + } + + public String getYear() { + return Year; + } + + public String getCOLOR() { + return COLOR; + } + + public String getVIN() { + return VIN; + } + + public void setID(Integer ID) { + this.ID = ID; + } + + public void setMAKE(String MAKE) { + this.MAKE = MAKE; + } + + public void setMODEL(String MODEL) { + this.MODEL = MODEL; + } + + public void setYear(String year) { + Year = year; + } + + public void setCOLOR(String COLOR) { + this.COLOR = COLOR; + } + + public void setVIN(String VIN) { + this.VIN = VIN; + } + + public int getId() { + return ID; + } + + public String toString(){ + return String.format("Car={car id = %d, make = %s, model = %s, year = %s, color = %s, vin = %s}",getID(),getMAKE(),getMODEL(),getYear(),getCOLOR(),getVIN()); + } +} diff --git a/src/main/java/ConnectionFactory.java b/src/main/java/ConnectionFactory.java new file mode 100644 index 0000000..2ca8ee4 --- /dev/null +++ b/src/main/java/ConnectionFactory.java @@ -0,0 +1,25 @@ +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/******"; + public static final String USER = "****"; + public static final String PASS = "********"; + + 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); + } + } + + } + + diff --git a/src/main/java/DAO.java b/src/main/java/DAO.java new file mode 100644 index 0000000..9dd6328 --- /dev/null +++ b/src/main/java/DAO.java @@ -0,0 +1,12 @@ +import java.util.List; +public interface DAO { + public X findById(int id); + + public List findAll(); + + public X update(X dto); + + public X create(X dto); + + public void delete(int id); +} \ No newline at end of file diff --git a/src/main/java/DTO.java b/src/main/java/DTO.java new file mode 100644 index 0000000..fde0fcb --- /dev/null +++ b/src/main/java/DTO.java @@ -0,0 +1,3 @@ +public interface DTO { + int getId(); +} diff --git a/src/test/java/TestCarsDAO.java b/src/test/java/TestCarsDAO.java new file mode 100644 index 0000000..86c4fae --- /dev/null +++ b/src/test/java/TestCarsDAO.java @@ -0,0 +1,35 @@ +import com.sun.xml.internal.bind.v2.model.core.ID; +import org.junit.Assert; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.List; + +public class TestCarsDAO { + + @Test + public void testFindById(){ + + CarsDAO c1 = new CarsDAO(); + CarsDTO a1 = c1.findById(1); + CarsDTO e1 = new CarsDTO(1 , "Nissan" , "Frontier" , "2019" , "white" , "1392BR568"); + /* Assert.assertEquals(a1.getID(),e1.getID()); + Assert.assertEquals(a1.getCOLOR(), e1.getCOLOR()); + Assert.assertEquals(a1.getMAKE() , e1.getMAKE()); + Assert.assertEquals(a1.getMODEL() , e1.getMODEL()); + Assert.assertEquals(a1.getYear() , e1.getYear()); + Assert.assertEquals(a1.getVIN() , e1.getVIN());*/ + Assert.assertEquals(a1.toString(),e1.toString()); + } + @Test + public void testFindAll(){ + CarsDAO c1 = new CarsDAO(); + List carList = new ArrayList(); + carList= c1.findAll(); + System.out.println(carList); + } + + + + } + diff --git a/target/classes/ApplicationRunner.class b/target/classes/ApplicationRunner.class new file mode 100644 index 0000000..fd5e825 Binary files /dev/null and b/target/classes/ApplicationRunner.class differ diff --git a/target/classes/CarsDAO.class b/target/classes/CarsDAO.class new file mode 100644 index 0000000..6822817 Binary files /dev/null and b/target/classes/CarsDAO.class differ diff --git a/target/classes/CarsDTO.class b/target/classes/CarsDTO.class new file mode 100644 index 0000000..e7a0724 Binary files /dev/null and b/target/classes/CarsDTO.class differ diff --git a/target/classes/ConnectionFactory.class b/target/classes/ConnectionFactory.class new file mode 100644 index 0000000..a0def2f Binary files /dev/null and b/target/classes/ConnectionFactory.class differ diff --git a/target/classes/DAO.class b/target/classes/DAO.class new file mode 100644 index 0000000..7d2e633 Binary files /dev/null and b/target/classes/DAO.class differ diff --git a/target/classes/DTO.class b/target/classes/DTO.class new file mode 100644 index 0000000..a4d4cef Binary files /dev/null and b/target/classes/DTO.class differ diff --git a/target/jdbcdao-1.0-SNAPSHOT.jar b/target/jdbcdao-1.0-SNAPSHOT.jar new file mode 100644 index 0000000..ec08666 Binary files /dev/null and b/target/jdbcdao-1.0-SNAPSHOT.jar differ diff --git a/target/maven-archiver/pom.properties b/target/maven-archiver/pom.properties new file mode 100644 index 0000000..1e4e974 --- /dev/null +++ b/target/maven-archiver/pom.properties @@ -0,0 +1,5 @@ +#Generated by Maven +#Sat Nov 30 14:14:12 EST 2019 +version=1.0-SNAPSHOT +groupId=com.zipcoder.lab +artifactId=jdbcdao diff --git a/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst b/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst new file mode 100644 index 0000000..e69de29 diff --git a/target/maven-status/maven-compiler-plugin/testCompile/default-testCompile/inputFiles.lst b/target/maven-status/maven-compiler-plugin/testCompile/default-testCompile/inputFiles.lst new file mode 100644 index 0000000..e69de29 diff --git a/target/test-classes/TestCarsDAO.class b/target/test-classes/TestCarsDAO.class new file mode 100644 index 0000000..e3a88e3 Binary files /dev/null and b/target/test-classes/TestCarsDAO.class differ