From a79d93ec6a0e15d57725261553ffffcd278189a1 Mon Sep 17 00:00:00 2001 From: Brandon Chambers Date: Sat, 30 Nov 2019 17:22:05 -0500 Subject: [PATCH] complete --- .idea/compiler.xml | 1 + .idea/vcs.xml | 6 + pom.xml | 11 +- src/main/java/daos/Car.java | 82 ++++++++ src/main/java/daos/ConnectionToDatabase.java | 195 +++++++++++++++++++ src/main/java/daos/DELETEME.txt | 0 src/main/java/daos/UserDAO.java | 11 ++ src/main/java/models/DELETEME.txt | 0 src/test/java/daos/DELETEME.txt | 0 src/test/java/models/DELETEME.txt | 0 target/classes/daos/Car.class | Bin 0 -> 2033 bytes 11 files changed, 305 insertions(+), 1 deletion(-) create mode 100644 .idea/vcs.xml create mode 100644 src/main/java/daos/Car.java create mode 100644 src/main/java/daos/ConnectionToDatabase.java delete mode 100644 src/main/java/daos/DELETEME.txt create mode 100644 src/main/java/daos/UserDAO.java delete mode 100644 src/main/java/models/DELETEME.txt delete mode 100644 src/test/java/daos/DELETEME.txt delete mode 100644 src/test/java/models/DELETEME.txt create mode 100644 target/classes/daos/Car.class 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 0000000000000000000000000000000000000000..7f37ea8aad4c94d77365efcc74c71a3bd0eb7917 GIT binary patch literal 2033 zcmb7^-%b-z5XNWQ{nM5z(9$X*Qb0>7f2x9d0YQ@*g;>P|L*k9eQZ{w5w6PQt-@}LS zQWF~!FMI$W%J_X}TPVvZ2{$umPiKDfotbm`_uub7BC;xv6B3uDxWr_crbKg}<^jz^ zniWS@9eHF(#NRh0T{&=1+)~|b)Jj{8wpa67YlfJI?kCTXRNs#~?UvuD;Wz8Bx$o5x z^Tl&p_^dYTP59`E-!Q~p_ZxnD1D}QYUBr|TYshfLZ+LHx5BI#*J9n?H$jz!--*sC) zzq^}e`=ftkNTRauHjheWmr6bqE^4@Rr+0`;gSv;I#LxW&u9DnW;3{dXY1O>;7Mh1$Ao z5#3r?c57kPtA(YimDp(>x2oO?p979ze9OFANl8gMDM`7Olf z#0?oayN}y@2VS);)3~&8+-_SUjK(TLobn(l52*6sDi1XLu;30$8g>j{EAR%qRs1E! z4;7YyO#mkdHxZu2Dj5XH4Y`Rg0>)tp_=-pyK1V=tp(ByvVn?FIrH+`zakB&RxYZH+ zjD1HO#E*iV!V%JFz!+=>#H>uA0_$xA%x*o<)!`^K$A&3&Id)B=YL7=W)ryC-pe-f0 zV5#sMqFFKDQ|M4(gbJfdA#_3^blT^1&Y;BtRVGMrnLyp!&^d{nJJ2~L_n?JZHv{^q z*1Q@BwdR#-b~3M2Q)^zSrq(Ze+ilne+RHiF zyur@dT@BvB8m6?a2-zU&bk=rv)&_1vUdM`rSx>=wHh{$k%&E`mSmeVb)8qHV3fy&d8eA>$1YES722Juz2UmD(YE%{}Vy)x50WlfW=os)`EH-i~j-A CK@pz- literal 0 HcmV?d00001