示例#1
0
  public void devolucao(Emprestimo emprestimo) {
    Connection conn = null;
    try {

      log.info("Abrindo conexão com o banco");

      conn = ConexaoBD.getConexao();
      PreparedStatement pstm =
          conn.prepareStatement(
              ""
                  + "update emprestimo "
                  + "set ind_devolvido = 'S' ,"
                  + " dataDevolucao = ? "
                  + "where id = ?");

      pstm.setTimestamp(1, new Timestamp(System.currentTimeMillis()));
      pstm.setInt(2, emprestimo.getId());

      pstm.executeUpdate();
      log.info("Devolução inserida corretamente.");

    } catch (Exception e) {
      log.error("Erro, ao tentar efetuar a devolução do livro!");
      throw new RuntimeException(e);
    } finally {
      if (conn != null) {
        try {
          conn.close();
        } catch (SQLException ex) {
        }
      }
    }
  }
示例#2
0
  public void salvar(Emprestimo emprestimo) {
    Connection conn = null;
    try {

      log.info("Abrindo conexão com o banco");

      conn = ConexaoBD.getConexao();
      PreparedStatement pstm =
          conn.prepareStatement(
              ""
                  + "INSERT INTO emprestimo "
                  + "(dataEmprestimo,dataDevolucao,id_usuario,id_livro,ind_devolvido) "
                  + "VALUES (?,?,?,?,?)",
              Statement.RETURN_GENERATED_KEYS);

      // pstm.setInt(1, p_usuario.getId());
      pstm.setTimestamp(1, new Timestamp(emprestimo.getDtEmprestimo().getTime()));
      pstm.setTimestamp(2, new Timestamp(emprestimo.getDtDevolucao().getTime()));
      pstm.setInt(3, emprestimo.getUsuario().getId());
      pstm.setInt(4, emprestimo.getLivro().getId());
      pstm.setString(5, emprestimo.getIndDevolvido());

      pstm.executeUpdate();
      log.info("Inserindo novo empréstimo no banco de dados");
      Long id = ConexaoBD.getLastKey(pstm);
      emprestimo.setId(id == null ? null : id.intValue());

      log.info("ID criado = " + emprestimo.getId());
    } catch (Exception e) {
      log.error("Erro ao tentar efetuar o empréstimo!");
      throw new RuntimeException(e);
    } finally {
      if (conn != null) {
        try {
          conn.close();
        } catch (SQLException ex) {
        }
      }
    }
  }