第二個範例 (Part III)
The following examples had been tested on Mozilla's Firefox and Microsoft's IE. The document is provided as is. You are welcomed to use it for non-commercial purpose.Written by: 國立中興大學資管系呂瑞麟 Eric Jui-Lin Lu
請勿轉貼
看其他教材
ZK 與資料庫的結合
在建立完 ZK 的畫面,並且安裝完資料庫之後,剩下的就是如何讓 ZK 與資料庫 來互動;基本上,這就是開始使用 ZK 的事件處理機制。在之前的 Ajax 的範例, 我們使用 Javascript 來完成這個部分,可是由於 ZK 可以與多種語言結合,而且 其預設的語言是 Java,所以我們也以 Java 來完成這項工作。 ZK 和 Java 連結使用的方式有以下三種:- 直接將 Java 程式碼寫在 zul 檔案內,而這些程式碼只需要放在 <zscript> 標籤內即可,它就可以像 JSP 一樣,在載入 zul 時就可以 執行。
- 把 Java 的原始碼放置於另一個檔案內,然後讓 zul 檔把它引入。
- zul 檔可以使用已經編譯好的 Java 類別檔(即 .class 檔)。
<zscript>
// 除了方法,其他的部分只會執行一次
import java.sql.*;
import java.util.*;
Statement stmt = null;
Connection conn = null;
List allItems = new ArrayList();
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1/eric", "jlu", "newpasswd");
stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from Product");
Product p;
while(rs.next()) {
p = new Product();
p.setId(rs.getInt(1));
p.setName(rs.getString(2));
p.setPrice(rs.getDouble(3));
p.setQty(rs.getInt(4));
allItems.add(p);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
</zscript>
<listitem forEach="${allItems}" value="${each}">
<listcell label="${each.id}"/>
<listcell label="${each.name}"/>
<listcell label="${each.price}"/>
<listcell label="${each.qty}"/>
</listitem>
執行該段程式碼後,執行的畫面如下:
<?xml version="1.0" encoding="Big5"?>
<window title="存貨管理系統" width="640px" border="normal" mode="highlighted">
<zscript>
// 除了方法的部分,其他的部分只會執行一次
import java.sql.*;
import java.util.*;
Statement stmt = null;
Connection conn = null;
List allItems = new ArrayList();
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1/eric", "jlu", "newpasswd");
stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from Product");
Product p;
while(rs.next()) {
p = new Product();
p.setId(rs.getInt(1));
p.setName(rs.getString(2));
p.setPrice(rs.getDouble(3));
p.setQty(rs.getInt(4));
allItems.add(p);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
</zscript>
<listbox id="box" multiple="true" rows="4">
<listhead>
<listheader label="料號" width="50px" />
<listheader label="品名" />
<listheader align="right" label="價格" width="60px" />
<listheader align="right" label="數量" width="60px" />
</listhead>
<listitem forEach="${allItems}" value="${each}">
<listcell label="${each.id}"/>
<listcell label="${each.name}"/>
<listcell label="${each.price}"/>
<listcell label="${each.qty}"/>
</listitem>
</listbox>
<groupbox>
<caption label="存貨管理"/>
料號: <intbox id="num" cols="5" />
品名: <textbox id="name" cols="25" />
價格: <decimalbox id="price" cols="8" />
數量: <intbox id="qty" cols="8" />
<div>
<button label="新增" width="46px" height="24px"/>
<button label="修改" width="46px" height="24px"/>
<button label="刪除" width="46px" height="24px"/>
</div>
</groupbox>
</window>
Written by: 國立中興大學資管系呂瑞麟 Eric Jui-Lin Lu
沒有留言:
張貼留言