You are on page 1of 5

Codingan Zkoss

CREATE TABLE person ( name VARCHAR(20) NOT NULL, gender VARCHAR(10), birthdate DATE, age INT, PRIMARY KEY (name) ) insert into person (name, gender, 'Male', '1988-12-24', 29); insert into person (name, gender, , '1977-11-20', 28); insert into person (name, gender, ', '1990-01-01', 19); insert into person (name, gender, ', '1966-04-18', 64); package com.baculsoft.zk.bean; import java.util.Date; public class Person { private private private private String name; int age; String gender; Date birthdate; birthdate, age) values ('Christian Gonzales', birthdate, age) values ('Firman Utina', 'Male' birthdate, age) values ('Irfan Bachdim', 'Male birthdate, age) values ('Julia Perez', 'Female

public Person(String name, int age, String gender, Date birthdate) { this.name = name; this.age = age; this.gender = gender; this.birthdate = birthdate; } public Person() { } // other setter getter <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class catalog="test" name="com.baculsoft.zk.bean.Person" table="person"> <id name="name" type="string"> <column length="20" name="name"/> <generator class="assigned"/> </id> <property name="gender" type="string"> <column length="10" name="gender"/> </property> <property name="birthdate" type="date">

<column length="10" name="birthdate"/> </property> <property name="age" type="java.lang.Integer"> <column name="age"/> </property> </class> </hibernate-mapping> <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration D TD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prope rty> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</pr operty> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</ property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">password</property> <property name="hibernate.connection.autocommit">true</property> <property name="hibernate.show_sql">true</property> <mapping resource="com/baculsoft/zk/bean/Person.hbm.xml"/> </session-factory> </hibernate-configuration> package com.baculsoft.zk.dao; import com.baculsoft.zk.bean.Person; import java.util.List; import org.hibernate.Session; // disini pake hibernate util bawaan zkoss import org.zkoss.zkplus.hibernate.HibernateUtil; public class PersonDAO { public List<Person> findAll() throws Exception { Session session = HibernateUtil.getSessionFactory().openSession(); return (List<Person>) session.createCriteria(Person.class).list(); } public boolean delete(Person person) throws Exception { if (person == null) { throw new Exception("empty person"); } Session session = HibernateUtil.getSessionFactory().openSession(); session.delete(person); session.flush(); session.close(); return true; } public boolean insert(Person person) throws Exception { if (person == null) { throw new Exception("empty person"); } Session session = HibernateUtil.getSessionFactory().openSession();

session.save(person); session.flush(); session.close(); return true; } public boolean update(Person person) throws Exception { if (person == null) { throw new Exception("empty person"); } Session session = HibernateUtil.getSessionFactory().openSession(); session.update(person); session.flush(); session.close(); return true; } } package com.baculsoft.zk.service; import import import import import com.baculsoft.zk.bean.Person; com.baculsoft.zk.dao.PersonDAO; java.util.List; org.apache.log4j.Logger; org.zkoss.zk.ui.util.GenericForwardComposer;

public class PersonService extends GenericForwardComposer { private PersonDAO dao = new PersonDAO(); private Person person = new Person(); private Logger logger = Logger.getLogger(PersonService.class); public Person getCurrent() { return person; } public void setCurrent(Person person) { this.person = person; } public List<Person> getAllPersons() { try { return dao.findAll(); } catch (Exception ex) { logger.error(ex.getMessage(), ex); return null; } } public void onClick$add() { try { Person nowPerson = new Person(person.getName(), person.getAge(), per son.getGender(), person.getBirthdate()); dao.insert(nowPerson); } catch (Exception ex) { logger.error(ex.getMessage(), ex); }

} public void onClick$update() { try { dao.update(person); } catch (Exception ex) { logger.error(ex.getMessage(), ex); } } public void onClick$delete() { try { dao.delete(person); } catch (Exception ex) { logger.error(ex.getMessage(), ex); } } } <?init class="org.zkoss.zkplus.databind.AnnotateDataBinderInit"?> <window id="win" width="900px" title="Person" border="normal" apply="com.baculsoft.zk.service.PersonService"> <listbox id="box" multiple="true" rows="5" model="@{win$composer.allPersons, load-after='add.onClick, delete.onClic k, update.onClick'}" selectedItem="@{win$composer.current}"> <listhead> <listheader <listheader <listheader <listheader > </listhead> <listitem self="@{each='person'}" value="@{person}"> <listcell label="@{person.name}"/> <listcell label="@{person.age}"/> <listcell label="@{person.gender}"/> <listcell label="@{person.birthdate}"/> </listitem> </listbox> <groupbox> <caption label="Person" /> Name: <textbox id="name" cols="25" value="@{win$composer.current.name}" /> Age: <intbox id="age" cols="1" value="@{win$composer.current.age}" /> Gender: <combobox id="gender" value="@{win$composer.current.gender}"> <comboitem label="Male" description="Alpha Male"/> <comboitem label="Female" description="Girl Power"/> <comboitem label="Others" description="WTF..?!?!"/> </combobox> Birthdate: <datebox id="birthdate" cols="8" value="@{win$composer.curren t.birthdate}" /> <button id="add" label="Add" height="24px"/> <button id="update" label="Update" height="24px"/> <button id="delete" label="Delete" height="24px"/> </groupbox> label="Name" sort="auto(name)"/> label="Age" width="80px" sort="auto(age)" /> label="Gender" width="170px" sort="auto(gender)" /> label="Birthdate" width="170px" sort="auto(birthdate)" /

</window> <?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/ j2ee/web-app_2_4.xsd"> <description><![CDATA[Edw's ZK Application]]></description> <display-name>ZKossTest</display-name> <listener> <description>ZK listener for session cleanup</description> <listener-class>org.zkoss.zk.ui.http.HttpSessionListener</listener-class > </listener> <servlet> <description>ZK loader for ZUML pages</description> <servlet-name>zkLoader</servlet-name> <servlet-class>org.zkoss.zk.ui.http.DHtmlLayoutServlet</servlet-class> <init-param> <param-name>update-uri</param-name> <param-value>/zkau</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>zkLoader</servlet-name> <url-pattern>*.zul</url-pattern> </servlet-mapping> <servlet> <description>The asynchronous update engine for ZK</description> <servlet-name>auEngine</servlet-name> <servlet-class>org.zkoss.zk.au.http.DHtmlUpdateServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>auEngine</servlet-name> <url-pattern>/zkau/*</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.zul</welcome-file> </welcome-file-list> </web-app>

You might also like