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-10 00:52:42 +0800

samchuang gravatar image samchuang
4084 4

Hi

if you use ZK5, you could refer to here, if you use ZK 6, you can refer to here

link publish delete flag offensive edit

answered 2012-02-10 17:55:58 +0800

javafan gravatar image javafan
12

Hi samchuang
Thank you your reply. I have gone through the referred documents but I am wondering I must have done some mistake somewhere! Could you please help me where did you make the mistake, my country combo box is not displaying anything, now I am getting an exception.
Please help. regards, Mortoza

CountryController.jave

public class CountryController extends GenericForwardComposer {

    private static final long serialVersionUID = -9145887024839938515L;
    private Country current = new Country();
    private CountryDAO cDao = new CountryDAO();
    private Textbox id;
    private Combobox country;

    public void afterCompose() {
   //     super.afterCompose();
        country.setModel(ListModels.toListSubModel(new ListModelList(getAllItems())));

    }
    
    List<Country> getAllItems() {
    	
    	CountryDAO cDAO=new CountryDAO();
    	return cDAO.findAll();
    	
    }
   


CountryDAO.java

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;
	}


ZUL file

<?init class="org.zkoss.zkplus.databind.AnnotateDataBinderInit"?>

<?page title="Country" contentType="text/html;charset=UTF-8"?>
<zk>
<window closable="true" minimizable="true" id="win" title="Customer profile" width="800px" height="600px" border="normal"  apply="com.integms.country.CountryController">
New Content Here!
 <combobox id="country" cols="25" model="@{country}" value="@{win$composer.current.country}" />

</window>
</zk>

I am now getting the following error. Please help.

type Exception report

message 

description The server encountered an internal error () that prevented it from fulfilling this request.

exception 

org.zkoss.zk.ui.UiException: Expects java.util.Set, java.util.List, java.util.Map, Object[], Enum Class, GroupsModel, ListModel,or BindingListModel only. class org.zkoss.zul.Combobox
	org.zkoss.zkplus.databind.ListModelConverter.coerceToUi(ListModelConverter.java:63)
	org.zkoss.zkplus.databind.Binding.myLoadAttribute(Binding.java:384)
	org.zkoss.zkplus.databind.Binding.loadAttribute(Binding.java:337)
	org.zkoss.zkplus.databind.DataBinder.loadAttrs(DataBinder.java:591)
	org.zkoss.zkplus.databind.DataBinder.loadComponent0(DataBinder.java:546)
	org.zkoss.zkplus.databind.DataBinder.loadAll(DataBinder.java:575)
	org.zkoss.zkplus.databind.AnnotateDataBinderInit.doAfterCompose(AnnotateDataBinderInit.java:189)
	org.zkoss.zk.ui.impl.RealInits.doAfterCompose(Initiators.java:107)
	org.zkoss.zk.ui.impl.UiEngineImpl.execNewPage0(UiEngineImpl.java:395)
	org.zkoss.zk.ui.impl.UiEngineImpl.execNewPage(UiEngineImpl.java:309)
	org.zkoss.zk.ui.http.DHtmlLayoutServlet.process(DHtmlLayoutServlet.java:220)
	org.zkoss.zk.ui.http.DHtmlLayoutServlet.doGet(DHtmlLayoutServlet.java:140)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:722)



Thanks in advance. Mortoza

link publish delete flag offensive edit

answered 2012-02-13 01:11:47 +0800

samchuang gravatar image samchuang
4084 4

Hi

that exception means you set the Combobox wrong model

model="@{country}"

link publish delete flag offensive edit

answered 2012-02-16 17:16:36 +0800

mortoza gravatar image mortoza
54

Hi Samchuang,
Thanks. I have done some changes, now I can see some texts in the combo box loaded like
"com.integms.country.Country@1187d2f"
com.integms.country.Country@1721e22
com.integms.country.Country@c2c65b

any additional tips?

regards
Mortoza

link publish delete flag offensive edit

answered 2012-02-17 08:12:59 +0800

Matze2 gravatar image Matze2
773 7

It invokes toString() on your Country object. Two choices:
1) you implement toString()
2) you have another property (e.g. getName() or getLabel()) and specify your comboitem explcitly (example with property name):

