Skip to content
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

Finished #21

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.

20 changes: 20 additions & 0 deletions .idea/jarRepositories.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.

3 changes: 2 additions & 1 deletion jdbc-dao.iml
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4" />
<module type="JAVA_MODULE" version="4" >
</module>
12 changes: 12 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,23 @@
<artifactId>jdbcdao</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.25</version>
</dependency>


</dependencies>
</project>
70 changes: 70 additions & 0 deletions src/main/java/MainExecutor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import daos.CarDao;
import models.Car;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.List;

public class MainExecutor {

public static final String URL = "jdbc:mysql://localhost:3306/zipcode";
public static final String USER = "keer";
public static final String PASS = "keer123";

public static void main(String[] args) {

// try {
// Class.forName("com.mysql.jdbc.driver");
// }
// catch (ClassNotFoundException e) {
// e.printStackTrace();
// }
//TRY RESOURCE METHOD
try (Connection connection = DriverManager.getConnection(URL, USER, PASS)){

CarDao carDao = new CarDao(connection);

List<Car> cars = carDao.findAll();
int noOfCars = cars.size();
System.out.println("No of Cars available in database: " + noOfCars);
cars.forEach(System.out::println);

Car foundCar = carDao.findById(cars.get(1).getId());
System.out.println("Find Car by ID : " + cars.get(1).getId() + " => Model: " + foundCar.getMake() + " Make: " + foundCar.getModel() + " VIN: " + foundCar.getVin());

Car newCar = getCar(++noOfCars, "Audi", "R8", 2020, "yellow", 154323456);
Car createdCar = carDao.create(newCar);
System.out.println("New Car created ID : " + createdCar.getId() + " => Model: " + createdCar.getMake() + " Make: " + createdCar.getModel() + " VIN: " + createdCar.getVin());

foundCar.setVin(foundCar.getVin()+100);
Car updatedCar = carDao.update(foundCar);
System.out.println("Updated Car ID : " + updatedCar.getId() + " => Model: " + updatedCar.getMake() + " Make: " + updatedCar.getModel() + " VIN: " + updatedCar.getVin());

List<Car> carsBeforeDelete = carDao.findAll();
int noOfCarsBeforeDelete = carsBeforeDelete.size();
System.out.println("No of Cars available before delete operation: " + noOfCarsBeforeDelete);

carDao.delete(noOfCars);
System.out.println("Successfully deleted car. ID: " + noOfCars);

List<Car> carsAfterDelete = carDao.findAll();
int noOfCarsAfterDelete = carsAfterDelete.size();
System.out.println("No of Cars available after delete operation: " + noOfCarsAfterDelete);

} catch (SQLException ex) {
throw new RuntimeException("Error connecting to the database", ex);
}
}

public static Car getCar(int id, String make, String model, int year, String color, long vin) {
Car car = new Car();
car.setId(id);
car.setModel(model);
car.setMake(make);
car.setYear(year);
car.setColor(color);
car.setVin(vin);
return car;
}
}
12 changes: 12 additions & 0 deletions src/main/java/daos/BaseDao.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package daos;

import java.util.List;

public abstract class BaseDao<T> {

public abstract T findById(int id);
public abstract List findAll();
public abstract T update(T dto);
public abstract T create(T dto);
public abstract void delete(int id);
}
144 changes: 144 additions & 0 deletions src/main/java/daos/CarDao.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
package daos;

