这是我在项目当中积累的资料关于配置tomcat和 weblogic的数据源,贴出来和大家分享,有什么问题还请多指教,共同学习,共同进步
一、tomcat数据源的配制
★★注意将数据库驱动程序最好放在common里,tomcat最先加载这里的驱动,放在server里有时候会出现找不到驱动程序的错误

 在tomcat5.5中需要下载apache-tomcat-5.5admin.tar.rar 这个包才可以进行配制
   1.首先进入tomcat的配制数据源的页面Data Sources中配制jndi等连接信息如(jndi为hdb)
   2.在自己应用中的WEB-INF中的web.xml中添加如下:
 <resource-ref>
   <description>
     Resource reference to a factory for java.sql.Connection
     instances that may be used for talking to a particular
     database that is configured in the server.xml file.
   </description>
   <res-ref-name>
     hdb
   </res-ref-name>
   <res-type>
     javax.sql.DataSource
   </res-type>
   <res-auth>
     Container
   </res-auth>
 </resource-ref>
    3.在META-INF中添加context.xml文件内容如下:
    <?xml version="1.0" encoding="UTF-8"?>
 <Context>
   <WatchedResource>WEB-INF/web.xml</WatchedResource>
   <Resource name="hdb" auth="Container"
      type="javax.sql.DataSource" username="wangwin" password="wangwin"
      driverClassName="oracle.jdbc.driver.OracleDriver"
      url="jdbc:oracle:thin:@127.0.0.1:1521:ora9"
      maxActive="10" maxIdle="2"/>
 </Context>
   4.在程序中通过JNDI得到连接对象内容如下:
   try {
 Context initCtx = new InitialContext();
 Context envCtx = (Context) initCtx.lookup("java:comp/env");//这里这是必须的。。。
 DataSource ds = (DataSource)(envCtx.lookup( "hdb" ));
 if (ds != null){
     Connection conn = ds.getConnection();//得到连接对象测试内容
     conn.setAutoCommit(false);
     Statement stm = conn.createStatement();
     ResultSet rs = stm.executeQuery("select * from tshp_flow");
     while(rs.next()){
  System.out.println(rs.getString(1));
  System.out.println(rs.getString(2));
  System.out.println(rs.getString(3));
     }
   }else{
     }
 } catch (NamingException ex) {
  ex.printStackTrace();
 }
二、weblogic数据源的配制
 1.打开weblogic的控制台-服务-JDBC-连接缓冲池 配制数据库的连接信息
 2.服务-JDBC-数据源创建JNDI-继续-选择刚刚配制好的数据库JDBC
 3.得到连接对象

posted on 2007-12-24 12:02 LifeNote 阅读(3128) 评论(0)  编辑  收藏 所属分类: Javatomcat

只有注册用户登录后才能发表评论。


网站导航: