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)

          隨筆分類

          隨筆檔案

          收藏夾

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 苗栗县| 石泉县| 安化县| 会同县| 肥乡县| 江永县| 唐河县| 共和县| 德清县| 穆棱市| 元江| 巴楚县| 嘉义县| 镇赉县| 民乐县| 济宁市| 锡林浩特市| 张家界市| 黄梅县| 衡水市| 遵义市| 普兰店市| 桃园市| 乌拉特后旗| 汉源县| 如东县| 九龙县| 大厂| 青河县| 屯留县| 双鸭山市| 汝州市| 天全县| 古丈县| 县级市| 东安县| 深州市| 曲水县| 白朗县| 齐齐哈尔市| 丽水市|