Home   Single Page

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インターフェースを実装

ビューと Java コードを混在に使用しないで、init コマンドを使用してデータを読み込むことができます。

<?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>

そして、my.CustomerFindAll クラスを org.zkoss.zk.ui.util.Initiator インターフェースを使用して、実装します。

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    
    }    
}