@Override public Usuario getById(int codigo) throws BusinessException { Connection con = null; String sql = null; try { // Obtem a conexão con = Conexao.obterConexao(); con.setAutoCommit(false); String origem = Conexao.obterOrigem(); sql = FabricaSql.getSql(origem + OBTER_USUARIO_BY_ID); if (DEBUG) System.out.println("SQL - " + sql); PreparedStatement stmt = con.prepareStatement(sql); stmt.setInt(1, codigo); ResultSet rs = stmt.executeQuery(); Usuario usu = null; while (rs.next()) { usu = new Usuario(); String nome = rs.getString("NOME"); String username = rs.getString("USERNAME"); usu.setNome(nome); usu.setUsername(username); } rs.close(); stmt.close(); return usu; } catch (SQLException e) { SQLExceptionHandler.tratarSQLException(this.getClass().getName(), e); return null; } finally { Conexao.fecharConexao(con); } }
/** * TODO - Documentar * * @throws BusinessException */ @Override public Usuario autenticar(Usuario user) throws BusinessException { Connection con = null; String sql = null; try { // Obtem a conexão con = Conexao.obterConexao(); con.setAutoCommit(false); String origem = Conexao.obterOrigem(); sql = FabricaSql.getSql(origem + AUTENTICAR_USER); PreparedStatement stmt = con.prepareStatement(sql); stmt.setString(1, user.getUsername()); stmt.setString(2, user.getPassword()); ResultSet rs = stmt.executeQuery(); Usuario usuario = null; while (rs.next()) { int codigo = Integer.parseInt(rs.getString("CODIGO")); String nome = rs.getString("NOME"); usuario = user; usuario.setCodigo(codigo); usuario.setNome(nome); } rs.close(); stmt.close(); if (Utils.isNullOrEmpty(usuario)) throw new BusinessException("Usuário não encontrado!"); else if (Utils.isNullOrEmpty(usuario.getNome())) throw new BusinessException("Usuário não possui nome."); return usuario; } catch (SQLException e) { SQLExceptionHandler.tratarSQLException(this.getClass().getName(), e); return null; } finally { Conexao.fecharConexao(con); } }