You are on page 1of 4

public Collection<Pet> getListPets() { if (listPets != null && !listPets.

isEmpty()) { return listPets; } else { Connection conn = null; listPets = new ArrayList<Pet>(); try { conn = DBManager.crearConexionBD(); } catch (Exception e) { FacesContext.getCurrentInstance().addMessage( null, new FacesMessage(FacesMessage.SE VERITY_FATAL, "Error conectand o a la base de datos", null)); } try { java.sql.PreparedStatement ps = conn .prepareStatement("select pet_id ,category,name,imageurl,price from pet"); ResultSet rows = ps.executeQuery(); Pet petTmp = null; while (rows.next()) { petTmp = new Pet(); petTmp.setPet_id(rows.getInt("pet_id")); petTmp.setCategory(rows.getString("categ ory")); petTmp.setName(rows.getString("name")); petTmp.setImage(rows.getString("imageurl ")); petTmp.setPrice(rows.getLong("price")); listPets.add(petTmp); } return listPets; } catch (Exception e) { FacesContext.getCurrentInstance().addMessage( null, new FacesMessage(FacesMessage.SE VERITY_ERROR, "Error cargando mascotas" + e.getMessage(), null)); } finally { DBManager.cerrarConexion(); } return null; } } public String guardarMascota() { Connection conn = null; try { conn = DBManager.crearConexionBD(); } catch (Exception e) { FacesContext.getCurrentInstance().addMessage( null, new FacesMessage(FacesMessage.SEVERITY_F

ATAL, "Error conectando a la b ase de datos", null)); } try { java.sql.PreparedStatement ps = conn .prepareStatement("select category,name, imageurl,price from mascota where name=?"); ps.setString(1, objPet.getName()); ResultSet rows = ps.executeQuery(); if (rows.next()) { FacesContext.getCurrentInstance().addMessage( null, new FacesMessage(FacesMessage.SE VERITY_FATAL, "El nombre" + ob jPet.getName() + "Ya existe", null)); } else { conn.prepareStatement("insert into mascota (desc ription,name,imageurl,price) values(?,?,?,?)"); ps.setString(1, objPet.getDescription()); ps.setString(2, objPet.getName()); ps.setString(3, objPet.getImage()); ps.setLong(4, objPet.getPrice()); int i = ps.executeUpdate(); if (i != 0) { FacesContext.getCurrentInstance().addMes sage( null, new FacesMessage(FacesMe ssage.SEVERITY_FATAL, "Se regi stro", null)); } } } catch (Exception e) { FacesContext.getCurrentInstance().addMessage( null, new FacesMessage(FacesMessage.SEVERITY_F ATAL, "Error conectando a la b ase de datos", null)); } finally { DBManager.cerrarConexion(); } return null; } public Collection<Pet> filtrar() { Connection conn = null; if (listPets != null && !listPets.isEmpty()) { return listPets; } listPets = new ArrayList<Pet>();

try { conn = DBManager.crearConexionBD(); FacesContext.getCurrentInstance().addMessage( null, new FacesMessage(FacesMessage.SEVERITY_F ATAL, "Error conectando a la b ase de datos" + objPet .getCategory(), null)); } catch (Exception e) { FacesContext.getCurrentInstance().addMessage( null, new FacesMessage(FacesMessage.SEVERITY_F ATAL, "Error conectando a la b ase de datos", null)); } try { java.sql.PreparedStatement ps = conn .prepareStatement("select category,name, imageurl,price from mascota where category=?"); ps.setString(1, objPet.getCategory()); ResultSet rows = ps.executeQuery(); Pet petTmp = null; while (rows.next()) { petTmp = new Pet(); petTmp.setCategory(rows.getString("category")); petTmp.setName(rows.getString("name")); petTmp.setImage(rows.getString("imageurl")); petTmp.setPrice(rows.getLong("price")); listPets.add(petTmp); } return listPets; } catch (Exception e) { FacesContext.getCurrentInstance().addMessage( null, new FacesMessage(FacesMessage.SEVERITY_E RROR, "Error cargando mascotas " + e.getMessage(), null)); } finally { DBManager.cerrarConexion(); } return null; } public void setListPets(Collection<Pet> pets) { this.listPets = pets; }

Connection conn = null; try {

conn = DBManager.crearConexionBD(); } catch (Exception e) { FacesContext.getCurrentInstance().addMessage( null, new FacesMessage(FacesMessage.SEVERITY_F ATAL, "Error conectando a la b ase de datos", null)); return null; } try { java.sql.PreparedStatement ps = conn .prepareStatement("select user,pass from User where user=? and pass=?"); ps.setString(1, usuario); ps.setString(2, password); ResultSet resultado = ps.executeQuery(); if (resultado.next()) { FacesContext.getCurrentInstance().addMessage( null, new FacesMessage(FacesMessage.SE VERITY_INFO, "Bienvenido "+us uario, null)); return "/secure/index"; } else { FacesContext.getCurrentInstance().addMessage( null, new FacesMessage(FacesMessage.SE VERITY_INFO, "Verifique su us uario o contrasea", null)); } } catch (Exception e) { FacesContext.getCurrentInstance().addMessage(null, new F acesMessage(FacesMessage.SEVERITY_ERROR, "Error consultando la base de datos", n ull)); } finally{ DBManager.cerrarConexion(); } return null;