📄 using the j2ee connector architecture common client interface.htm
字号:
<span class="sp20"> </span><br /><!-- END VCD7 CODE SAMPLE COMPONENT --><p>The <code>AcctManageEJB</code> bean also includes a private method, <code>closeCCIConnection</code>, to close a connection. The method invokes the <code>Connection</code> object's <code>close</code> method from within a <code>try/catch</code> block. Like the <code>getCCIConnection</code> method, this is a private method intended to be called from within the session bean.</p><!-- BEGIN VCD7 CODE SAMPLE COMPONENT --><table border="0" cellpadding="10" cellspacing="0" width="100%" class="grey4"><tr><td><pre>private void closeCCIConnection(Connection con) { try { con.close(); } catch (ResourceException ex) { ex.printStackTrace(); } }</pre></td></tr></table><span class="sp20"> </span><br /><!-- END VCD7 CODE SAMPLE COMPONENT --><h3>Reading Database Records</h3><p>The <code>AcctManageEJB's</code> <code>getAcctAmt</code> method illustrates how to use the CCI to read records from a database table. This method does not directly read the database records itself; instead, it invokes a procedure stored in the database called <code>GETAMT</code>. It is the stored procedure that actually reads the records in the database table. </p><p>The CCI provides interfaces for three types of records: <code>IndexedRecord</code>, <code>MappedRecord</code>, and <code>ResultSet</code>. These three record types inherit from the base interface, <code>Record</code>. They differ only in how they map the record elements within the record. The example uses <code>IndexedRecord</code>. <code>IndexedRecord</code> holds its record elements in an ordered, indexed collection based on <code>java.util.List</code>. As a result, a <code>Iterator</code> is used to access the individual elements in the list.</p><p>Begin by looking at how the <code>getAcctAmt</code> method uses the CCI to invoke a database stored procedure. The method first obtains a connection, then creates a new <code>Interaction</code> instance by calling the <code>Connection</code> object's <code>createInteraction</code> method. The <code>Interaction</code> object enables a bean to execute EIS functions such as invoking stored procedures. </p><!-- BEGIN VCD7 CODE SAMPLE COMPONENT --><table border="0" cellpadding="10" cellspacing="0" width="100%" class="grey4"><tr><td><pre>public int getAcctAmt() { int count = -1; try { // Obtain a connection to the database. Connection con = getCCIConnection(); // Create a new Interaction instance // so that the session bean can invoke // the EIS stored procedure. Interaction ix = con.createInteraction();...</pre></td></tr></table><span class="sp20"> </span><br /><!-- END VCD7 CODE SAMPLE COMPONENT --><p>Then, the bean must instantiate a <code>CciInteractionSpec</code> object to pass properties to the <code>Interaction</code> object. The <code>CciInteractionSpec</code> is the implementation class for the <code>InteractionSpec</code> interface, and it holds properties required by the <code>Interaction</code> object to interact with an EIS instance. Such properties include schema name, catalog name, and the stored procedure name. The session bean uses the <code>CciInteractionSpec</code> methods <code>setSchema</code>, <code>setCatalog</code>, and <code>setFunctionName</code> to set the required values into the instance's fields. This example passes <code>GETAMT</code> to <code>setFunctionName</code> because this is the name of the stored procedure it intends to invoke.</p><!-- BEGIN VCD7 CODE SAMPLE COMPONENT --><table border="0" cellpadding="10" cellspacing="0" width="100%" class="grey4"><tr><td><pre> ... // Instantiate a CciInteractionSpec object. CciInteractionSpec iSpec = new CciInteractionSpec(); // Set values for the CciInteractionSpec instance's //fields: user name and stored procedure name. Our //example uses a Cloudscape database, which does not // require a catalog name. iSpec.setSchema(user); iSpec.setCatalog(null); iSpec.setFunctionName("GETAMT"); ...</pre></td></tr></table><span class="sp20"> </span><br /><!-- END VCD7 CODE SAMPLE COMPONENT --><p>Now, the <code>getAcctAmt</code> method uses the <code>ConnectionFactory</code> to obtain a reference to a <code>RecordFactory</code>. With the <code>RecordFactory reference</code>, it invokes the <code>createIndexedRecord</code> method to create an <code>IndexedRecord</code> instance. (The bean could also use the <code>RecordFactory</code> to create <code>MappedRecord</code> or <code>ResultSet</code> instances.) It creates the <code>IndexedRecord</code> using the name <code>InputRecord</code>, which it passes to <code>createIndexedRecord</code> as an argument. This completes the set up work that must be done prior to invoking the stored procedure.</p><!-- BEGIN VCD7 CODE SAMPLE COMPONENT --><table border="0" cellpadding="10" cellspacing="0" width="100%" class="grey4"><tr><td><pre> ... // Obtain a RecordFactory reference. RecordFactory rf = cf.getRecordFactory(); // Invoke the RecordFactory's // createIndexedRecord method. IndexedRecord iRec = rf.createIndexedRecord("InputRecord"); ...</pre></td></tr></table><span class="sp20"> </span><br /><!-- END VCD7 CODE SAMPLE COMPONENT --><p>The <code>getAcctAmt</code> method uses the <code>Interaction</code> instance's <code>execute</code> method to invoke the stored procedure <code>GETAMT</code>. The bean passes two objects to the <code>execute</code> method: the <code>InteractionSpec</code> object, whose properties reference the <code>GETAMT</code> stored procedure, and the <code>IndexedRecord</code> object, which is expected to be an input <code>Record</code>. The <code>execute</code> method returns an <code>output</code> <code>Record</code> object.</p><!-- BEGIN VCD7 CODE SAMPLE COMPONENT --><table border="0" cellpadding="10" cellspacing="0" width="100%" class="grey4"><tr><td><pre> ... // Use the Interaction instance's execute // method to invoke GETAMT. oRec =ix.execute(iSpec,iRec); ...</pre></td></tr></table><span class="sp20"> </span><br /><!-- END VCD7 CODE SAMPLE COMPONENT --><p>Then, the bean uses an <code>Iterator</code> to retrieve the individual elements from the <code>Record</code> returned by the <code>execute</code> method. It first casts the output <code>Record</code> object to an <code>IndexedRecord</code>. Note that <code>IndexedRecord</code> contains an iterator which it inherits from <code>java.util.List.</code> The bean uses the <code>iterator.hasNext</code> method to retrieve each element in the returned record object. Each extracted element is an <code>Object</code>, and the bean evaluates whether it is an integer or decimal value and processes it accordingly. When it has completely processed the record, it closes the connection to the database and returns the results.</p><!-- BEGIN VCD7 CODE SAMPLE COMPONENT --><table border="0" cellpadding="10" cellspacing="0" width="100%" class="grey4"><tr><td><pre> ... // Retrieve the individual elements from // the returned IndexedRecord using the // iterator.hasNext method. Iterator iterator = ((IndexedRecord)oRec).iterator(); while(iterator.hasNext()) { Object obj = iterator.next(); if (obj instanceof Integer) { amt = ((Integer)obj).intValue(); } else if (obj instanceof BigDecimal) { amt = ((BigDecimal)obj).intValue(); } } // Close the connection to the database. closeCCIConnection(con); } catch (ResourceException ex) {...}return amt;}</pre></td></tr></table><span class="sp20"> </span><br /><!-- END VCD7 CODE SAMPLE COMPONENT --><h3>Inserting Database Records</h3><p>The <code>AcctManageEJB</code> session bean implements the <code>insertAcct</code> method to add new records into the <code>Account</code> database table. This method invokes the <code>ADDACCT</code> stored procedure, which inserts an account record and sets values for the two elements in the recordaccount name and amountusing values passed to it as arguments. </p><p>The <code>insertAcct</code> method shown here illustrates how to use the CCI to invoke a stored procedure that expects to be passed argument values. Much of the set up is the same as what was done in the <code>getAcctAmt</code> method. The method first establishes a connection to the database, then creates a new <code>Interaction</code> instance to execute the stored procedure. The bean also instantiates a <code>CciInteractionSpec</code> object (from the <code>CciInteractionSpec</code> class that implements the <code>InteractionSpec</code> interface) to hold the properties that the <code>Interaction</code> object requires to communicate to the database, then it uses the functions <code>setSchema</code>, <code>setCatalog</code>, and <code>setFunctionName</code> to set the necessary property values. Notice that the method sets the function name to <code>ADDACCT</code>.</p><!-- BEGIN VCD7 CODE SAMPLE COMPONENT --><table border="0" cellpadding="10" cellspacing="0" width="100%" class="grey4"><tr><td><pre>public void insertAcct(String acctName, int amt) { try { // Establish a connection to the database. Connection con = getCCIConnection(); // Create a new Interaction instance // to execute the stored procedure. Interaction ix = con.createInteraction(); // Instantiate a CciInteractionSpec object to // pass necessary properties to the // Interaction object. CciInteractionSpec iSpec = new CciInteractionSpec(); // Set schema, catalog and function name // values in the CciInteractionSpec // object fields. iSpec.setFunctionName("ADDACCT"); iSpec.setSchema(user); iSpec.setCatalog(null); ...</pre></td></tr></table><span class="sp20"> </span><br /><!-- END VCD7 CODE SAMPLE COMPONENT --><p>Next, the bean obtains a reference to a <code>RecordFactory</code> using the <code>ConnectionFactory</code>'s <code>getRecordFactory</code> method. It then invokes the RecordFactory's <code>createIndexedRecord</code> method to create a new <code>IndexedRecord</code> called <code>InputRecord</code>. At this point, the bean needs to set values for the two elements in the input record that are to be passed to the <code>ADDACCT</code> stored procedure. The bean uses the <code>IndexedRecord</code> object's <code>add</code> method to set up these values in the new record. It calls the <code>add</code> method once for each element. It sets the first record element to the account name value and the second element to the amount value. (Notice that the <code>int</code> value <code>amt</code> is set to an <code>Integer</code> object when it is passed to the <code>add</code> method. The <code>add</code> method accepts only Java objects.) The <code>AcctManageEJB</code> bean is now ready to pass the new account record to the stored procedure which will add it to the database.</p><p>It calls the <code>Interaction</code> instance's <code>execute</code> method to invoke the stored procedure <code>ADDACCT</code>. Just as when invoking the <code>GETAMT</code> procedure, the bean passes two objects to the <code>execute</code> method: the <code>InteractionSpec</code> object with the correctly set properties for the <code>ADDACCT</code> stored procedure and the <code>IndexedRecord</code> object representing an input <code>Record</code>. The <code>execute</code> method is not expected to return anything in this case.</p><p>The method completes by closing the connection to the database.</p><!-- BEGIN VCD7 CODE SAMPLE COMPONENT --><table border="0" cellpadding="10" cellspacing="0" width="100%" class="grey4"><tr><td><pre> ... // Obtain a reference to a RecordFactory. RecordFactory rf = cf.getRecordFactory(); // Create a new IndexedRecord called // InputRecord. IndexedRecord iRec = rf.createIndexedRecord("InputRecord"); // Use the IndexedRecord add method to // add acctName and amt to // the input record. boolean flag = iRec.add(name); flag = iRec.add(new Integer(qty)); // Invoke the Interaction execute method
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -