JBoss4.0.3 + Oracle 9i (JNDI)

 

这个测试的这个例子中不含EJB内容,只是一个非常简单的数据库连接,因为其中一个细节纠缠了一整天所以记下来权做教训:).
试验的最终目的是这样:
  ......
    Context ctx = new InitialContext();   //具体jndi环境设置在jndi.properties中指定
    DataSource ds = (DataSource)ctx.lookup("Zqb Oracle Test"); //其中Zqb Oracle Test是我在oracle-ds.xml中指定的JNDI名
    
    Connection conn = ds.getConnection();
    PreparedStatement ps = conn.prepareStatement("select count(1) from css_emp_info");
    ResultSet rs = ps.executeQuery();
 ......

步骤如下:
   1,jndi.properties(使用jboss缺省的内容,不做修改):
          java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
          java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
   2,将jboss-4.0.1sp1/docs/examples/jca下的oracle-ds.xml拷贝到jboss-4.0.1sp1/server/default/deploy下,并做相应修改,主要内容如下:
<datasources>
  <local-tx-datasource>
    <jndi-name>Zqb Oracle Test</jndi-name>
    <use-java-context>false</use-java-context>
    <connection-url>jdbc:oracle:thin:@10.25.10.175:1521:pacesdev</connection-url>
    <driver-class>oracle.jdbc.driver.OracleDriver</driver-class>
    <user-name>cesbase</user-name>
    <password>voiture6</password>
    <exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.OracleExceptionSorter</exception-sorter-class-name>
      <metadata>
         <type-mapping>Oracle9i</type-mapping>
      </metadata>
  </local-tx-datasource>
</datasources>
   3,OK,部署war,搞定!其中红色标注的是后面加上的,有了这句在jndi前可以不用加前缀;否则,需要加前缀java(即:lookup("Zqb Oracle Test") 和 lookup("java:Zqb Oracle Test")的区别).

补充: 发现在jboss官方网站有关于use-java-context这个属性的说明 http://wiki.jboss.org/wiki/Wiki.jsp?page=ConfigDataSources ,仔细看看!
 

--------------------------------------------------自己的例子--------------------------------------------

oracle-ds.xml

<? xml version="1.0" encoding="UTF-8" ?>

<!--  =====================================================================  -->
<!--                                                                         -->
<!--   JBoss Server Configuration                                            -->
<!--                                                                         -->
<!--  =====================================================================  -->

<!--  $Id: oracle-ds.xml,v 1.6 2004/09/15 14:37:40 loubyansky Exp $  -->
<!--  ====================================================================  -->
<!--   Datasource config for Oracle originally from Steven Coy              -->
<!--  ====================================================================  -->


< datasources >
  
< local-tx-datasource >
 
<!--    <jndi-name>OracleDS</jndi-name>    -->
    
< jndi-name > ming </ jndi-name >
    
< use-java-context > false </ use-java-context >
    
< connection-url > jdbc:oracle:thin:@3.242.165.91:1521:GETEST </ connection-url >
    
<!--

        Here are a couple of the possible OCI configurations.
        For more information, see http://otn.oracle.com/docs/products/oracle9i/doc_library/release2/java.920/a96654/toc.htm

    <connection-url>jdbc:oracle:oci:@youroracle-tns-name</connection-url>
        or
    <connection-url>jdbc:oracle:oci:@(description=(address=(host=youroraclehost)(protocol=tcp)(port=1521))(connect_data=(SERVICE_NAME=yourservicename)))</connection-url>

        Clearly, its better to have TNS set up properly.
     
-->
    
< driver-class > oracle.jdbc.driver.OracleDriver </ driver-class >
    
< user-name > ming </ user-name >
    
< password > tata123 </ password >
    
<!--  Uses the pingDatabase method to check a connection is still valid before handing it out from the pool  -->
    
<!-- valid-connection-checker-class-name>org.jboss.resource.adapter.jdbc.vendor.OracleValidConnectionChecker</valid-connection-checker-class-name -->
    
<!--  Checks the Oracle error codes and messages for fatal errors  -->
    
< exception-sorter-class-name > org.jboss.resource.adapter.jdbc.vendor.OracleExceptionSorter </ exception-sorter-class-name >
        
<!--  sql to call when connection is created
        <new-connection-sql>some arbitrary sql</new-connection-sql>
        
-->

        
<!--  sql to call on an existing pooled connection when it is obtained from pool - the OracleValidConnectionChecker is prefered
        <check-valid-connection-sql>some arbitrary sql</check-valid-connection-sql>
        
