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

五、数据层(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="#"

οnclick="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的思想。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值