posts - 122,  comments - 25,  trackbacks - 0
          1、編碼方式(注意:jndi命名時,unix\linux系統不允許jdbc/john形式,應改為:jdbc_john):
          {// for DBConnection
              System.setProperty(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.fscontext.RefFSContextFactory");
              
          //
              InitialContext ic = new InitialContext();
              
          {
                  
          // Construct BasicDataSource reference
                  Reference ref = new Reference("javax.sql.DataSource",
                          
          "org.apache.commons.dbcp.BasicDataSourceFactory",
                          
          null);
                  ref.add(
          new StringRefAddr("driverClassName",
                          
          "oracle.jdbc.driver.OracleDriver"));
                  ref.add(
          new StringRefAddr("url", dbURL));
                  ref.add(
          new StringRefAddr("username", username));
                  ref.add(
          new StringRefAddr("password", password));
                  ic.rebind(
          "jdbc/john", ref);
              }

          }

          2、Jndi with tomcat
          a). 配置應用程序WEB-INF目錄下的web.xml:
            <resource-ref>
               
          <description>connection</description>
               
          <res-ref-name>jdbc/john</res-ref-name>
               
          <res-type>javax.sql.DataSource</res-type>
               
          <res-auth>Container</res-auth>
            
          </resource-ref>

          b).tomcat配置文件TOMCAT_HOME/conf/server.xml中context定義如下:
          <Context path="/john" reloadable="false" docBase="/opt/scommnet/john" workDir="/opt/scommnet/john/work" >
              
          <Resource name="jdbc/john"
                  type
          ="javax.sql.DataSource"
                  driverClassName
          ="oracle.jdbc.driver.OracleDriver"
                  password
          ="dbpwd"
                  maxIdle
          ="2"
                  maxWait
          ="5000"
                  username
          ="dbuser"
                  url
          ="jdbc:oracle:thin:@192.168.0.100:1521:cbxx"
                  maxActive
          ="10"/>
          </Context>

          c).調用代碼
          InitialContext ic = new InitialContext();
          DataSource ds 
          = (DataSource)ic.lookup("java:comp/env/jdbc/john");
          try{
              Connection conn 
          = ds.getConnection();
              logger.info(
          "Connection info:"+conn.getMetaData().getDriverName());
              
          //數據查詢操作
          }

          catch(Exception ex){
              ex.printStackTrace();
              logger.error(ex.getMessage(),ex);
          }
          posted on 2007-11-21 16:46 josson 閱讀(416) 評論(0)  編輯  收藏 所屬分類: java 開發
          <2007年11月>
          28293031123
          45678910
          11121314151617
          18192021222324
          2526272829301
          2345678

          常用鏈接

          留言簿(3)

          隨筆分類

          隨筆檔案

          收藏夾

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 广南县| 仪陇县| 曲靖市| 定西市| 碌曲县| 安乡县| 巴中市| 满洲里市| 思茅市| 江北区| 轮台县| 湟中县| 韶关市| 丹阳市| 通河县| 宜黄县| 任丘市| 沙田区| 桦川县| 迁安市| 大庆市| 合阳县| 朝阳区| 义马市| 云龙县| 白沙| 英超| 临安市| 福州市| 神农架林区| 九台市| 青州市| 张掖市| 民勤县| 马龙县| 福建省| 精河县| 内江市| 霍邱县| 依兰县| 泽普县|