Jump to content

Recommended Posts

Posted

Oi ppl

Estou com um pequeno problema em preencher a minha combobox com dados registados numa base de dados

Na minha classe UsersDAL tenho este método

  public void retrieveCargo(int id) throws SQLException{

	 String sqlCommand = "select * from CARGO";
	 ResultSet carg;
	 carg = this.dbo.executeQuery(sqlCommand);

	 while (carg.next()){
		 this.idCargo = id;
		 this.cargo = carg.getString("CARGO");
	 }
	 dbo.closeConnection();
   }

e na minha classe UserPanel

tenho este método para chamar os dados para a combo

private void preenchimentoCombo() throws SQLException {
   UsersDAL b = new UsersDAL(this.dbo);
  try{
	    if (this.idCargo != -1){
		    b.retrieveCargo(this.idCargo);
		    jComboCargo.addItem(b.getCargo());

		    }
    } catch (SQLException e){
	    System.out.println("Erro..." + e.getMessage());
	    javax.swing.JOptionPane.showMessageDialog(null, "ERRO: " + e.getMessage(), "Aviso", javax.swing.JOptionPane.ERROR_MESSAGE);
    }
    this.idCargo = 0;   
 }

O problema é que sé me aparece o primeiro registo....tenho três

Algum problema com este código?

Obrigado

gmc11

 

Posted

Bom dia,

O principal problema é a estrutura do programa o segundo é a escolha de nomes de variáveis ​​que não são bem explícitos.

I- Exemplos nomes de variáveis : em vez de variável b podia ser usersDal , em vez de carg podia ser resultSet , em vez de dbo podia ser dbStatement etc ...

II- Mas o verdadeiro problema é a estrutura do programa (Design Patterns : DAO , MVC , ...).

Muito bem, você separou a classe de accesso aos dados da BD, Data Access Object ( DAO ) chamado no seu caso UsersDAL e a Vista (VIEW) na camada apresentação chamada no seu caso UserPanel. Muito bem !

Você deve separar a apresentação do accesso aos dados.

Isto é o que se chama em inglês : Separation of concerns

III- addItem tem que estar numa LOOP !

Codigo para JDK 1.7

public ArrayList retrieveCargo(int id) throws SQLException
{
			 ArrayList<String> cargos = new ArrayList<>();

			 String sqlCommand = "select * from CARGO";
			 ResultSet carg;
			 carg = this.dbo.executeQuery(sqlCommand);

			 while (carg.next()){
					 this.idCargo = id;
					 this.cargo = carg.getString("CARGO");
					 cargos.add(cargo);
			 }
			 dbo.closeConnection();


return cargos;
}

---------------//

private void preenchimentoCombo() throws SQLException {

 UsersDAL b = new UsersDAL(this.dbo);
 ArrayList<String> cargos = null;

	  try{
			    if (this.idCargo != -1)
			    {
                            cargos = b.retrieveCargo(this.idCargo);

                            for( String cargo : cargos)
						    {	  
				                         jComboCargo.addItem(cargo);
						    }
			    }
	    } catch (SQLException e){
			    System.out.println("Erro..." + e.getMessage());
			    javax.swing.JOptionPane.showMessageDialog(null, "ERRO: " + e.getMessage(), "Aviso", javax.swing.JOptionPane.ERROR_MESSAGE);
	    }
	    this.idCargo = 0; 
	 }

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.