WebLogic運用DB的Java控制項訪問資料庫
草木瓜 2006-6-8
一、方法
WebLogic頁面與資料通訊時,一般採用Java控制項直接存取資料連線池,資料的直接操作都定義在
Java控制項中,頁面流做為資料的邏輯處理單元,普通頁面做為顯示層。可以看出WebLogic這個方法是
典型的三層結構,資料層(Java控制項),商務邏輯層(頁面流),顯示層(頁面)
二、建立串連池,資料來源
配置config.xml檔案,這裡用的是WebLogic內建的E:/bea/weblogic81/samples/domains/workshop
的cgServer。
<JDBCConnectionPool DriverName="oracle.jdbc.driver.OracleDriver"
LoginDelaySeconds="1" MaxCapacity="20" Name="liwei"
PasswordEncrypted="{3DES}WBNJPYUOAvE=" Properties="user=liwei"
Targets="cgServer" URL="jdbc:oracle:thin:@localhost:1521:wincn"/>
<JDBCTxDataSource JNDIName="liwei" Name="liwei" PoolName="liwei" Targets="cgServer"/>
或者 工具->WebLogic Server->資料來源查看器->建立資料來源 步驟比較簡單,主要輸入對應參數:
DriverName="oracle.jdbc.driver.OracleDriver"
URL="jdbc:oracle:thin:@localhost:1521:wincn"
然後使用者名稱密碼即可。
以上內容可參看《Weblogic中JSP串連資料庫》一文
三、相關頁面
Test/TestWeb/recordset/RecordsetController.jpf
Test/TestWeb/recordset/index.jsp
Test/TestWeb/recordset/test.jcx java控制項
四、資料庫
CREATE TABLE TEST(
A VARCHAR2(10),
B VARCHAR2(10),
C VARCHAR2(10),
D VARCHAR2(10)
)
五、資料層(JAVA控制項)
本次樣本使用tblTest自訂靜態類實現返回資料集。(還可以使用netui:gird+RecordSet實現,參見內建樣本)
其中update方法與insert方法十分類似,故未提供具體的實現代碼。
資料層並沒有什麼複雜之處,只是對邏輯層(頁面流)提供足夠的資料操作介面。tblTest自訂的靜態類
是完成資料傳遞必不可少的環節。
Test/TestWeb/recordset/test.jcx 全代碼
package recordset;
import com.bea.control.*;
import java.sql.SQLException;
/*
* @jc:connection data-source-jndi-name="liwei"
*/
public interface test extends DatabaseControl, com.bea.control.ControlExtension
{
/**
* @jc:sql statement::
* INSERT INTO TEST (A,B,C,D)
* VALUES ({_A},{_B},{_C},{_D})
* ::
*/
public int insert( String _A, String _B,String _C,String _D );
/**
* @jc:sql statement::
* UPDATE TEST SET B = {_B} ,C = {_C} ,D = {_D} WHERE A = {_A}
* ::
*/
public int update( String _A, String _B,String _C,String _D );
/**
* @jc:sql statement::
* DELETE TEST WHERE A = {_A}
* ::
*/
public int delete( String _A );
/**
* @jc:sql statement::
* SELECT * FROM TEST WHERE A = {_A}
* ::
*/
public tblTest select( String _A );
/**
* @jc:sql statement::
* SELECT * FROM TEST
* ::
*/
public tblTest[] selectAll();
public static class tblTest implements java.io.Serializable
{
public String A;
public String B;
public String C;
public String D;
}
}
六、邏輯層(頁面流)
Test/TestWeb/recordset/RecordsetController.jpf 主要代碼,省略了自動產生部分
public class RecordsetController extends PageFlowController
{
/*
*
* @common:control
*/
private test recTest; //定義資料介面
private test.tblTest[] recNew; //定義資料集
//因為樣本串連的是英文資料庫,會存在亂碼問題,下面是轉碼的函數,這也充分
//說明了,邏輯層在處理資料的關鍵所在。
private String getGBString(String strIn)
{
try
{
byte[] tmpByte=strIn.getBytes("ISO8859-1");
return new String(tmpByte,"gb2312");
}
catch(Exception e)
{
return "";
}
}
//返回全記錄,調用recTest的selectAll,介面函數
public test.tblTest[] getAll()
{
recNew=recTest.selectAll();
int i;
for(i=0;i<recNew.length;i++)
{
recNew[i].A=getGBString(recNew[i].A);
recNew[i].B=getGBString(recNew[i].B);
recNew[i].C=getGBString(recNew[i].C);
recNew[i].D=getGBString(recNew[i].D);
}
return recNew;
}
//添加資料,這時通過頁面傳遞的參數值,調用介面Add資料
/**
* @jpf:action
* @jpf:forward name="success" path="index.jsp"
*/
public Forward add()
{
recTest.insert(this.getRequest().getParameter("a"), this.getRequest().getParameter("b"),this.getRequest().getParameter("c"),this.getRequest().getParameter("d"));
return new Forward( "success" );
}
//刪除資料
/**
* @jpf:action
* @jpf:forward name="success" path="index.jsp"
*/
public Forward delete()
{
recTest.delete(this.getRequest().getParameter("ToDelete"));
return new Forward( "success");
}
/**
* 此方法代表進入頁面流的入口
* @jpf:action
* @jpf:forward name="success" path="index.jsp"
*/
protected Forward begin()
{
return new Forward("success");
}
}
七、顯示層(頁面)
Test/TestWeb/recordset/index.jsp 最外層顯示,查看下面完全代碼時,可以看到netui控制項的極大
靈活性。
技術痛點並不多,這裡使用的是netui-data:repeater,重複擷取記錄集資料。
<body>
<table border=1>
<tr>
<td width="100" class="header-text">A</td>
<td width="100" class="header-text">B</td>
<td width="100" class="header-text">C</td>
<td width="100" class="header-text">D</td>
</tr>
<netui-data:repeater dataSource="{pageFlow.all}">
<netui-data:repeaterHeader> </netui-data:repeaterHeader>
<netui-data:repeaterItem>
<tr>
<td width="100" class="row-text"><a href="#" onclick="window.alert('<netui:content value='{container.item.A}-{container.item.B}-{container.item.C}-{container.item.D}'/>')"><netui:label value="{container.item.A}"/></a></td>
<td width="100" class="row-text"><netui:label value="{container.item.B}"/></td>
<td width="100" class="row-text"><netui:label value="{container.item.C}"/></td>
<td width="100" class="row-text"><netui:label value="{container.item.D}"/></td>
<td>
<netui:anchor action="delete" onClick="return(window.confirm('Del?'))">
<netui:parameter name="ToDelete" value="{container.item.A}"/>
Delete
</netui:anchor>
</td>
</tr>
</netui-data:repeaterItem>
<netui-data:repeaterFooter> </netui-data:repeaterFooter>
</netui-data:repeater>
</table>
<hr>
<netui:form action="add" >
A:<input type="text" name="a"/><br>
B:<input type="text" name="b"/><br>
C:<input type="text" name="c"/><br>
D:<input type="text" name="d"/><br>
<input type="submit" value="add">
</netui:form>
</body>
八、小結
以前對java的瞭解為0,因項目迫切需要適當的研究下WebLogic,作為入門級的選手就能感受到WebLogic魅力
的一二。清晰的層次非常便於組織項目的架構。頁面流在Web開發過程可為核心,結合展示層的netui控制項,將
大量指令碼可以化為簡單輕鬆的物件導向的java語句。不管是前面提到的樹形,還是本文的資料,隨意而不零亂
卻是有機的整體。強!
感覺需要選本書系統的學習一下WebLogic的思想。