jboss-4.2.2.GA中配置数据源详细步骤

最近使用jboss-4.2.2.GA版本配置数据源,略有心得,特发此篇,希望对还在迷茫的同学有帮助!

         各种类型的数据库,都可以根据%JBOSS_HOME%/docs/examples/jca/下的示例文档配置,
 下面以mssql为例,提供我们在生产机环境的配置。
 该xml文件存在于%JBOSS_HOME%/docs/examples/jca/下的mssql-ds.xml,
 修改文件内容形如

  < datasources >
  
< local-tx-datasource >
    
< jndi-name > dataSource </ jndi-name >
    
< connection-url > jdbc:microsoft:sqlserver://localost:1433;DatabaseName=jcwork </ connection-url >
    
< driver-class > com.microsoft.jdbc.sqlserver.SQLServerDriver </ driver-class >
    
< user-name > sa </ user-name >
    
< password ></ password >
        
<!--  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
        <check-valid-connection-sql>some arbitrary sql</check-valid-connection-sql>
        
-->
      
<!--  corresponding type-mapping in the standardjbosscmp-jdbc.xml (optional)  -->
      
< metadata >
         
< type-mapping > MS SQLSERVER2000 </ type-mapping >
      
</ metadata >
  
</ local-tx-datasource >
</ datasources >

然后将其拷贝至%JBOSS_HOME%/server/all(or default)/deploy/下,
对应的数据库驱动程序jar需copy至部署的配置的lib下,本例中就要拷贝mssqlserver.jar,msbase.jar,msutil.jar到该目录下
以下是在jsp页面中测试该数据源的代码

Context ctx = null ;
   Connection cnn
= null ;
   java.sql.Statement stmt
= null ;
   ResultSet rs
= null ;
   
try
   {
    ctx
= new  InitialContext();
    
if (ctx == null )
     
throw   new  Exception( " initialize  the Context  failed " );
    DataSource ds
= (DataSource)ctx.lookup( " java:dataSource " );
    out.println(ds);
    
if (ds == null )
     
throw   new  Exception( " datasource  is  null " );
     
try {
        cnn
= ds.getConnection();   
        out.println(
" <br> connection: " + cnn + " 你已经成功得到了数据源! " );
    }
catch (Exception e){
        e.printStackTrace();
    }

   }
   
finally
   {
    
if (rs != null )
     rs.close();
    
if (stmt != null )
     stmt.close();
    
if (cnn != null )
     cnn.close();
    
if (ctx != null )
     ctx.close();
   }  

之后启动jboss服务,如果一切顺利可以看到如下输出:
org.jboss.resource.adapter.jdbc.WrapperDataSource@10d78ec
connection:org.jboss.resource.adapter.jdbc.WrappedConnection@1ccf342你已经成功得到了数据源!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值