您的位置:首页 > 编程语言 > Java开发

WebLogic运用DB的Java控件访问数据库

2007-03-12 11:45 405 查看
阅前声明: http://blog.csdn.net/heimaoxiaozi/archive/2007/01/19/1487884.aspx

WebLogic运用DB的Java控件访问数据库



一、方法

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

五、数据层(J***A控件)

本次示例使用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的思想。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: