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)

          隨筆分類

          隨筆檔案

          收藏夾

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 西和县| 普安县| 葫芦岛市| 泰和县| 玛曲县| 杭锦后旗| 乳山市| 丰镇市| 岱山县| 新乐市| 房产| 毕节市| 醴陵市| 洮南市| 中西区| 广安市| 东丰县| 克拉玛依市| 濮阳市| 洮南市| 滕州市| 工布江达县| 西林县| 宁陕县| 翼城县| 浮梁县| 京山县| 班玛县| 迭部县| 南涧| 朔州市| 永州市| 齐河县| 金湖县| 南靖县| 山西省| 河曲县| 甘孜县| 灵川县| 金湖县| 房产|