WEB开发网      濠电娀娼ч崐濠氬疾椤愶附鍋熸い鏍ㄧ〒闂勫嫰鏌﹀Ο渚Ц闁诲氦顕ч湁婵犲﹤楠告禍鍓х磼鏉堛劌绗氶柟宄版嚇閹晠宕归銈嗘濠电偞鍨堕幐鎾磻閹捐秮褰掓偐閻戞﹩妫勯梺鎼炲妼鐎涒晝绮嬪澶樻晝闁挎繂鏌婇敃鍌涚厵閻庢稒锚閻忥絾绻濇繝鍐ㄧ伌闁诡垰鍟村畷鐔碱敂閸♀晙绱樺┑鐐差嚟婵儳螞閸曨剚鍙忛柍鍝勬噹缁€澶嬬箾閹存繄锛嶆鐐灲閹綊宕惰濡插鏌涢妸銉ヮ劉缂佸倸绉归弫鎾绘晸閿燂拷 ---闂備焦瀵уú鈺呭箯閿燂拷
开发学院WEB开发Jsp Tomcat中通过JNDI配置访问数据库 阅读

Tomcat中通过JNDI配置访问数据库

 2008-01-05 09:42:52 来源:WEB开发网 闂備線娼уΛ鎾箯閿燂拷闂備礁鎲¢崹鐢垫崲閹扮増鍎嶆い鎺戝€甸崑鎾斥槈濞嗗秳娌紓鍌氱▌閹凤拷濠电姭鎷冮崨顓濈捕闂侀潧娲ゅú銊╁焵椤掍胶鈯曢柕鍥╁仧缁辩偤鏁撻敓锟�闂備線娼уΛ鎾箯閿燂拷  闂備胶枪缁绘鈻嶉弴銏犳瀬闁绘劕鎼痪褔鏌曟繝蹇曠窗闁煎壊浜滈—鍐偓锝庡墮娴犙勭箾閸喎鐏ユい鏇樺劦椤㈡瑩鎮℃惔銇帮拷
核心提示:<b>1,所需要的jar文件:commons-pool.jar, commons-dbcp.jar</b><b>2,Tomcat中通过JNDI配置访问数据库,server.xml的配置:</b><Resource name="jdbc/hellohiber

  <b>1,所需要的jar文件:commons-pool.jar, commons-dbcp.jar</b>
  
  <b>2,server.xml的配置:</b>
  <Resource name="jdbc/hellohibernate" scope="Shareable" auth="Container"
   type="javax.sql.DataSource"/>
   <ResourceParams name="jdbc/hellohibernate">
    <parameter>
    <name>factory</name>
    <value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
    </parameter>
    <parameter>
         <name>removeAbandoned</name>
         <value>true</value>
        </parameter>
    <parameter>
         <name>logAbandoned</name>
         <value>true</value>
        </parameter>
    <!-- DBCP database connection settings -->
    <parameter>
    <name>url</name>
    <value>jdbc:MySQL://localhost:3306/test</value>
    </parameter>
    <parameter>
    <name>driverClassName</name><!--<value>com.mysql.jdbc.Driver</value>-->
    <value>org.gjt.mm.mysql.Driver</value>
    </parameter>
    <parameter>
    <name>username</name>
    <value>root</value>
    </parameter>
    <parameter>
    <name>passWord</name>
    <value></value>
    </parameter>
  
    <!-- DBCP connection pooling options -->
    <parameter>
    <name>maxWait</name>
    <value>3000</value>
    </parameter>
    <parameter>
    <name>maxIdle</name>
    <value>100</value>
    </parameter>
    <parameter>
    <name>maxActive</name>
    <value>10</value>
    </parameter>
   </ResourceParams>
  
  <b>3,程序中的调用:</b>
  
  Context ctx = new InitialContext();
        if (ctx == null)
          throw new Exception("Boom - No Context");
  
        DataSource ds =
          (DataSource) ctx.lookup(
          "java:comp/env/jdbc/hellohibernate");
  
        if (ds != null) {
          Connection conn = ds.getConnection();
  
          if (conn != null) {
            foo = "Got Connection " + conn.toString();
            Statement stmt = conn.createStatement();
            ResultSet rst =
              stmt.executeQuery(
              "select username,id from user");
            if (rst.next()) {
              foo = rst.getString(1);
              bar = rst.getInt(2);
            }
            conn.close();
          }
        }

Tags:Tomcat 通过 JNDI

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接