0

Combobox population from database -please help

asked 2012-02-06 16:36:36 +0800

mortoza gravatar image 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 flag offensive retag edit

11 Replies

Sort by ยป oldest newest

answered 2012-02-18 15:12:48 +0800

Matze2 gravatar image 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.

link publish delete flag offensive edit
Your reply
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow

RSS

Stats

Asked: 2012-02-06 16:36:36 +0800

Seen: 574 times

Last updated: Feb 18 '12

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More