Skip to content

complete #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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.

11 changes: 10 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,16 @@
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<version>4.12</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.18</version>
</dependency>


</dependencies>


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

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


}





Empty file removed src/main/java/daos/DELETEME.txt
Empty file.
11 changes: 11 additions & 0 deletions src/main/java/daos/UserDAO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package daos;

import java.util.Set;

public interface UserDAO {
Car getCar();
Car getUserByMakeAndModel();
boolean insertCar();
boolean updateCar();
boolean deleteCar();
}
Empty file removed src/main/java/models/DELETEME.txt
Empty file.
Empty file removed src/test/java/daos/DELETEME.txt
Empty file.
Empty file removed src/test/java/models/DELETEME.txt
Empty file.
Binary file added target/classes/daos/Car.class
Binary file not shown.