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

Lab Done #2

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.

2 changes: 1 addition & 1 deletion .idea/misc.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.

5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
<version>1.0-SNAPSHOT</version>

<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.18</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
Empty file removed src/main/java/daos/DELETEME.txt
Empty file.
116 changes: 116 additions & 0 deletions src/main/java/daos/PetsConcreteDao.java
Original file line number Diff line number Diff line change
@@ -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<Pet> findAll() {


try {
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM pets;");
List<Pet> pets = new ArrayList<Pet>();

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

}
46 changes: 46 additions & 0 deletions src/main/java/models/AppRunner.java
Original file line number Diff line number Diff line change
@@ -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<Pet> petList){
for(Pet pet : petList)
printPet(pet);
}
}

22 changes: 22 additions & 0 deletions src/main/java/models/Connections.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
Empty file removed src/main/java/models/DELETEME.txt
Empty file.
77 changes: 77 additions & 0 deletions src/main/java/models/Pet.java
Original file line number Diff line number Diff line change
@@ -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;
}



}
7 changes: 7 additions & 0 deletions src/main/java/models/PetDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package models;

interface PetDto {

Integer getPetId();

}