第二個範例 (Part V):修改
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
請勿轉貼
看其他教材
修改存貨資料
修改存貨資料分成兩個主要的步驟:若使用者點選某一項工作,被選擇的工作項目以及 其相關值,會被複製到相對應的輸入欄位;然後,等到使用者修改完資料後,使用者 需要按"修改"按鈕來完成修改工作。 首先,我們要為 <listbox> 註冊一個 onSelect 的事件處理方法 move(); 該註冊的事件處理過程為:一旦使用者選擇了(也就是觸動了 onSelect 事件) <listbox> 中的某一個存貨資料,ZK 開始執行 move() 方法,而該方法所做的 事情就是把該存貨資料的值分別複製到相對應的輸入欄位。註冊的原始碼如下:<listbox id="box" multiple="true" rows="4" onSelect="move()">
void move() {
Product p = (Product) box.selectedItem.value;
num.value = p.getId();
name.value = p.getName();
price.value = new java.math.BigDecimal(p.getPrice());
qty.value = p.getQty();
}
<button label="修改" width="46px" height="24px" onClick="update()"/>
void update(){
// 修改 box 和 allItems 物件的內容
Product upp = (Product) box.selectedItem.value;
upp.setId(num.value.intValue());
upp.setName(name.value);
upp.setPrice(price.value.doubleValue());
upp.setQty(qty.value.intValue());
allItems.set(box.getSelectedIndex(), upp);
// 修改資料庫的內容
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1/eric", "jlu", "newpasswd");
stmt = conn.createStatement();
String uSQL = "update Product set name='" + name.value + "', price=" + price.value +
", qty=" + qty.value + " where id=" + num.value;
if (stmt.executeUpdate(uSQL) <= 0)
throw new SQLException("資料修改失敗");
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
// 修改 listbox 的內容
List children = box.selectedItem.children;
((Listcell)children.get(0)).label = num.value.toString();
((Listcell)children.get(1)).label = name.value;
((Listcell)children.get(2)).label = price.doubleValue().toString();
((Listcell)children.get(3)).label = qty.value.toString();
}
Written by: 國立中興大學資管系呂瑞麟 Eric Jui-Lin Lu
沒有留言:
張貼留言