Problem setting a list in a Singleton

Asked

Viewed 35 times

0

well I created a list with a select in the database and wanted to set it on my Singleton so I could use it on all screens: code of my DAO:

 public List<Requisicao> pegarRequisicoesUser() {
    List<Requisicao> requisicoes = new ArrayList<>();
    String sql = "SELECT * FROM equipamento_requisicao equipreq INNER JOIN equipamento_user equipuser ON (equipreq.idequipamento_user = equipuser.id_equipamento_do_usuario)  INNER JOIN usuario user ON (user.id_usuario=equipuser.idusuario) INNER JOIN equipamentos equip ON (equip.id_equipamentos = equipuser.idequipamentos) INNER JOIN detalhe_status dStatus ON (dStatus.idequipamento_requisicao= equipreq.id_requisicao) INNER JOIN status_requisicao statusreq on (statusreq.id_status= dStatus.idstatus) INNER JOIN permissao p ON(user.idpermissao= p.id_permissao) INNER JOIN departamentos dp ON(user.iddepartamento = dp.id_departamentos) INNER JOIN chefe_departamento cp ON(dp.id_departamentos = cp.iddepartamento) where statusreq.categoria='Ativo' and user.id_usuario = "+ Sessao.getInstancia().getUsuario().getId() + " ";
    try {
        PreparedStatement stmt = con.prepareStatement(sql);
        ResultSet rs = stmt.executeQuery();
        while (rs.next()) {       


            //CHEFES DE DEPARTAMENTOS            
            // usuario chefe depart
            Usuario usuarioChefe = new Usuario();
            usuarioChefe.setId(rs.getLong("cp.idusuario")); 
            // usuario chefe depart
            // departamento chefe
            Departamento departamentoChefe = new Departamento();
            departamentoChefe.setId_departamento(rs.getInt("cp.iddepartamento"));
            // departamento chefe
            // INICIO DA TABELA CHEFE
            ChefesDepartamentos chefes = new ChefesDepartamentos();
            chefes.setId_chefe(rs.getInt("cp.id_chefe"));
            chefes.setCpIdusuario(usuarioChefe);
            chefes.setCpIddepartamento(departamentoChefe);
            // FIM TABELA CHEFE
            // FIM    // PARA ESTUDOS

            // INICIO TABELA USUARIO 

            //Permissao Usuario
            Permissao permissaoUsuario = new Permissao();
            permissaoUsuario.setId_permissao(rs.getInt("user.idpermissao"));
            permissaoUsuario.setCategoria_permissao(rs.getString("p.categoria"));
            //fim permissao usuario
            //incio departamento usuario
            Departamento departamentoUsuario = new Departamento();
            departamentoUsuario.setId_departamento(rs.getInt("dp.id_departamentos"));
            departamentoUsuario.setNome_setor(rs.getString("dp.nome_setor"));
            //FIM
            // usuario
            Usuario usuario = new Usuario();
            usuario.setNome(rs.getString("user.nome"));
            usuario.setId(rs.getLong("user.id_usuario"));
            usuario.setMatricula(rs.getString("user.matricula"));
            usuario.setLogin(rs.getString("user.login"));
            usuario.setpUsuario(permissaoUsuario);
            usuario.setdUsuario(departamentoUsuario);
            // FIM TABELA USUARIO               
            //equipamento // equipamento user tabelas
            Equipamentos equipamento = new Equipamentos();
            equipamento.setEquipamento_nome(rs.getString("equip.equipamento_nome"));
            equipamento.setSerial_equipamento(rs.getString("equipuser.serial_equipamento"));
            equipamento.setId_equipamento_do_Usuario(rs.getLong("equipreq.idequipamento_user"));
            //status tabela
            Status status = new Status();
            status.setCategoria(rs.getString("statusreq.categoria"));
            status.setIdstatus(rs.getInt("statusreq.id_status"));
            //status detalhes tabela
            Usuario usuarioStatus = new Usuario();//id do usuaro na tabela detalhes status
            usuarioStatus.setId(rs.getLong("dStatus.idusuario"));
            StatusDetalhes statusDetalhes = new StatusDetalhes();
            statusDetalhes.setId_statusdetalhes(rs.getLong("dStatus.id_statusdetalhes"));
            statusDetalhes.setData_status(rs.getTimestamp("dStatus.data"));
            statusDetalhes.setObservacao_status(rs.getString("dStatus.observacao"));
            statusDetalhes.setIdUsuario(usuarioStatus);
            statusDetalhes.setIdStatus(status);
            // Id da requisicao na tabela detalhes status
            Requisicao requisicaoStatus = new Requisicao();
            requisicaoStatus.setId(rs.getLong("dStatus.idequipamento_requisicao"));
            //requisicao tabela
            Requisicao req = new Requisicao();
            req.setId(rs.getLong("equipreq.id_requisicao"));
            req.setNome(rs.getString("equipreq.nome"));
            req.setData_criada(rs.getTimestamp("equipreq.data_requisicao"));
            req.setMotivo(rs.getString("equipreq.observacao"));
            req.setReqEquipamento(equipamento);
            req.setReqStatus(statusDetalhes);
            req.setReqUsuario(usuario);
            req.setReqStatus_Detalhes(statusDetalhes);
            req.setChefes(chefes);
            requisicoes.add(req);
        }
        stmt.close();
        rs.close();
    } catch (SQLException ex) {
        Logger.getLogger(RequisicaoDAO.class.getName()).log(Level.SEVERE, null, ex);
        return null;
    }

    return requisicoes;
}

and in my Singleton I did:

private List<Requisicao> requisicaolist;
public List<Requisicao> getRequisicaolist() {
    return requisicaolist;
}

public void setRequisicaolist(List<Requisicao> requisicaolist) {
    this.requisicaolist = requisicaolist;
}

but I don’t know how I’ll be able to access this list anywhere in the program

RequisicaoDAO dao = new RequisicaoDAO();
aSessao.getInstancia().setRequisicaolist(dao.pegarRequisicoesUser());

I set her up in my main how I could get data from that list?

  • I think it’s not quite Singleton what you want... Singleton is to keep a same instance of a class, and you want to keep a list, that would be simpler, a list Static or even better, because it is database data, a cache for example, that can expire

  • type when logging I save all data of user class all data of equipment class all data of all departments class of the head departments, I wanted to save all requests created by that user in a list when he log in.

  • could you help me? I don’t know how to cache and how to access it in any program’s Stage.

  • What you need is definitely not Singleton... it’s a web application?

  • Desktop..

  • I’m just wanting to save all the database data in their respective classes in java.

  • To type a "session"

Show 2 more comments

1 answer

0


You can turn the object into a Static to have a single instance, and check if it has already been instantiated in get:

private static List<Requisicao> requisicaolist;

public List<Requisicao> getRequisicaolist() {
    if (requisicaolist == null) {
        RequisicaoDAO dao = new RequisicaoDAO();
        requisicaolist = dao.pegarRequisicoesUser();
    }
    return requisicaolist;
}

So, when using the get, if the list has not yet been instantiated, call the method pegarRequisicoesUser(), otherwise only returns the value that is already stored, causing you to refer to the bank only once.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.