From e345b8ebfdd40d7c78315f83660d7f05b2da399f Mon Sep 17 00:00:00 2001 From: miferreiro Date: Thu, 21 Feb 2019 17:35:11 +0100 Subject: [PATCH] Renaming the class and adding exception in the getPets () function --- .../daa/dao/{PetDAO.java => PetsDAO.java} | 21 ++++++++++--------- 1 file changed, 11 insertions(+), 10 deletions(-) rename src/main/java/es/uvigo/esei/daa/dao/{PetDAO.java => PetsDAO.java} (97%) diff --git a/src/main/java/es/uvigo/esei/daa/dao/PetDAO.java b/src/main/java/es/uvigo/esei/daa/dao/PetsDAO.java similarity index 97% rename from src/main/java/es/uvigo/esei/daa/dao/PetDAO.java rename to src/main/java/es/uvigo/esei/daa/dao/PetsDAO.java index f75371b..e31c4ef 100644 --- a/src/main/java/es/uvigo/esei/daa/dao/PetDAO.java +++ b/src/main/java/es/uvigo/esei/daa/dao/PetsDAO.java @@ -18,8 +18,8 @@ import es.uvigo.esei.daa.entities.Pet; * * @author Miguel Ferreiro Diaz */ -public class PetDAO extends DAO { - private final static Logger LOG = Logger.getLogger(PetDAO.class.getName()); +public class PetsDAO extends DAO { + private final static Logger LOG = Logger.getLogger(PetsDAO.class.getName()); /** * Returns a person stored persisted in the system. @@ -95,11 +95,7 @@ public class PetDAO extends DAO { if (name == null || specie == null) { throw new IllegalArgumentException("name and specie can't be null " + specie); } - - if ( idOwner <= 0) { - throw new IllegalArgumentException("idOwner can't be negative"); - } - + try (Connection conn = this.getConnection()) { final String query = "INSERT INTO pets VALUES(null, ?, ?,?)"; @@ -198,8 +194,13 @@ public class PetDAO extends DAO { * @throws IllegalArgumentException if the provided id does not corresponds * with any persisted person. */ - public List getPets(int id) + public List getPets(int idOwner) throws DAOException, IllegalArgumentException { + + if ( idOwner <= 0) { + throw new IllegalArgumentException("idOwner can't be negative"); + } + try (final Connection conn = this.getConnection()) { final String query = "SELECT T.id, T.name, T.specie, T.idOwner " + "FROM people P, pets T " @@ -207,8 +208,8 @@ public class PetDAO extends DAO { try (final PreparedStatement statement = conn.prepareStatement(query)) { - statement.setInt(1, id); - + statement.setInt(1, idOwner); + try (final ResultSet result = statement.executeQuery()) { final List pets = new LinkedList<>(); -- 2.18.1