MySQL table and Combobox/TreeTable

Hi, I want to show in a treetable or combobox unique values from a MySQL table.

For example, if in column 1 of the table, I have 3 rows named “A”, and 3 rows named “B”, I would like to show only once “A” and “B”, instead of six different options (that is what I have so far)…

any ideas on how to do it??

thanks!!!

any ideas on this???

It’s almost impossible to know how to guide you, as I don’t know how you are populating the container behind the combo box - there are many ways to do this.

If you are retrieving just the names in a SQL statement, change the SQL from

SELECT NAME FROM TABLE

to

SELECT DISTINCT NAME FROM TABLE

Other than that, show us the code.

Cheers,

Charles.

Hi:

Thank you for your answer… what I have is this:


ComboBox combo1 = new ComboBox("Please select a name");
		combo1.setContainerDataSource(contenedor);
		combo1.setItemCaptionPropertyId("firstName");
		layout.addComponent(combo1);

where “contenedor” is a SQLContainer for a table like the on shown below. As you can see, the name "Juan appears twice and I would like to show it only once. Is it possible?

reagards, and thank you very much
12234.jpg
12235.jpg

Hi,

Well, the most important bit of code is the bit you haven’t shown us! How are you creating the container?

I’ve never used SQLContainer myself, so I don’t know exactly how it works. The contents of the container control what appears in the combo box, so you need to limit the contents of the container. A quick look at the source code for SQLContainer suggests that something like the following (untested, probably doesn’t even compile) would do.

Query query = new FreeformQuery("SELECT DISTINCT FIRSTNAME FROM PEOPLE",null, connectionPool);
SQLContainer c = new SQLContainer(query);

If you need to use the same container for two different tables/combo boxes, then this approach wouldn’t work.

Cheers,

Charles.

Hi Charles, thank you very much for your answer…here is the rest of the code:

before I populate the combobox, I create the “contenedor” SQLContainer:

Conexion conexion=new Conexion();
		SQLContainer contenedor= conexion.getContainer6_Users();

where Conexion() is:

package com.example.prueba11;

import java.sql.SQLException;
import com.vaadin.addon.sqlcontainer.SQLContainer;
import com.vaadin.addon.sqlcontainer.connection.JDBCConnectionPool;
import com.vaadin.addon.sqlcontainer.connection.SimpleJDBCConnectionPool;
import com.vaadin.addon.sqlcontainer.query.TableQuery;

public class Conexion {
	
	public TableQuery q6;
	private JDBCConnectionPool connectionPool = null;
	private SQLContainer Container6_Users=null;
	
	public Conexion() {
	  	  initConnectionPool();
	  	  initContainers();
	    }
	
	private void initConnectionPool() {
		try {
          	connectionPool=new SimpleJDBCConnectionPool(
                  	"com.mysql.jdbc.Driver",
                  	"jdbc:mysql://localhost:3306/data", "root", "hola", 2, 5);
      	} catch (SQLException e) {
        	  e.printStackTrace();
        }	
	}
	
	public void initContainers()  {
		try{
					
				q6=new TableQuery("users", connectionPool);
				q6.setVersionColumn("VERSION");
				
				Container6_Users=new SQLContainer(q6);
				
			} catch (SQLException e) {
	      	  e.printStackTrace();
	      }
		}

	public SQLContainer getContainer6_Users() {
		return Container6_Users;
	}

	public void setContainer6_Users(SQLContainer container6_Users) {
		Container6_Users = container6_Users;
	}
}

This code works perfect so far, except for the filter distinct for the combobox. For example, to filter in a simple treetable I use:

filter1= new Compare.Equal(que, valor);

which works just fine…

Can you help me out with this?..thank you very much for all your help…

regards,

Hugo