mortoza
54

Hi I am trying to populate country name from my database into the combobox and but still unsuccessful. Could you please tell me where is the mistake that I have ?
The codes of different files are given below-

in Customer. zul file
inside grid:

        <cell colspan="1">
<combobox id="country" cols="25" value="@{win$composer.current.country}" apply="com.integms.country.CountryCombo" /> </cell>  CountryCombo.java file package com.integms.country; import java.util.ArrayList; import java.util.List; import org.zkoss.zk.ui.util.GenericForwardComposer; import org.zkoss.zul.Combobox; import org.zkoss.zul.ListModelList; import org.zkoss.zul.ListModels; public class CountryCombo extends GenericForwardComposer { /** * */ private static final long serialVersionUID = 1L; Combobox country; public void afterCompose() { //super.afterCompose(); System.out.println("CountryCombo1"); country.setModel(ListModels.toListSubModel(new ListModelList(getAllItems()))); } List <Country> getAllItems() { //return all items List<Country> allCountry = new ArrayList<Country>(); CountryDAO cDAO=new CountryDAO(); allCountry=cDAO.findAll(); System.out.println("CountryCombo2"); return allCountry; } }  CountryDAO.java package com.integms.country; /** * Country DAO. * * @author Mortoza */ import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.List; import com.integms.lib.DataSource; public class CountryDAO { public List<Country> findAll() { DataSource ds = new DataSource(); List<Country> allCountry = new ArrayList<Country>(); try { // get connection Statement stmt = ds.getStatement(); ResultSet rs = stmt.executeQuery("select id, country from country"); // fetch all events from database Country cust; while (rs.next()) { cust = new Country(); cust.setId(rs.getString(1)); cust.setCountry(rs.getString(2)); allCountry.add(cust); } } catch (SQLException e) { e.printStackTrace(); } finally { ds.close(); } return allCountry; } public List<Country> findAllCountry() { DataSource ds = new DataSource(); List<Country> allCountry = new ArrayList<Country>(); try { // get connection Statement stmt = ds.getStatement(); ResultSet rs = stmt.executeQuery("select country from country"); // fetch all events from database Country cust; while (rs.next()) { cust = new Country(); cust.setCountry(rs.getString(2)); allCountry.add(cust); } } catch (SQLException e) { e.printStackTrace(); } finally { ds.close(); } return allCountry; } public boolean delete(Country cust) { return execute("delete from country where id = '" + cust.getId() + "'"); } public boolean insert(Country cust) { return execute("insert into country (id, country) " + "values ('"+cust.getId()+"' , '"+cust.getCountry()+"'"); } public boolean update(Country cust) { return execute("update country set country = '"+cust.getCountry()+"' where id = '" + cust.getId() + "'"); } private boolean execute(String sql) { DataSource ds = new DataSource(); try { Statement stmt = ds.getStatement(); stmt.execute(sql); if (stmt != null) { stmt.close(); } return true; } catch (SQLException e) { return false; } finally { ds.close(); } } }  delete retag edit ## 11 Replies Sort by » oldest newest answered 2012-02-18 15:12:48 +0800 Matze2 773 7 In case of the composer above, you would have the method win$composer.allItems.

But ... I saw, that you have lots of mistakes in your composer: wrong method names, wrong visibility.
I strongly recommend that you go at first through the tutorials.
I recommended the following two tutorials for somebody else recently, both available in the ZK Documentation, "Getting Started" section:

Example 1: "Tutorial" (the first link on the Documentation page). There is a data-binding example at the end. This should be good start.
Example 2: The article "Create a database-driven application" has an example for listbox, which is very similar to the combobox use case above.

[hide preview]