1. 参考文献和引用声明
本文主要参考:
Tutorial: Reading from the DB with Netbeans and ZK (Part 2)
http://javadude.wordpress.com/2009/03/24/tutorial-reading-from-the-db-with-netbeans-and-zk-part-2/
我使用了该文的代码,结合另一数据库的数据,稍加修改。原代码版权归原作者所有。
2. 数据库及 persistence.xml
我仍沿用 USERS
http://kingsz1.iteye.com/admin/blogs/356416 数据库。这是 persistence.xml 的内容:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="ZKJPA3PU" transaction-type="RESOURCE_LOCAL">
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
<class>entity.Users</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="openjpa.ConnectionPassword" value="admin"/>
<property name="openjpa.ConnectionDriverName" value="org.apache.derby.jdbc.ClientDriver"/>
<property name="openjpa.ConnectionUserName" value="admin"/>
<property name="openjpa.ConnectionURL" value="jdbc:derby://localhost:1527/USERS"/>
</properties>
</persistence-unit>
</persistence>
3. 实体类及其控制
实体类 Users.java ,从数据库生成,没有修改过。
package entity;
import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
/**
*
* @author admin
*/
@Entity
@Table(name = "USERS")
@NamedQueries({@NamedQuery(name = "Users.findAll", query = "SELECT u FROM Users u"), @NamedQuery(name = "Users.findById", query = "SELECT u FROM Users u WHERE u.id = :id"), @NamedQuery(name = "Users.findByName", query = "SELECT u FROM Users u WHERE u.name = :name"), @NamedQuery(name = "Users.findByLoginid", query = "SELECT u FROM Users u WHERE u.loginid = :loginid"), @NamedQuery(name = "Users.findByPassword", query = "SELECT u FROM Users u WHERE u.password = :password"), @NamedQuery(name = "Users.findByAge", query = "SELECT u FROM Users u WHERE u.age = :age")})
public class Users implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "ID")
private Integer id;
@Basic(optional = false)
@Column(name = "NAME")
private String name;
@Basic(optional = false)
@Column(name = "LOGINID")
private String loginid;
@Basic(optional = false)
@Column(name = "PASSWORD")
private String password;
@Basic(optional = false)
@Column(name = "AGE")
private int age;
public Users() {
}
public Users(Integer id) {
this.id = id;
}
public Users(Integer id, String name, String loginid, String password, int age) {
this.id = id;
this.name = name;
this.loginid = loginid;
this.password = password;
this.age = age;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLoginid() {
return loginid;
}
public void setLoginid(String loginid) {
this.loginid = loginid;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public int hashCode() {
int hash = 0;
hash += (id != null ? id.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof Users)) {
return false;
}
Users other = (Users) object;
if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
return false;
}
return true;
}
@Override
public String toString() {
return "entity.Users[id=" + id + "]";
}
}
实体类的控制类 Controller.java ,从数据库取数,放入 List
package test;
import entity.Users;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import org.zkoss.zul.Window;
public class Controller extends Window {
List users = new ArrayList();
Users selected = new Users();
public Controller() {
EntityManagerFactory emf;
emf = Persistence.createEntityManagerFactory("ZKJPA3PU");
EntityManager em = emf.createEntityManager();
javax.persistence.Query q = em.createQuery("select u from Users as u");
users = q.getResultList();
em.close();
}
public List getUsers() {
return users;
}
public Users getSelected() {
return selected;
}
public void setSelected(Users selected) {
this.selected = selected;
}
}
4. ZK 文件 index.zul
<?xml version="1.0" encoding="UTF-8" ?>
<?page id="indexZUL" title="Sample ZK with JPA + Derby" cacheable="false"
language="xul/html" zscriptLanguage="Java" contentType="text/html;charset=UTF-8"?>
<?init class="org.zkoss.zkplus.databind.AnnotateDataBinderInit"?>
<zk xmlns="http://www.zkoss.org/2005/zul"
xmlns:h="http://www.w3.org/1999/xhtml"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.zkoss.org/2005/zul http://www.zkoss.org/2005/zul/zul.xsd">
<window id="win" title="Sample ZK with JPA + Derby " border="normal" width="97%" height="95%"
mode="overlapped" position="center" use="test.Controller">
<listbox id="lb" model="@{win.users}" selectedItem="@{win.selected}" mold="paging" pageSize="4">
<listhead sizable="true">
<listheader label="Name 姓名" width="150px"/>
<listheader label="LoginID 登录名" width="250px"/>
<listheader label="Password 密码" width="150px"/>
<listheader label="Age 年龄" width="50px"/>
</listhead>
<listitem self="@{each=users}">
<listcell label="@{users.name}"/>
<listcell label="@{users.loginid}"/>
<listcell label="@{users.password}"/>
<listcell label="@{users.age}"/>
</listitem>
</listbox>
<separator bar="true"/>
<hbox>
<groupbox id="gb" mold="3d" width="100%">
<caption label="Selected User"/>
Name: <textbox id="name" cols="30" value="@{win.selected.name}" />
LoginID: <textbox id="loginid" cols="20" value="@{win.selected.loginid}" />
Password: <textbox id="password" cols="20" value="@{win.selected.password}" />
Age: <textbox id="age" cols="6" value="@{win.selected.age}" />
</groupbox>
</hbox>
</window>
</zk>
5. 试运行。
可分页显示数据库记录,每页4个记录,当然太少了,要改就该这里:
pageSize="4"
点击某个记录,在下面的 textbox 显示各字段内容。
如果加入更多的控制,就可以添加、修改、删除记录了。继续读书,继续模仿。
:-
分享到:
相关推荐
springmvc-百里香叶-jpa Spring MVC + Thymeleaf,Spring Security,Spring ORM + Spring Data JPA产品用以下技术开发的对象: Spring Security,Spring MVC和Thymeleaf。 Spring JDBC,Spring ORM,Spring Data JPA...
db-derby-10.11.1.1-bin.zip,apache derby 相当于java db(derby)的完整版
db-derby-10.11.1.1-src.zip,derby数据库源码
db-derby-10.13.1.1-bin.tar.gz
java derby数据库,包含jar文件和官方教程
原型-jpa 带有 JUnit 和 Derby 的 Java JPA 原型。 使用 JPA 和 Eclipse Link 进行数据库开发的基本结构。 使用 JUnit 和 Derby 在 maven 测试周期中测试 JPA 实体。
spring3+hibernate3+derby的完整实例,完全原创,可运行!
收集的学习资料,包括:Derby数据库(V10.9)用户手册(PDF版),用 Apache Derby 进行 Java 数据库开发,用 Apache Derby 进行数据库开发,03开源项目(三)嵌入式数据库Apache Derby(1)(开发指南).pdf ,Apache Derby ...
Apache Derby 是100% Java 编写的内存数据库,...这里直接以derby实现一个例子,方便需要了解的同学参考,也有很多时候写demo或者小工具的时候可以用到这种嵌入式的数据库 springboot+derby+mybatisplus+swagger2例子
经典JAVA EE企业应用实战 基于WEBLOGIC JBOSS的JSF+EJB 3+JPA整合开发。第三部分。
db-derby-10.6.2.1-bin.tar.gz
apache-derby.jar
db-derby所有的jar包 很齐全,绝对真实 本人经过测试
derby-examples, Derby框架的示例应用程序 Derby框架使得编写实时的协作应用程序变得很容易,在 node.js 和浏览器中运行。请参见 http://derbyjs.com/示例聊天http://chat.derbyjs.com/lobby简单的聊天演示请注意,...
Apache Derby数据库 Derby数据库是一个纯用Java实现的珍袖型的数据库,属于Apache的一个开源项目。由于是用Java实现的,所以可以在任何平台上运行;另外一个特点是体积小,免安装,java1.6开始集成了derby数据库,...
NULL 博文链接:https://dncsoft.iteye.com/blog/785656
Java for developer services
Hive 1.2.1 由于网上的资源基本都不可以用了。所以去找了一个传上来
Apache OpenJPA-自述文件 前言 感谢您下载此版本的Apache OpenJPA...自己编译Apache OpenJPA的最佳方法是针对默认的derby数据库运行构建。 $> mvn clean install -Dsurefire.excludes.locking=**/* 针对不同的数据库进