[转帖]WebLogic运用DB的Java控件访问数据库_Tomcat, WebLogic及J2EE讨论区_Weblogic技术|Tuxedo技术|中间件技术|Oracle论坛|JAVA论坛|Linux/Unix技术|hadoop论坛_联动北方技术论坛  
网站首页 | 关于我们 | 服务中心 | 经验交流 | 公司荣誉 | 成功案例 | 合作伙伴 | 联系我们 |
联动北方-国内领先的云技术服务提供商
»  游客             当前位置:  论坛首页 »  自由讨论区 »  Tomcat, WebLogic及J2EE讨论区 »
总帖数
1
每页帖数
101/1页1
返回列表
0
发起投票  发起投票 发新帖子
查看: 3291 | 回复: 0   主题: [转帖]WebLogic运用DB的Java控件访问数据库        下一篇 
晶晶
注册用户
等级:少校
经验:1086
发帖:89
精华:0
注册:2012-11-8
状态:离线
发送短消息息给晶晶 加好友    发送短消息息给晶晶 发消息
发表于: IP:您无权察看 2012-11-13 16:02:33 | [全部帖] [楼主帖] 楼主

一、方法

    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.jcxjava控件

    四、数据库

CREATE TABLE TEST(
AVARCHAR2(10),
BVARCHAR2(10),
CVARCHAR2(10),
DVARCHAR2(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;
      }


  }共2页。

    六、逻辑层(页面流)

    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语句。不管是前面提到的树形,还是本文的数据,随意而不零乱却是有机的整体。




赞(0)    操作        顶端 
总帖数
1
每页帖数
101/1页1
返回列表
发新帖子
请输入验证码: 点击刷新验证码
您需要登录后才可以回帖 登录 | 注册
技术讨论