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/misc.xml b/.idea/misc.xml index 803a716..68eaee3 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -8,7 +8,7 @@ - + \ No newline at end of file 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..ce592d2 100644 --- a/pom.xml +++ b/pom.xml @@ -9,6 +9,11 @@ 1.0-SNAPSHOT + + mysql + mysql-connector-java + 8.0.18 + junit junit 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/PetsConcreteDao.java b/src/main/java/daos/PetsConcreteDao.java new file mode 100644 index 0000000..2e21555 --- /dev/null +++ b/src/main/java/daos/PetsConcreteDao.java @@ -0,0 +1,116 @@ +package daos; + +import models.Connections; +import models.Pet; + +import java.sql.*; +import java.util.ArrayList; +import java.util.List; + +public class PetsConcreteDao implements PetsDao { + + Connection connection = Connections.getConnection(); + + public Pet findById(int id) { + try { + + Statement stmt = connection.createStatement(); + ResultSet rs = stmt.executeQuery("SELECT * FROM pets WHERE pet_id=" + id); + + if(rs.next()) + return extractPetFromResultSet(rs); + + } catch (SQLException ex) { + ex.printStackTrace(); + } + + return null; + } + + private Pet extractPetFromResultSet(ResultSet rs) throws SQLException { + + Pet pet = new Pet(); + + pet.setPetId( rs.getInt("pet_id") ); + pet.setPetName( rs.getString("pet_name") ); + pet.setPetOwner( rs.getString("pet_owner") ); + pet.setPetType( rs.getString("pet_type")); + pet.setPetSex( rs.getString( "pet_sex")); + return pet; + } + + public List findAll() { + + + try { + Statement stmt = connection.createStatement(); + ResultSet rs = stmt.executeQuery("SELECT * FROM pets;"); + List pets = new ArrayList(); + + while(rs.next()) + { + Pet pet = extractPetFromResultSet(rs); + pets.add(pet); + } + + return pets; + + } catch (SQLException ex) { + ex.printStackTrace(); + } + + return null; + } + + public Pet update(Pet pet) { + + try { + PreparedStatement ps = connection.prepareStatement("UPDATE pets SET pet_name=?, pet_owner=?, " + + "pet_type=?, pet_sex=? WHERE pet_id=?"); + ps.setString(1, pet.getPetName()); + ps.setString(2, pet.getPetOwner()); + ps.setString(3, pet.getPetType()); + ps.setString(4, pet.getPetSex()); + ps.setInt(5, pet.getPetId()); + int i = ps.executeUpdate(); + + if(i == 1) { + return findById(pet.getPetId()); + } + } catch (SQLException ex) { + ex.printStackTrace(); + } + return null; + } + + public Pet create(Pet pet) { + + try { + PreparedStatement ps = connection.prepareStatement("INSERT INTO pets VALUES (NULL, ?, ?, ?, ?)"); + ps.setString(1, pet.getPetName()); + ps.setString(2, pet.getPetOwner()); + ps.setString(3, pet.getPetType()); + ps.setString(4, pet.getPetSex()); + int i = ps.executeUpdate(); + + if(i == 5) { + + return findById(pet.getPetId()); + } + } catch (SQLException ex) { + ex.printStackTrace(); + } + return null; + } + + public void delete(int id) { + + try { + Statement stmt = connection.createStatement(); + int i = stmt.executeUpdate("DELETE FROM pets WHERE pet_id=" + id); + + } catch (SQLException ex) { + ex.printStackTrace(); + } + } +} diff --git a/src/main/java/daos/PetsDao.java b/src/main/java/daos/PetsDao.java new file mode 100644 index 0000000..5422108 --- /dev/null +++ b/src/main/java/daos/PetsDao.java @@ -0,0 +1,14 @@ +package daos; + +import models.Pet; + +import java.util.List; + +interface PetsDao { + public Pet findById(int id); + public List findAll(); + public Pet update(Pet pet); + public Pet create(Pet pet); + public void delete(int id); + +} diff --git a/src/main/java/models/AppRunner.java b/src/main/java/models/AppRunner.java new file mode 100644 index 0000000..f2f8b23 --- /dev/null +++ b/src/main/java/models/AppRunner.java @@ -0,0 +1,46 @@ +package models; + +import daos.PetsConcreteDao; +import java.util.List; + +public class AppRunner { + + + public static void main(String[] args) { + PetsConcreteDao pets = new PetsConcreteDao(); + + printPet(pets.findById(2)); + + printPetsList(pets.findAll()); + + Pet pet1 = new Pet("Kiki", "Yuri Fontes", "Cat", "Female"); + printPet(pets.create(pet1)); + + Pet pet = pets.findById(1); + pet.setPetOwner("Maira Botelho"); + printPet(pets.update(pet)); + + pets.delete(3); + + printPetsList(pets.findAll()); + + } + + public static void printPet(Pet pet){ + System.out.println( + "=======================" + + "\nPet Id: " + pet.getPetId() + + "\nPet Name: " + pet.getPetName() + + "\nPet's Owner: " + pet.getPetOwner() + + "\nPet Type: " + pet.getPetType() + + "\nPet Sex: " + pet.getPetSex() + + "\n=======================\n" + ); + } + + public static void printPetsList(List petList){ + for(Pet pet : petList) + printPet(pet); + } +} + diff --git a/src/main/java/models/Connections.java b/src/main/java/models/Connections.java new file mode 100644 index 0000000..45bf5d8 --- /dev/null +++ b/src/main/java/models/Connections.java @@ -0,0 +1,22 @@ +package models; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; + +public class Connections { + public static final String URL = "jdbc:mysql://localhost:3306/vet"; + public static final String USER = "root"; + public static final String PASS = "password"; + /** + * Get a connection to database + * @return Connection object + */ + public static Connection getConnection() { + try { + return DriverManager.getConnection(URL, USER, PASS); + } catch (SQLException ex) { + throw new RuntimeException("Error connecting to the database", ex); + } + } +} 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/main/java/models/Pet.java b/src/main/java/models/Pet.java new file mode 100644 index 0000000..df63675 --- /dev/null +++ b/src/main/java/models/Pet.java @@ -0,0 +1,77 @@ +package models; + +import java.util.Date; + +public class Pet implements PetDto { + + private Integer petId; + private String petName; + private String petOwner; + private String petType; + private String petSex; + + public Pet(){ + } + + public Pet(Integer petId, String petName, String petOwner, String petType, + String petSex){ + + this.petId = petId; + this.petName = petName; + this.petOwner = petOwner; + this.petType = petType; + this.petSex = petSex; + } + + public Pet(String petName, String petOwner, String petType, + String petSex){ + + this.petName = petName; + this.petOwner = petOwner; + this.petType = petType; + this.petSex = petSex; + } + + public Integer getPetId() { + return petId; + } + + public void setPetId(Integer petId) { + this.petId = petId; + } + + public String getPetName() { + return petName; + } + + public void setPetName(String petName) { + this.petName = petName; + } + + public String getPetOwner() { + return petOwner; + } + + public void setPetOwner(String petOwner) { + this.petOwner = petOwner; + } + + public String getPetType() { + return petType; + } + + public void setPetType(String petType) { + this.petType = petType; + } + + public String getPetSex() { + return petSex; + } + + public void setPetSex(String petSex) { + this.petSex = petSex; + } + + + +} diff --git a/src/main/java/models/PetDto.java b/src/main/java/models/PetDto.java new file mode 100644 index 0000000..77e08f2 --- /dev/null +++ b/src/main/java/models/PetDto.java @@ -0,0 +1,7 @@ +package models; + +interface PetDto { + + Integer getPetId(); + +}