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.

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.

20 changes: 19 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,26 @@
<groupId>com.zipcoder.lab</groupId>
<artifactId>jdbcdao</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>6</source>
<target>6</target>
</configuration>
</plugin>
</plugins>
</build>

<dependencies>
<dependencies>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.18</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
109 changes: 109 additions & 0 deletions src/main/java/daos/Car_Dao.java
Original file line number Diff line number Diff line change
@@ -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 <Car>{

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;
}

}
33 changes: 33 additions & 0 deletions src/main/java/daos/Car_Dto.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
17 changes: 17 additions & 0 deletions src/main/java/daos/Dao.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package daos;

import java.util.Set;

public interface Dao <T> {
public T findByID(Integer id);

public Set findAll();

public Boolean update(T dto);

public Boolean create(T dto);

public Boolean delete(Integer id);

}

8 changes: 8 additions & 0 deletions src/main/java/daos/Dto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package daos;

import models.Car;

public interface Dto<T>{
int getID (Car object);
}

28 changes: 28 additions & 0 deletions src/main/java/models/Application.java
Original file line number Diff line number Diff line change
@@ -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));

}
}
71 changes: 71 additions & 0 deletions src/main/java/models/Car.java
Original file line number Diff line number Diff line change
@@ -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;
}




}
62 changes: 62 additions & 0 deletions src/main/java/models/ConnectionFactory.java
Original file line number Diff line number Diff line change
@@ -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");
}
}
Empty file removed src/main/java/models/DELETEME.txt
Empty file.
Binary file added target/classes/META-INF/jdbcdao.kotlin_module
Binary file not shown.
Binary file added target/classes/daos/Car_Dao.class
Binary file not shown.
Binary file added target/classes/daos/Car_Dto.class
Binary file not shown.
Binary file added target/classes/daos/Dao.class
Binary file not shown.
Binary file added target/classes/daos/Dto.class
Binary file not shown.
Binary file added target/classes/models/Application.class
Binary file not shown.
Binary file added target/classes/models/Car.class
Binary file not shown.
Binary file added target/classes/models/ConnectionFactory$1.class
Binary file not shown.
Binary file added target/classes/models/ConnectionFactory.class
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -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