Package br.com.yaw.sjc.exception

Examples of br.com.yaw.sjc.exception.PersistenceException


  }
 
  @Override
  public void save(Mercadoria mercadoria) throws PersistenceException {
    if (mercadoria == null) {
      throw new PersistenceException("Informe a Mercadoria para salvar!");
    }
    Connection conn = null;
    PreparedStatement stmt = null;
    try {
      conn = ConnectionManager.getConnection();
      if (mercadoria.getId() == null) {
        stmt = getStatementInsert(conn, mercadoria);
      } else {
        stmt = getStatementUpdate(conn, mercadoria);
      }
      stmt.executeUpdate();
      conn.commit();
      log.debug("Mercadoria foi salva");
    } catch (SQLException e) {
      try { conn.rollback(); } catch (Exception sx) {}
      String errorMsg = "Erro ao salvar Mercadoria!";
      log.error(errorMsg, e);
      throw new PersistenceException(errorMsg, e);
    } finally {
      ConnectionManager.closeAll(conn, stmt);
    }
  }
View Full Code Here


  }

  @Override
  public void remove(Mercadoria m) throws PersistenceException {
    if (m == null || m.getId() == null) {
      throw new PersistenceException("Informe a mercadoria para exclusao!");
    }
    Connection conn = null;
    PreparedStatement stmt = null;
    try {
      conn = ConnectionManager.getConnection();
      stmt = createStatementWithLog(conn, DELETE_MERCADORIA);
      stmt.setInt(1, m.getId());
      stmt.executeUpdate();
      conn.commit();
      log.debug("Mercadoria foi excluida");
    } catch (SQLException e) {
      try { conn.rollback(); } catch (Exception sx) {}
      String errorMsg = "Erro ao excluir Mercadoria!";
      log.error(errorMsg, e);
      throw new PersistenceException(errorMsg, e);
    }finally{
      ConnectionManager.closeAll(conn, stmt);
    }
  }
View Full Code Here

  }
 
  @Override
  public Mercadoria findById(Integer id) throws PersistenceException {
    if (id == null || id.intValue() <= 0) {
      throw new PersistenceException("Informe o id válido para fazer a busca!");
    }
    Connection conn = null;
    PreparedStatement stmt = null;
    ResultSet rs = null;
    Mercadoria m = null;
   
    try {
      conn = ConnectionManager.getConnection();
      stmt = createStatementWithLog(conn, GET_MERCADORIA_BY_ID);
      stmt.setInt(1, id);
      rs = stmt.executeQuery();
     
      if (rs.next()) {
        String nome = rs.getString("nome");
        String descricao = rs.getString("descricao");
        int qtde = rs.getInt("quantidade");
        double preco = rs.getDouble("preco");
       
        m = new Mercadoria(id, nome, descricao, qtde, preco);
      }
      return m;
    } catch (SQLException e) {
      String errorMsg = "Erro ao consultar mercadoria por id!";
      log.error(errorMsg, e);
      throw new PersistenceException(errorMsg, e);
    } finally {
      ConnectionManager.closeAll(conn, stmt, rs);
    }
  }
View Full Code Here

     
      return toMercadorias(rs);
    } catch (SQLException e) {
      String errorMsg = "Erro ao consultar todas as mercadorias!";
      log.error(errorMsg, e);
      throw new PersistenceException(errorMsg, e);
    } finally {
      ConnectionManager.closeAll(conn, stmt, rs);
    }
  }
View Full Code Here

     
      return toMercadorias(rs);
    } catch (SQLException e) {
      String errorMsg = "Erro ao consultar mercadorias por nome!";
      log.error(errorMsg, e);
      throw new PersistenceException(errorMsg, e);
    } finally {
      ConnectionManager.closeAll(conn, stmt, rs);
    }
  }
View Full Code Here

      log.debug("Aberta a conexão com banco de dados!");
      return conn;
    } catch (ClassNotFoundException e) {
      String errorMsg = "Driver (JDBC) não encontrado";
      log.error(errorMsg, e);
      throw new PersistenceException(errorMsg, e);
    } catch (SQLException e) {
      String errorMsg = "Erro ao obter a conexão";
      log.error(errorMsg, e);
      throw new PersistenceException(errorMsg, e);
    }
  }
View Full Code Here

      if (r > 0) {
        log.info("Criou a tabela 'mercadoria'");
      }
    } catch (SQLException e) {
      log.error(e);
      throw new PersistenceException("Não foi possivel inicializar o banco de dados: " + CREATE_TABLE, e);
    } finally {
      ConnectionManager.closeAll(conn, stmt);
    }
  }
View Full Code Here

TOP

Related Classes of br.com.yaw.sjc.exception.PersistenceException

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.