硬件学院 | 网络学院 | 游戏秘籍 | 求职技巧 | 企业管理 | 软件资讯 | IT导购 | 软件下载 | 源码下载
软件学院 | 安全资讯 | 图形图象 | 网络营销 | 电子商务 | 硬件资讯 | IT生活 | 教程下载 | 电影娱乐
网站首页    个人求职    单位招聘    高校联盟    猎头服务    培训服务    资讯中心    IT论坛
让每一个热爱IT的人都找到一份满意的工作!
文章搜索:
 您的位置首页->-> 软件学院-> JSP技术-> 利用JDBC RowSet讓EJB返回數据集(手記)
利用JDBC RowSet讓EJB返回數据集(手記)
作者:中国资讯网 来源:zixuen.com 加入时间:2005-5-12 www.cnitrc.com
利用JDBC RowSet讓EJB返回數据集(手記)

**************************************************************************
前言:

   前几日在Weblogic6.1上試驗使用數据集OK(EJB和客戶端都在Weblogic上),后來將
客戶端部署到另外一臺服務器上(運行Resin),想由Resin上的JSP客戶端調用來獲得
Weblogic上的EJB返回的數据集,發現行不通.
   經過日夜搜索,終于發現一偏關于JDBC RowSet的文章(JDBC 2.0 Optional Package API),
正是我想要的.
   馬上行動!

**************************************************************************

注意:安以下步驟部署你必須熟悉Weblogic6.1的EJB部署,數据源部署.

1.到http://developer.java.sun.com/developer/earlyAccess/下載rowset.jar

//JDBC RowSet The three JDBC RowSet implementations in this release demonstrate some of the many possibilities for implementing the javax.sql.RowSet interface, which is part of the JDBC 2.0 Optional Package API. (June 30, 2000)


2.將rowset.jar放到你的CLASSPATH中.(我使用的是Weblogic6.1)

3.寫你的EJB,如下列:
************************************************************************
CoffeesEJB.java
************************************************************************
import java.sql.*;
import javax.sql.*;
import sun.jdbc.rowset.*;  //倒入rowset.jar類
import javax.naming.*;
import javax.ejb.*;

public class CoffeesEJB implements SessionBean {

    private SessionContext sc = null;
    private Context ctx = null;
    private DataSource ds = null;
    
    public CoffeesEJB () {}
    
    public void ejbCreate() throws CreateException {

        try {
            ctx = new InitialContext();
            ds = (DataSource)ctx.lookup("bbb.OracleThintxds"); //數据源
        }
        catch (Exception e) {
            System.out.println(e.getMessage());
            throw new CreateException();
        }
    }

    public RowSet getCoffees() throws SQLException {
        
        Connection con = null;
    ResultSet rs;
        CachedRowSet crs;

        try {
            con = ds.getConnection("system", "command1");   //EJB Server的用戶和密碼,我用的是Weblogic6.1
            Statement stmt = con.createStatement();
            rs =  stmt.executeQuery("select * from coffees");
            
            crs = new CachedRowSet();
            crs.populate(rs);
            // the writer needs this because JDBC drivers
            // don't provide this meta-data.
            crs.setTableName("coffees");
            
            rs.close();
            stmt.close();
        } finally {
            if (con != null)
                con.close();
        }
        return crs;
    }


       
    //
    // Methods inherited from SessionBean
    //
    
    public void setSessionContext(SessionContext sc) {
        this.sc = sc;
    }
    
    public void ejbRemove() {}
    public void ejbPassivate() {}
    public void ejbActivate() {}

    
}
************************************************************************

4.發布CoffeesEJB到你的EJB Server上.
  ok!EJB在EJB Server上的部署完成!下面是客戶端的部署.

5.同樣將rowset.jar放到你的客戶端服務器的CLASSPATH中.(我使用的是Resin2.0)

6.寫你的客戶端jsp代碼,如下列:
************************************************************************
CoffeesClient.jsp
************************************************************************
<%@ page import="java.sql.*"%>
<%@ page import="java.util.*"%>
<%@ page import="javax.naming.*"%>
<%@ page import="sun.jdbc.rowset.*"%>
<%@ page import="javax.servlet.*"%>
<%@ page import="javax.servlet.http.*"%>
<%@ page import="weblogic.jndi.T3InitialContextFactory" %>
<%       // 來自weblogic.jar  %>
<%@ page import="java.io.*"%>
<%@ page import="javax.rmi.*"%>

<%@ page import="Coffees"%>
<%@ page import="CoffeesHome"%>
<%@ page contentType="text/html;charset=big5" %>


<%
Properties p = new Properties() ;
p.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.T3InitialContextFactory");
p.put(Context.PROVIDER_URL,"t3://192.1.1.23:7001");    //Weblogic服務器的地址和端口

String info="";
try{
Context initial = new InitialContext(p);

Object objref = initial.lookup("statelessSession.Coffees");

CoffeesHome home =(CoffeesHome)PortableRemoteObject.narrow(objref,CoffeesHome.class);
Coffees currencymyString = home.create();

CachedRowSet rset = (CachedRowSet)currencymyString.getCoffees();


String coffeeName="";
while (rset.next()) {

       coffeeName = rset.getString("COF_NAME");
       out.println(coffeeName + "<BR><HR size=1>" );
}

out.println("<br>********************<br>");

currencymyString.remove();

}catch(javax.naming.NamingException ne){
       info = "錯誤:EJB服務器找不到!無法使用遠程方法.<br>" + ne.toString();
}catch(java.rmi.RemoteException re){
       info = re.toString();
}catch(javax.ejb.CreateException ce){
       info = ce.toString();
}catch(javax.ejb.RemoveException re){
       info = re.toString();
}
     

out.println("<hr>"+info+"<hr>");
out.println("<hr>Test By Jun_bai@sohu.com<hr>");
%>

************************************************************************

7.運行客戶CoffeesClient.jsp

  如果報錯肯定是你的CLASSPATH問題,仔細看看一定成功!
  
  
  
全文完

=======================================================
火火火 于2002/3/8虎門 jun_bai@sohu.com
=======================================================  
  

  相关文章:
JSP技术
ASP技术
PHP技术
JSP技术
.NET技术
服务器技术
数据库技术
其它类
工具软件
办公软件
本类阅读TOP10
 
关于我们   |   服务声明   |   使用帮助   |   广告合作   |   网站地图   |   友情链接   |   加盟合作   |   联系我们
Copyright © 2006 cnitrc.com Inc. All Rights Reserved. 浙ICP备05074295号
中国IT人才网 版权所有 网络实名:中国IT人才
未经书面授权严禁转载和复制本站的任何招聘信息和文章