import models.Car;
import models.Car;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class CarDao extends BaseDao<Car> {

private Connection connection;

public CarDao(Connection connection) {
this.connection = connection;
}

/**
* This method is used to find Car by id
* @param id
* @return Car
*/
public Car findById(int id) {

Car car = null;
try (PreparedStatement ps = connection.prepareStatement("Select id,make,model,year,color,vin from Car where id = ?")) {

ps.setInt(1, id);

ResultSet rs = ps.executeQuery();
while (rs.next()) {

car = new Car();
car.setId(rs.getInt(1));
car.setMake(rs.getString(2));
car.setModel(rs.getString(3));
car.setYear(rs.getInt(4));
car.setColor(rs.getString(5));
car.setVin(rs.getLong(6));
}

} catch (SQLException sqlException) {
sqlException.printStackTrace();
}
return car;
}

/**
*
* @return
*/
public List findAll() {
List<Car> carList = new ArrayList<>();
try (PreparedStatement ps = connection.prepareStatement("Select id,make,model,year,color,vin from Car order by id")) {

ResultSet rs = ps.executeQuery();
while (rs.next()) {

Car car = new Car();
car.setId(rs.getInt(1));
car.setMake(rs.getString(2));
car.setModel(rs.getString(3));
car.setYear(rs.getInt(4));
car.setColor(rs.getString(5));
car.setVin(rs.getLong(6));
carList.add(car);
}
} catch (SQLException sqlException) {
sqlException.printStackTrace();
}
return carList;
}

/**
*
* @param car
* @return
*/
public Car update(Car car) {

try (PreparedStatement ps = connection.prepareStatement("update car 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.setLong(5, car.getVin());
ps.setInt(6, car.getId());

boolean status = ps.execute();
if (status) {
System.out.println("Successfully Updated Car Data");
}
} catch (SQLException sqlException) {
sqlException.printStackTrace();
}
return car;
}

/**
*
* @param car
* @return
*/
public Car create(Car car) {

try (PreparedStatement ps = connection.prepareStatement("insert into Car(id,make,model,year,color,vin) values(?,?,?,?,?,?)")) {


ps.setInt(1, car.getId());
ps.setString(2, car.getMake());
ps.setString(3, car.getModel());
ps.setInt(4, car.getYear());
ps.setString(5, car.getColor());
ps.setLong(6, car.getVin());

boolean status = ps.execute();
if (status) {
System.out.println("Successfully Inserted Car Data");
}
} catch (SQLException sqlException) {
sqlException.printStackTrace();
}
return car;
}

/**
*
* @param id
*/
public void delete(int id) {
try (PreparedStatement ps = connection.prepareStatement("delete from car where id = ?")) {

ps.setInt(1, id);

boolean status = ps.execute();
if (status) {
System.out.println("Successfully Delete Car Data");
}
} catch (SQLException sqlException) {
sqlException.printStackTrace();
}
}
}
6 changes: 6 additions & 0 deletions src/main/java/models/BaseModel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package models;

public interface BaseModel {

int getId();
}
65 changes: 65 additions & 0 deletions src/main/java/models/Car.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package models;

public class Car implements BaseModel {

int id;
String make;
String model;
String color;
int year;
long vin;

@Override
public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getMake() {
return make;
}

public void setMake(String make) {
this.make = make;
}

public String getModel() {
return model;
}

public void setModel(String model) {
this.model = model;
}

public String getColor() {
return color;
}

public void setColor(String color) {
this.color = color;
}

public int getYear() {
return year;
}

public void setYear(int year) {
this.year = year;
}

public long getVin() {
return vin;
}

public void setVin(long vin) {
this.vin = vin;
}

@Override
public String toString() {
return String.format("Car {id: %s, make: %s, model: %s, year: %s, color: %s, vin: %s}", id, make, model, year, color, vin);
}
}
Binary file added target/classes/MainExecutor.class
Binary file not shown.
Binary file added target/classes/daos/BaseDao.class
Binary file not shown.
Binary file added target/classes/daos/CarDao.class
Binary file not shown.
Binary file added target/classes/models/BaseModel.class
Binary file not shown.
Binary file added target/classes/models/Car.class
Binary file not shown.
Binary file added target/jdbcdao-1.0-SNAPSHOT.jar
Binary file not shown.
5 changes: 5 additions & 0 deletions target/maven-archiver/pom.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#Generated by Maven
#Tue Dec 07 20:29:51 EST 2021
version=1.0-SNAPSHOT
groupId=com.zipcoder.lab
artifactId=jdbcdao
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
daos/CarDao.class
models/Car.class
daos/BaseDao.class
MainExecutor.class
models/BaseModel.class
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/Users/keerthanasrinivasan/Documents/projects/Maven.JDBC-DAO/src/main/java/models/Car.java
/Users/keerthanasrinivasan/Documents/projects/Maven.JDBC-DAO/src/main/java/daos/CarDao.java
/Users/keerthanasrinivasan/Documents/projects/Maven.JDBC-DAO/src/main/java/daos/BaseDao.java
/Users/keerthanasrinivasan/Documents/projects/Maven.JDBC-DAO/src/main/java/MainExecutor.java
/Users/keerthanasrinivasan/Documents/projects/Maven.JDBC-DAO/src/main/java/models/BaseModel.java