易于数据库访问的ZK特性

2 篇文章 0 订阅

org.zkoss.zk.ui.event.EventThreadCleanup 接口

就像以前强调过的,在finally子句中关闭连接是很重要的,这样每个连接都会被正确的返回连接池。

为使你的应用程序更加健壮(robust),你可以实现org.zkoss.zk.ui.event.EventThreadCleanup 接口来关闭任何处在等待之际的连接和语句(any pending connections and statements),以防你的一些应用程序代码忘记了在finally子句中关闭它们。

但是,关闭处在等待之际的连接和语句实际上依赖于你使用的服务器。你需要参考服务器的文档来得知如何编写关闭代码。

[提示]: 在许多情况下,并不需要(且并不容易)提供这样的方法,因为多数连接池的实现可以循环一个连接若调用了连接池的finalized方法(because most implementation of connection pooling be recycled a connection if its finalized method is called)。

在EL表达式中访问数据库

除了在事件监听器中访问数据库,使用EL表达式访问数据库填充一个属性也是很常见的。在下面的例子中,我们从数据库取出数据,并使用EL表达式将它们用listbox展示出来。

 

 

 

<zscript>
import my.CustomerManager;
customers = new CustomerManager().findAll(); //load from database
</zscript>
<listbox id="personList" width="800px" rows="5">
<listhead>
<listheader label="Name"/>
<listheader label="Surname"/>
<listheader label="Due Amount"/>
</listhead>
<listitem value="${each.id}" forEach="${customers}">
<listcell label="${each.name}"/>
<listcell label="${each.surname}"/>
<listcell label="${each.due}"/>
</listitem>
</listbox>

 

有几种方式来实现findAll方法。

读取所有数据并将其拷贝到链表

最简单的方式是在findAll方法内获取所有的数据,将它们拷贝到一个列表,然后关闭连接。

 

 

 

public class CustomerManager {
public List findAll() throws Exception {
DataSource ds = (DataSource)new InitialContext()
.lookup("java:comp/env/jdbc/MyDB");

Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
List results = new LinkedList();
try {
conn = ds.getConnection();
stmt = conn.createStatement();
rs = stmt.executeQuery("SELECT id, name, surname FROM customers");
while (rs.next()) {
long id = rs.getInt("id");
String name = rs.getString("name");
String surname = rs.getString("surname");
results.add(new Customer(id, name, surname));
}
return results;
} finally {
if (rs != null) try { rs.close(); } catch (SQLException ex) [}
if (stmt != null) try { stmt.close(); } catch (SQLException ex) [}
if (conn != null) try { conn.close(); } catch (SQLException ex) [}
}
}
}

 

实现org.zkoss.zk.ui.util.Initiator接口

你可以使用init指令来加载数据,以代替在试图中混合使用Java代码。

 

 

 

<?init class="my.AllCustomerFinder" arg0="customers"?>

<listbox id="personList" width="800px" rows="5">
<listhead>
<listheader label="Name"/>
<listheader label="Surname"/>
<listheader label="Due Amount"/>
</listhead>
<listitem value="${each.id}" forEach="${customers}">
<listcell label="${each.name}"/>
<listcell label="${each.surname}"/>
<listcell label="${each.due}"/>
</listitem>
</listbox>

 

然后,使用org.zkoss.zk.ui.util.Initiator接口实现my.CustomerFindAll类。

 

 

 

import org.zkoss.zk.ui.Page;
import org.zkoss.zk.ui.util.Initiator;

public class AllCustomerFinder implements Initiator {
public void doInit(Page page, Object[] args) {
try {
page.setVariable((String)args[0], new CustomerManager().findAll());
//Use setVariable to pass the result back to the page
} catch (Exception ex) {
throw UiException.Aide.wrap(ex);
}
}
public void doCatch(Throwable ex) { //ignore
}
public void doFinally() { //ignore
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值