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..d02efa6 100644 --- a/pom.xml +++ b/pom.xml @@ -12,7 +12,16 @@ junit junit - 4.10 + 4.12 + + mysql + mysql-connector-java + 8.0.18 + + + + + \ No newline at end of file diff --git a/src/main/java/daos/Car.java b/src/main/java/daos/Car.java new file mode 100644 index 0000000..30a2f06 --- /dev/null +++ b/src/main/java/daos/Car.java @@ -0,0 +1,82 @@ +package daos; + +public class Car { + + + private Integer id; + private String make; + private String model; + private Integer year; + private String color; + private String vin; + + public Car(){ + + } + + public Car(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 Car(Integer id, String make, String model, Integer year, String color, String vin) { + this.id = id; + this.make = make; + this.model = model; + this.year = year; + this.color = color; + this.vin = vin; + } + + public Integer getId() { + return id; + } + + public String getMake() { + return make; + } + + public String getModel() { + return model; + } + + public Integer 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(Integer year) { + this.year = year; + } + + public void setColor(String color) { + this.color = color; + } + + public void setVin(String vin) { + this.vin = vin; + } +} + diff --git a/src/main/java/daos/ConnectionToDatabase.java b/src/main/java/daos/ConnectionToDatabase.java new file mode 100644 index 0000000..ae13f82 --- /dev/null +++ b/src/main/java/daos/ConnectionToDatabase.java @@ -0,0 +1,195 @@ +package daos; + +//import com.mysql.jdbc.Driver; +//import com.sun.jdi.connect.Connector; + +import java.sql.*; +import java.util.HashSet; +import java.util.Set; + + +public class ConnectionToDatabase { + + private static final String URL = "jdbc:mysql://localhost:5432/cars"; + private static final String USER = "root"; + private static final String PASS = "root"; + + private static Connection con; + private static Statement stmt; + private static ResultSet rs; + + public static ConnectionToDatabase connectionToDatabase = new ConnectionToDatabase(); + + + + 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 = connectionToDatabase.getConnection(); + } + + + public static void javaToPostgres() { + String query = "select count(*) from cars"; + + try { + con = DriverManager.getConnection(URL, USER, PASS); + + stmt = con.createStatement(); + + rs = stmt.executeQuery(query); + + while (rs.next()) { + int count = rs.getInt(1); + System.out.println("Total number of cars in the table : " + count); + } + + } catch (SQLException sqlEx) { + sqlEx.printStackTrace(); + } finally { + + try { + con.close(); + } catch (SQLException se) { + } + try { + stmt.close(); + } catch (SQLException se) { + } + try { + rs.close(); + } catch (SQLException se) { + } + } + } + + private Car extractUserFromResultSet(ResultSet rs) throws SQLException { + Car car = new Car(); + car.setId(rs.getInt("id")); + car.setMake(rs.getString("make")); + car.setModel(rs.getString("model")); + car.setYear(rs.getInt("year")); + car.setColor(rs.getString("color")); + car.setVin(rs.getString("vin")); + return car; + } + + public Car getCar(int id) { + Connection connection = connectionToDatabase.getConnection(); + try { + Statement stmt = connection.createStatement(); + ResultSet rs = stmt.executeQuery("SELECT * FROM user WHERE id=" + id); + if (rs.next()) { + return extractUserFromResultSet(rs); + } + } catch (SQLException ex) { + ex.printStackTrace(); + } + return null; + } + + public Car getUserByUserNameAndPassword(String user, String pass) { + Connection connection = connectionToDatabase.getConnection(); + try { + PreparedStatement ps = connection.prepareStatement("SELECT * FROM user WHERE user=? AND pass=?"); + ps.setString(1, user); + ps.setString(2, pass); + ResultSet rs = ps.executeQuery(); + if (rs.next()) { + return extractUserFromResultSet(rs); + } + } catch (SQLException ex) { + ex.printStackTrace(); + } + return null; + } + + public Set getAllUsers() { + Connection connection = connectionToDatabase.getConnection(); + try { + Statement stmt = connection.createStatement(); + ResultSet rs = stmt.executeQuery("SELECT * FROM user"); + Set cars = new HashSet(); + while (rs.next()) { + Car car = extractUserFromResultSet(rs); + cars.add(car); + } + return cars; + } catch (SQLException ex) { + ex.printStackTrace(); + } + return null; + } + + + public boolean insertUser(Car car) { + Connection connection = connectionToDatabase.getConnection(); + try { + PreparedStatement ps = connection.prepareStatement("INSERT INTO user VALUES (NULL, ?, ?, ?,?,?)"); + ps.setString(1, car.getMake()); + ps.setString(2, car.getModel()); + ps.setInt(3, car.getYear()); + ps.setString(4, car.getColor()); + ps.setString(5, car.getVin()); + + int i = ps.executeUpdate(); + if (i == 1) { + return true; + } + } catch (SQLException ex) { + ex.printStackTrace(); + } + return false; + } + + public boolean updateUser(Car car) { + Connection connection = connectionToDatabase.getConnection(); + try { + PreparedStatement ps = connection.prepareStatement("UPDATE user SET make=?, model=?, year=?,color=?, vin=? WHERE id=?"); + + ps.setString(1, car.getMake()); + ps.setString(2, car.getModel()); + ps.setInt(3, car.getYear()); + ps.setString(4, car.getColor()); + ps.setString(5, car.getVin()); + ps.setInt(6, car.getId()); + + int i = ps.executeUpdate(); + if (i == 1) { + return true; + } + } catch (SQLException ex) { + ex.printStackTrace(); + } + return false; + } + + public boolean deleteUser(int id) { + Connection connection = connectionToDatabase.getConnection(); + try { + Statement stmt = connection.createStatement(); + int i = stmt.executeUpdate("DELETE FROM user WHERE id=" + id); + if (i == 1) { + return true; + } + } catch (SQLException ex) { + ex.printStackTrace(); + } + return false; + } + + +} + + + + + 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/UserDAO.java b/src/main/java/daos/UserDAO.java new file mode 100644 index 0000000..68c3413 --- /dev/null +++ b/src/main/java/daos/UserDAO.java @@ -0,0 +1,11 @@ +package daos; + +import java.util.Set; + +public interface UserDAO { + Car getCar(); + Car getUserByMakeAndModel(); + boolean insertCar(); + boolean updateCar(); + boolean deleteCar(); +} 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/DELETEME.txt b/src/test/java/daos/DELETEME.txt deleted file mode 100644 index e69de29..0000000 diff --git a/src/test/java/models/DELETEME.txt b/src/test/java/models/DELETEME.txt deleted file mode 100644 index e69de29..0000000 diff --git a/target/classes/daos/Car.class b/target/classes/daos/Car.class new file mode 100644 index 0000000..7f37ea8 Binary files /dev/null and b/target/classes/daos/Car.class differ