go4it

just do it

ZK数据库CURD--Todo

1.建立mysql数据库和表:

create database zktodo  character set=utf8;

create table event(

   id varchar(55),

  name varchar(55),

  priority int,

  date date

);

 

2.新建Dynamic web project,添加ZK supports,建立完成之后添加mysql jdbc jar包到项目build path

3.建立entity和dao

package com.zktodo.dao;

import java.util.Date;

public class MyEvent {
	private String id;
	private String name;
	private int priority;
	private Date date;
	
	public MyEvent(){}
	
	public MyEvent(String id,String name,int priority,Date date){
		this.id = id;
		this.name = name;
		this.priority = priority;
		this.date = date;
	}
	public Date getDate() {
		return date;
	}
	public void setDate(Date date) {
		this.date = date;
	}
	public int getPriority() {
		return priority;
	}
	public void setPriority(int priority) {
		this.priority = priority;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
}
 
package com.zktodo.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;


public class EventDAO {
	private String url = "jdbc:mysql://localhost:3306/zktodo";

	private String user = "root";

	private String pwd = "root";

	public EventDAO() {
		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
	
	public List findAll(){
		Statement stmt = null;
		Connection conn = null;
		List allEvents = new ArrayList();
		try {
			// get connection
			conn = DriverManager.getConnection(url, user, pwd);
			stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery("select * from event");

			// fetch all events from database
			MyEvent evt;
			while (rs.next()) {
				evt = new MyEvent();
				evt.setId(rs.getString(1));
		        evt.setName(rs.getString(2));
				evt.setPriority(rs.getInt(3));
				evt.setDate(rs.getDate(4));

				allEvents.add(evt);
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			try {
				stmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}		
		return allEvents;
	}
	
	public boolean delete(MyEvent evt){
		Connection conn = null;
		Statement stmt = null;
		boolean result = false;
		try {
			// get connection
			conn = DriverManager.getConnection(url, user, pwd);
			stmt = conn.createStatement();
			if (stmt.executeUpdate("delete from event where id = '" + evt.getId() + "'") > 0);
			result = true;
			
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			try {
				stmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
		return result;
	}
	
	public boolean insert(MyEvent evt){
		Connection conn = null;
		Statement stmt = null;
		boolean result = false;
		try {
			// get connection
			conn = DriverManager.getConnection(url, user, pwd);
			stmt = conn.createStatement();
			if (stmt.executeUpdate("insert into event(id,name,priority,date) " +
					"values ('" + UUID.randomUUID().toString() + "','" + evt.getName() +
					"'," + evt.getPriority() + ",'" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
                                        .format(evt.getDate()) + "')") > 0);
			result = true;
			
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			try {
				stmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
		return result;
	}
	
	public boolean update(MyEvent evt){
		Connection conn = null;
		Statement stmt = null;
		boolean result = false;
		try {
			// get connection
			conn = DriverManager.getConnection(url, user, pwd);
		    stmt = conn.createStatement();				
			if (stmt.executeUpdate("update event set name = '" + evt.getName() + 
					"', priority = " + evt.getPriority() + ", date = '" + 
					new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(evt.getDate())+ 
					"' where id = '" + evt.getId() + "'") > 0);
			result = true;
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			try {
				stmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
		return result;
	}

}

4.新建index.zul文档

<?page title="zk todo index" contentType="text/html;charset=UTF-8"?>
<zk>
<window title="To do list" width="640px" border="normal">

<zscript>

    
    import com.zktodo.dao.EventDAO; 
    import java.util.ArrayList; 
    import java.text.SimpleDateFormat; 
    import java.util.UUID;
    import com.zktodo.dao.MyEvent;
    
    EventDAO evtdao = new EventDAO(); 
    List allEvents = evtdao.findAll();  
    
    void add(){ 
		//insert into database 
		MyEvent newEvt = new MyEvent(UUID.randomUUID().toString(),
		  name.value,priority.value.intValue(),date.value);
		evtdao.insert(newEvt);

		//synchronized data with database 
		allEvents = evtdao.findAll();

		//insert a listEvent into the listbox 
		Listitem li = new Listitem(); 
		li.setValue(newEvt); 
		li.appendChild(new Listcell(name.value)); 
		li.appendChild(new Listcell(priority.value.toString())); 
		li.appendChild(new Listcell(new SimpleDateFormat("yyyy-MM-dd").format(date.value)));
		box.appendChild(li);
		}
		
		void update(){
		//update database
		MyEvent editEvt = (MyEvent)box.selectedItem.value;
		editEvt.setName(name.value);
		editEvt.setPriority(priority.value);
		editEvt.setDate(date.value);
		  evtdao.update(editEvt);
		
		//update listbox
		List children = box.selectedItem.children;
		((Listcell)children.get(0)).label = name.value;
		((Listcell)children.get(1)).label = priority.value.toString();
		((Listcell)children.get(2)).label = new SimpleDateFormat("yyyy-MM-dd").format(date.value);
		} 
		
		void delete(){
		evtdao.delete((MyEvent)box.selectedItem.value);
		box.removeItemAt(box.getSelectedIndex());
		cleargb();

		}
		void move(){
		name.value = ((MyEvent)box.selectedItem.value).getName();
		priority.value = ((MyEvent)box.selectedItem.value).getPriority();
		date.value = ((MyEvent)box.selectedItem.value).getDate();
		}
		
		void cleargb(){
		name.value = null;
		priority.value = null;
		date.value = null;
		}
  </zscript>  

<listbox id="box" multiple="true"  rows="4" onSelect="move()">
  <listhead>
   <listheader label="Item" />
   <listheader label="Priority" width="50px" />
   <listheader label="Opened" width="90px" />
  </listhead>
  <listitem forEach="${allEvents }" value="${ each }">
   <listcell label="${each.name }"/>
   <listcell label="${each.priority }"/>
   <listcell label="${each.date }"/>
  </listitem>
 </listbox>
    <groupbox>
              <caption label="Event" />
       Item: <textbox id="name" cols="50" />
       Priority: <intbox id="priority" cols="1" />
       Date: <datebox id="date" cols="8"/>
       <button label="Add" width="36px" height="24px" onClick="add()" />
       <button label="Update" width="46px" height="24px" onClick="update()"/>
       <button label="Delete" width="46px" height="24px" onClick="delete()"/>
    </groupbox>
</window>

</zk>

 

5.运行结果:

image

初步还行,缺点如下:

1.没有分页,数目多了,只有下拉条

2.缺乏输入合法性判断(包括输入空值)

3.没有唯一性判断

4.缺乏选中多个删除和全部删除

 

优点:

1.界面好看

2.一个文件搞定CURD

3.中文化很顺利

 

6.要点

(1)zscript代码

<zscript>

    
    import com.zktodo.dao.EventDAO; 
    import java.util.ArrayList; 
    import java.text.SimpleDateFormat; 
    import java.util.UUID;
    import com.zktodo.dao.MyEvent;
    
    EventDAO evtdao = new EventDAO(); 
    List allEvents = evtdao.findAll();  
    
    void add(){ 
		//insert into database 
		MyEvent newEvt = new MyEvent(UUID.randomUUID().toString(),
		  name.value,priority.value.intValue(),date.value);
		evtdao.insert(newEvt);

		//synchronized data with database 
		allEvents = evtdao.findAll();

		//insert a listEvent into the listbox 
		Listitem li = new Listitem(); 
		li.setValue(newEvt); 
		li.appendChild(new Listcell(name.value)); 
		li.appendChild(new Listcell(priority.value.toString())); 
		li.appendChild(new Listcell(new SimpleDateFormat("yyyy-MM-dd").format(date.value)));
		box.appendChild(li);
		}
		
		void update(){
		//update database
		MyEvent editEvt = (MyEvent)box.selectedItem.value;
		editEvt.setName(name.value);
		editEvt.setPriority(priority.value);
		editEvt.setDate(date.value);
		  evtdao.update(editEvt);
		
		//update listbox
		List children = box.selectedItem.children;
		((Listcell)children.get(0)).label = name.value;
		((Listcell)children.get(1)).label = priority.value.toString();
		((Listcell)children.get(2)).label = new SimpleDateFormat("yyyy-MM-dd").format(date.value);
		} 
		
		void delete(){
		evtdao.delete((MyEvent)box.selectedItem.value);
		box.removeItemAt(box.getSelectedIndex());
		cleargb();

		}
		void move(){
		name.value = ((MyEvent)box.selectedItem.value).getName();
		priority.value = ((MyEvent)box.selectedItem.value).getPriority();
		date.value = ((MyEvent)box.selectedItem.value).getDate();
		}
		
		void cleargb(){
		name.value = null;
		priority.value = null;
		date.value = null;
		}
  </zscript>  
 

(2)forEach循环显示

  <listitem forEach="${allEvents }" value="${ each }">
   <listcell label="${each.name }"/>
   <listcell label="${each.priority }"/>
   <listcell label="${each.date }"/>
  </listitem>
 

(3)注册事件:

<listbox id="box" multiple="true"  rows="4" onSelect="move()">
.....
       <button label="Add" width="36px" height="24px" onClick="add()" />
       <button label="Update" width="46px" height="24px" onClick="update()"/>
       <button label="Delete" width="46px" height="24px" onClick="delete()"/>

posted on 2009-02-08 22:13  cxccbv  阅读(1407)  评论(0编辑  收藏  举报

导航