第二個範例 (Part VI):刪除
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
請勿轉貼
看其他教材
刪除存貨資料
刪除存貨資料的工作也是分成兩個主要的步驟,而且其步驟跟之前的 add() 和 update() 的程式碼類似:若使用者點選某一項工作,被選擇的工作項目以及 其相關值,會被複製到相對應的輸入欄位(這步驟與 update() 的第一個部分 相同);然後,等到使用者點選"刪除"按鈕,程式需要把資料從資料庫中刪除, 然後把輸入欄位內的資料清除(清除資料的部分跟 add() 的最後工作相同)。 首先,因為第一個步驟的工作與 update() 相同,因此程式碼不需要做任何 更動。然後,我們需要註冊以及定義使用者點選"刪除"按鈕時的事件處理機制。 在本範例中,該事件會觸發 delete() 方法,所以 我們必須更改刪除的按鈕標籤如下:<button label="刪除" width="46px" height="24px" onClick="delete()"/>
void delete() {
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1/eric", "jlu", "newpasswd");
stmt = conn.createStatement();
String dSQL = "delete from Product where id=" + num.value;
if (stmt.executeUpdate(dSQL) <= 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();
}
}
// 下列兩個敘述的順序不可以對調
// 確保刪除的資料也從 box 和 allItems 內消失
allItems.remove(box.getSelectedIndex());
box.removeItemAt(box.getSelectedIndex());
clearData();
}
void clearData(){
// 清除輸入欄位
num.value = null;
name.value = null;
price.value = null;
qty.value = null;
}
<?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();
}
}
// 資料新增 ==========================================================
void add() {
// 確保新增的資料也在 allItems 內
Product newp = new Product(num.value.intValue(), name.value,
price.value.doubleValue(), qty.value.intValue());
allItems.add(newp);
// 經資料新增至資料庫
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1/eric", "jlu", "newpasswd");
stmt = conn.createStatement();
String iSQL = "insert into Product values(" + num.value + ",'" +
name.value + "'," + price.value + "," + qty.value + ")";
if (stmt.executeUpdate(iSQL) <= 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();
}
}
// 將新增資料形成一個 Listitem 物件(或者節點)
Listitem li = new Listitem();
li.setValue(newp); // 在之後的 update, delete 會用到
li.appendChild(new Listcell(num.value.toString()));
li.appendChild(new Listcell(name.value));
li.appendChild(new Listcell(price.value.toString()));
li.appendChild(new Listcell(qty.value.toString()));
// 將 Listitem 物件變成 box 的子節點
// box 是 listbox 的 id 值
box.appendChild(li);
// 清除輸入欄位
clearData();
}
// 資料修改 ==========================================================
void move() {
// 確保修改的資料也在 allItems 內
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();
}
// 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();
}
// 資料刪除 =========================================================
void delete() {
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1/eric", "jlu", "newpassw");
stmt = conn.createStatement();
String dSQL = "delete from Product where id=" + num.value;
if (stmt.executeUpdate(dSQL) <= 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();
}
}
// 下列兩個敘述的順序不可以對調
// 確保刪除的資料也從 allItems 內消失
allItems.remove(box.getSelectedIndex());
box.removeItemAt(box.getSelectedIndex());
clearData();
}
// ==============================================================
void clearData(){
// 清除輸入欄位
num.value = null;
name.value = null;
price.setText("");
qty.value = null;
}
</zscript>
<listbox id="box" multiple="true" rows="4" onSelect="move()">
<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" onClick="add()"/>
<button label="修改" width="46px" height="24px" onClick="update()"/>
<button label="刪除" width="46px" height="24px" onClick="delete()"/>
</div>
</groupbox>
</window>
Written by: 國立中興大學資管系呂瑞麟 Eric Jui-Lin Lu
沒有留言:
張貼留言