<combobox id="country" cols="25" model="@{country}" value="@{win$composer.current.country}">
	<comboitem self="@{each=_country}" value="@{_country}" label="@{_country.name}" />
</combobox>

link publish delete flag offensive edit

answered 2012-02-17 14:37:55 +0800

mortoza gravatar image mortoza
54

Hi Matze2
Thank you so much for your help. Unfortunately it's still not working. I realised table name and field name is same "country" and that might be confusing to me. Therefore, I renamed the field name country to countryname. I provide below all the code again for your quick review and help please.

customer.zul

						<combobox id="country" cols="25" model="@{country}" value="@{win$composer.current.country}"  apply="com.integms.country.CountryCombo">
						    <comboitem self="@{each=_country}" value="@{_country}" label="@{_country.countryname}" />
						</combobox>

Country.java

public class Country {
   
    private String id;
    private String countryname;
    
    public Country() {
    }

    public Country(String id, String countryname) {
        this.id = id;
        this.countryname = countryname;
        
    }

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getCountryname() {
		return countryname;
	}

	public void setCountryname(String countryname) {
		this.countryname = countryname;
	}    
}

CountryDAO.java


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, countryname from country");

			// fetch all events from database
			Country c;

			while (rs.next()) {
				c = new Country();
				c.setId(rs.getString(1));
				c.setCountryname(rs.getString(2));
				allCountry.add(c);
			}
		} 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 countryname from country");

			// fetch all events from database
			Country c;

			while (rs.next()) {
				c = new Country();
				c.setCountryname(rs.getString(1));
				allCountry.add(c);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			ds.close();
		}

		return allCountry;
	}
	

Exception when I run

org.zkoss.zk.ui.UiException: Expects java.util.Set, java.util.List, java.util.Map, Object[], Enum Class, GroupsModel, ListModel,or BindingListModel only. class org.zkoss.zul.Combobox
	at org.zkoss.zkplus.databind.ListModelConverter.coerceToUi(ListModelConverter.java:63)
	at org.zkoss.zkplus.databind.Binding.myLoadAttribute(Binding.java:384)
	at org.zkoss.zkplus.databind.Binding.loadAttribute(Binding.java:337)
	at org.zkoss.zkplus.databind.DataBinder.loadAttrs(DataBinder.java:591)
	at org.zkoss.zkplus.databind.DataBinder.loadComponent0(DataBinder.java:546)
	at org.zkoss.zkplus.databind.DataBinder.loadAll(DataBinder.java:575)
	at org.zkoss.zkplus.databind.AnnotateDataBinderInit.doAfterCompose(AnnotateDataBinderInit.java:189)
	at org.zkoss.zk.ui.impl.RealInits.doAfterCompose(Initiators.java:107)
	at org.zkoss.zk.ui.impl.UiEngineImpl.execNewPage0(UiEngineImpl.java:395)
	at org.zkoss.zk.ui.impl.UiEngineImpl.execNewPage(UiEngineImpl.java:309)
	at org.zkoss.zk.ui.http.DHtmlLayoutServlet.process(DHtmlLayoutServlet.java:220)
	at org.zkoss.zk.ui.http.DHtmlLayoutServlet.doGet(DHtmlLayoutServlet.java:140)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)

Thanks in advance for helping again.
regards

link publish delete flag offensive edit

answered 2012-02-17 14:40:05 +0800

Matze2 gravatar image Matze2
773 7

samchuang already answered that.

link publish delete flag offensive edit

answered 2012-02-17 14:41:18 +0800

mortoza gravatar image mortoza
54

Hi Matze2
This may be mentioned here that the zul code lines are from customer.zul file and composer if customer composer where composer is customer composer. do you also suggest to apply countryController? I also tried that but didnt work.

link publish delete flag offensive edit

answered 2012-02-17 14:47:04 +0800

Matze2 gravatar image Matze2
773 7

No idea what you are talking about.
The question is: how is

model="@{country}"
be filled?

link publish delete flag offensive edit

answered 2012-02-17 17:31:42 +0800

mortoza gravatar image mortoza
54

I just followed your code in zul file. will this be model="@{country.countryname}" ?

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: 578 times

Last updated: Feb 18 '12

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