-->

      
<!--  corresponding type-mapping in the standardjbosscmp-jdbc.xml (optional)  -->
      
< metadata >
         
< type-mapping > Oracle9i </ type-mapping >
      
</ metadata >
  
</ local-tx-datasource >
  
< local-tx-datasource >
 
<!--    <jndi-name>OracleDS</jndi-name>    -->
    
< jndi-name > mingtest </ jndi-name >
    
< use-java-context > false </ use-java-context >
    
< connection-url > jdbc:oracle:thin:@3.242.164.53:1521:ora </ connection-url >
    
<!--

        Here are a couple of the possible OCI configurations.
        For more information, see http://otn.oracle.com/docs/products/oracle9i/doc_library/release2/java.920/a96654/toc.htm

    <connection-url>jdbc:oracle:oci:@youroracle-tns-name</connection-url>
        or
    <connection-url>jdbc:oracle:oci:@(description=(address=(host=youroraclehost)(protocol=tcp)(port=1521))(connect_data=(SERVICE_NAME=yourservicename)))</connection-url>

        Clearly, its better to have TNS set up properly.
     
-->
    
< driver-class > oracle.jdbc.driver.OracleDriver </ driver-class >
    
< user-name > ming </ user-name >
    
< password > tata123 </ password >
    
<!--  Uses the pingDatabase method to check a connection is still valid before handing it out from the pool  -->
    
<!-- valid-connection-checker-class-name>org.jboss.resource.adapter.jdbc.vendor.OracleValidConnectionChecker</valid-connection-checker-class-name -->
    
<!--  Checks the Oracle error codes and messages for fatal errors  -->
    
< exception-sorter-class-name > org.jboss.resource.adapter.jdbc.vendor.OracleExceptionSorter </ exception-sorter-class-name >
        
<!--  sql to call when connection is created
        <new-connection-sql>some arbitrary sql</new-connection-sql>
        
-->

        
<!--  sql to call on an existing pooled connection when it is obtained from pool - the OracleValidConnectionChecker is prefered
        <check-valid-connection-sql>some arbitrary sql</check-valid-connection-sql>
        
-->

      
<!--  corresponding type-mapping in the standardjbosscmp-jdbc.xml (optional)  -->
      
< metadata >
         
< type-mapping > Oracle9i </ type-mapping >
      
</ metadata >
  
</ local-tx-datasource >

</ datasources >

jsp page ------------welcome.jsp

<% @ page contentType = " text/html; charset=gbk "  language = " java "   %>
<% @ page  import = " javax.naming.Context " %>
<% @ page  import = " javax.sql.DataSource " %>
<% @ page  import = " javax.naming.InitialContext " %>
<% @ page  import = " java.sql.* " %>


<%
    DataSource ds 
=   null ;
      Context initCtx 
=   new  InitialContext();
//       Context envCtx = (Context) initCtx.lookup("java:comp/env");
//       ds = (DataSource)envCtx.lookup("java:jdbc/BSTxDataSource");
    ds  =  (DataSource)initCtx.lookup( " ming " );
      
if (ds != null ) {
        out.println(
"Connection is OK!");
        Connection cn
=ds.getConnection();
            
if(cn!=null){
                       out.println(
"cn is Ok!");
                       Statement stmt 
= cn.createStatement();
                       ResultSet rst 
= stmt.executeQuery("select * from log_users");
                       out.println(
"<p>rst is Ok!" + rst.next());
                       
while(rst.next()){
                                         out.println(
"<P>username:" + rst.getString(2));
                                       }

                                       cn.close();
                 }
else{
                       out.println(
"rst Fail!");
                     }

               }

               
else  
                 out.println(
" Fail! " );
%>
< hr color = " red " >
-----------------------------------------------
<%
    DataSource ds2 
=   null  ;
    Context initCox 
=   new  InitialContext();
    ds2 
=  (DataSource)initCox.lookup( " mingtest " );
    String sql 
=   " select U_NAME from log_users " ;
    
if (ds2 !=   null )
    
{    
        out.print(
"ds2 isn't null!<br>");
        Connection conn2 
= ds2.getConnection();
        Statement st 
= conn2.createStatement();
        ResultSet rs2 
= null ;
        rs2 
= st.executeQuery(sql);
        
while(rs2.next())
        
{
            String name 
= rs2.getString("U_name");
            out.print(
"UserName:"+name+"<br>");
        }

        conn2.close();
    }

    
else
    out.println(
" ds2 is null!  " );
%>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值