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..52ab0aa 100644 --- a/pom.xml +++ b/pom.xml @@ -7,8 +7,26 @@ com.zipcoder.lab jdbcdao 1.0-SNAPSHOT + + + + org.apache.maven.plugins + maven-compiler-plugin + + 6 + 6 + + + + - + + + + mysql + mysql-connector-java + 8.0.18 + junit junit diff --git a/src/main/java/daos/Car_Dao.java b/src/main/java/daos/Car_Dao.java new file mode 100644 index 0000000..42daf5b --- /dev/null +++ b/src/main/java/daos/Car_Dao.java @@ -0,0 +1,109 @@ +package daos; + +import models.Car; +import models.ConnectionFactory; + +import java.sql.*; +import java.util.HashSet; +import java.util.Set; + +public class Car_Dao implements Dao { + + Connection connection = ConnectionFactory.getConnection(); + + public Car extractCarFromSQL(ResultSet rs) throws SQLException { + Car car = new Car(); + + car.setId(((ResultSet) rs).getInt(1)); + car.setMake(((ResultSet) rs).getString("Nissan")); + car.setModel(((ResultSet) rs).getString("Maxima")); + car.setColor(((ResultSet) rs).getString("White")); + car.setYear(((ResultSet) rs).getInt(2017)); + + return car; + } + + + public Car findByID(Integer id) { + try { + Statement stmt = connection.createStatement(); + ResultSet rs = ((Statement) stmt).executeQuery("SELECT * FROM car WHERE id=" + id); + + if (((ResultSet) rs).next()) { + return extractCarFromSQL(rs); + } + } catch (SQLException ex) { + ex.printStackTrace(); + } + return null; + } + + public Set findAll() { + try { + Statement stmt = connection.createStatement(); + ResultSet rs = stmt.executeQuery("SELECT * FROM car"); + + Set carSet = new HashSet(); + + while (rs.next()) { + Car car = extractCarFromSQL(rs); + carSet.add(car); + } + + return carSet; + } catch (SQLException ex) { + ex.printStackTrace(); + } + return null; + } + + public Boolean update(Car car) { + try { + PreparedStatement ps = connection.prepareStatement("UPDATE user SET id=?, make=?, model=? color=?, year=?"); + if (preparedStatement(car, ps)) return true; + } catch (SQLException ex) { + ex.printStackTrace(); + } + return false; + + } + + public Boolean create(Car car) { + try { + PreparedStatement ps = connection.prepareStatement("INSERT INTO user VALUES (?, ?, ?, ?, ?)"); + if (preparedStatement(car, ps)) return true; + } catch (SQLException ex) { + ex.printStackTrace(); + } + return false; + } + + public Boolean delete(Integer id) { + try { + Statement stmt = connection.createStatement(); + int i = stmt.executeUpdate("DELETE FROM videogames WHERE id=" + id); + if (i == 1) { + return true; + } + } catch (SQLException ex) { + ex.printStackTrace(); + } + return false; + } + + + private boolean preparedStatement(Car car, PreparedStatement ps) throws SQLException { + ps.setInt(1, car.getId()); + ps.setString(2, car.getMake()); + ps.setString(3, car.getModel()); + ps.setString(4, car.getColor()); + ps.setInt(5, car.getYear()); + int i = ps.executeUpdate(); + + if (i == 1) { + return true; + } + return false; + } + +} diff --git a/src/main/java/daos/Car_Dto.java b/src/main/java/daos/Car_Dto.java new file mode 100644 index 0000000..2145f3d --- /dev/null +++ b/src/main/java/daos/Car_Dto.java @@ -0,0 +1,33 @@ +package daos; + +import models.Car; +import models.ConnectionFactory; + +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; + +public class Car_Dto implements Dto { + Connection connection = ConnectionFactory.getConnection(); + + public int getID(Car car) { + try { + Statement stmt = connection.createStatement(); + ResultSet rs = stmt.executeQuery("SELECT Id FROM car WHERE Id=" + car.getId()); + if(rs.next()) + { + Car cars = new Car(); + cars.setId( rs.getInt(1) ); + cars.setMake( rs.getString("Nissan") ); + cars.setModel( rs.getString("Maxima") ); + cars.setColor( rs.getString("White") ); + cars.setYear(rs.getInt(2017)); + return cars.getId(); + } + } catch (SQLException sqle) { + sqle.printStackTrace(); + } + return -1; + } +} diff --git a/src/main/java/daos/Dao.java b/src/main/java/daos/Dao.java new file mode 100644 index 0000000..a708933 --- /dev/null +++ b/src/main/java/daos/Dao.java @@ -0,0 +1,17 @@ +package daos; + +import java.util.Set; + +public interface Dao { + public T findByID(Integer id); + + public Set findAll(); + + public Boolean update(T dto); + + public Boolean create(T dto); + + public Boolean delete(Integer id); + +} + diff --git a/src/main/java/daos/Dto.java b/src/main/java/daos/Dto.java new file mode 100644 index 0000000..8906c93 --- /dev/null +++ b/src/main/java/daos/Dto.java @@ -0,0 +1,8 @@ +package daos; + +import models.Car; + +public interface Dto{ + int getID (Car object); +} + diff --git a/src/main/java/models/Application.java b/src/main/java/models/Application.java new file mode 100644 index 0000000..8818358 --- /dev/null +++ b/src/main/java/models/Application.java @@ -0,0 +1,28 @@ +package models; + +import daos.Car_Dao; + +import java.util.Set; + +public class Application { + + public static void main(String[] args) { + Set resultSet; + Car_Dao car_dao = new Car_Dao(); + Car car1 = new Car(1, "Nissan", "Maxima", "White", 2017); + Car car2 = new Car(2, "Subaru", "WRX STI", "White", 2018); + Car car3 = new Car(3, "Chevy", "Corvette", "Orange", 2020); + + car_dao.create(car1); + car_dao.create(car2); + car_dao.create(car3); + + resultSet = car_dao.findAll(); + car_dao.update(car2); + + car_dao.delete(2018); + + System.out.println(car_dao.findByID(2018)); + + } +} diff --git a/src/main/java/models/Car.java b/src/main/java/models/Car.java new file mode 100644 index 0000000..0d12f81 --- /dev/null +++ b/src/main/java/models/Car.java @@ -0,0 +1,71 @@ +package models; + +public class Car { + private Integer id; + private String make; + private String model; + private String color; + private Integer year; + + public Car(){ + } + + public Car(String make, String model, String color, Integer year){ + this.make = make; + this.model = model; + this.color = color; + this.year = year; + } + + public Car(Integer id, String make, String model, String color, Integer year){ + this.id = id; + this.make = make; + this.model = model; + this.color = color; + this.year = year; + } + + public Integer getId() { + return id; + } + + public String getMake() { + return make; + } + + public String getModel() { + return model; + } + + public String getColor() { + return color; + } + + public Integer getYear() { + return year; + } + + 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 setColor(String color) { + this.color = color; + } + + public void setYear(Integer year) { + this.year = year; + } + + + + +} diff --git a/src/main/java/models/ConnectionFactory.java b/src/main/java/models/ConnectionFactory.java new file mode 100644 index 0000000..c3b7188 --- /dev/null +++ b/src/main/java/models/ConnectionFactory.java @@ -0,0 +1,62 @@ +package models; + +import java.sql.*; +import java.util.Properties; +import java.util.logging.Logger; + + +public class ConnectionFactory { + + public static final String URL = "jdbc:mysql://localhost:3306/car"; + public static final String USER = "root"; + public static final String PASSWORD = ""; + + public static Connection getConnection(){ + try { + DriverManager.registerDriver(new Driver() { + @Override + public Connection connect(String url, Properties info) throws SQLException { + return null; + } + + @Override + public boolean acceptsURL(String url) throws SQLException { + return false; + } + + @Override + public DriverPropertyInfo[] getPropertyInfo(String url, Properties info) throws SQLException { + return new DriverPropertyInfo[0]; + } + + @Override + public int getMajorVersion() { + return 0; + } + + @Override + public int getMinorVersion() { + return 0; + } + + @Override + public boolean jdbcCompliant() { + return false; + } + + @Override + public Logger getParentLogger() throws SQLFeatureNotSupportedException { + return null; + } + }); + return DriverManager.getConnection(URL,USER,PASSWORD); + } catch (SQLException ex){ + throw new RuntimeException("Error connecting to database", ex); + } + } + + public static void main(String[] args) { + ConnectionFactory.getConnection(); + System.out.println("Connection Successful"); + } +} 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/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/Car_Dao.class b/target/classes/daos/Car_Dao.class new file mode 100644 index 0000000..c058892 Binary files /dev/null and b/target/classes/daos/Car_Dao.class differ diff --git a/target/classes/daos/Car_Dto.class b/target/classes/daos/Car_Dto.class new file mode 100644 index 0000000..4d360c8 Binary files /dev/null and b/target/classes/daos/Car_Dto.class differ diff --git a/target/classes/daos/Dao.class b/target/classes/daos/Dao.class new file mode 100644 index 0000000..77eb414 Binary files /dev/null and b/target/classes/daos/Dao.class differ diff --git a/target/classes/daos/Dto.class b/target/classes/daos/Dto.class new file mode 100644 index 0000000..d50e2a3 Binary files /dev/null and b/target/classes/daos/Dto.class differ diff --git a/target/classes/models/Application.class b/target/classes/models/Application.class new file mode 100644 index 0000000..8764fd9 Binary files /dev/null and b/target/classes/models/Application.class differ diff --git a/target/classes/models/Car.class b/target/classes/models/Car.class new file mode 100644 index 0000000..c18828b Binary files /dev/null and b/target/classes/models/Car.class differ diff --git a/target/classes/models/ConnectionFactory$1.class b/target/classes/models/ConnectionFactory$1.class new file mode 100644 index 0000000..d11e0d4 Binary files /dev/null and b/target/classes/models/ConnectionFactory$1.class differ diff --git a/target/classes/models/ConnectionFactory.class b/target/classes/models/ConnectionFactory.class new file mode 100644 index 0000000..d0a9d1d Binary files /dev/null and b/target/classes/models/ConnectionFactory.class differ diff --git a/src/main/java/daos/DELETEME.txt b/target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst similarity index 100% rename from src/main/java/daos/DELETEME.txt rename to target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst 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..7769400 --- /dev/null +++ b/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst @@ -0,0 +1,3 @@ +/Users/john/Desktop/Labs/Week8/Maven.JDBC-DAO/src/main/java/Car.java +/Users/john/Desktop/Labs/Week8/Maven.JDBC-DAO/src/main/java/Connection.java +/Users/john/Desktop/Labs/Week8/Maven.JDBC-DAO/src/main/java/UserDao.java