JNDI配置

本文介绍了两种在Tomcat环境中配置数据库连接的方法:一种是通过在web应用内部配置context.xml文件来实现,另一种是在Tomcat的conf/context.xml中进行全局配置。两种方法都需要将数据库驱动包放置到Tomcat的lib目录下。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


介绍两种方式,第一种是不依赖于tomcat的配置;第二中是依赖于tomcat的配置;但是这两种都需要在tomcat的lib包下添加所需的数据库驱动包

第一种,不依赖于tomcat的配置:

1、把所需驱动包放到tomcat的lib包下

2、配置web工程的web/META-INF/添加“context.xml”文件

  1. <Context>
  2. <!--Defaultsetofmonitoredresources-->
  3. <WatchedResource>WEB-INF/web.xml</WatchedResource>
  4. <Resourcename="jdbc/pmlf"
  5. type="javax.sql.DataSource"
  6. driverClassName="oracle.jdbc.driver.OracleDriver"
  7. url="jdbc:oracle:thin:@localhost:1521:orcl"
  8. username="GH_HN"
  9. password="GH_HN"
  10. maxIdle="10"
  11. maxWait="5000"
  12. validationQuery=""
  13. DatabaseName="PMLF"
  14. maxActive="20"/>
  15. </Context>


3、 代码调用

  1. publicConnectiongetConnection(){
  2. Connectionconn=null;
  3. try{
  4. ContextinitCtx=newInitialContext();
  5. Contextctx=(Context)initCtx.lookup("java:comp/env");//固定字符串<spanstyle="font-family:Arial,Helvetica,sans-serif;">java:comp/env</span>
  6. Objectobj=(Object)ctx.lookup("jdbc/pmlf");
  7. javax.sql.DataSourceds=(javax.sql.DataSource)obj;
  8. conn=ds.getConnection();
  9. if(conn==null){
  10. System.out.printly("获取连接失败");
  11. }
  12. returnconn;
  13. }
  14. catch(Exceptione){
  1. <spanstyle="white-space:pre"></span>e.printStackTrace();
  2. }
  3. }

第二中,依赖于tomcat的配置,从网上摘录,理论可以,但是没有亲自确认。

1、修改tomcat中配置文件-conf/context.xml,配置如下

  1. <Context>
  2. <!--Defaultsetofmonitoredresources-->
  3. <WatchedResource>WEB-INF/web.xml</WatchedResource>
  4. <!--UncommentthistodisablesessionpersistenceacrossTomcatrestarts-->
  5. <!--
  6. <Managerpathname=""/>
  7. -->
  8. <Resourcename="jcptDataSourceJNDI"auth="Container"type="javax.sql.DataSource"
  9. username="tysp"password="12345678"driverClassName="oracle.jdbc.driver.OracleDriver"
  10. url="jdbc:oracle:thin:@192.168.1.52:1521:fuxing"/>
  11. <!--UncommentthistoenableCometconnectiontacking(providesevents
  12. onsessionexpirationaswellaswebapplifecycle)-->
  13. <!--
  14. <ValveclassName="org.apache.catalina.valves.CometConnectionManagerValve"/>
  15. -->
  16. </Context>

2、在项目下的WEB-INF/web.xml添加配置代码

  1. <welcome-file-list>
  2. <welcome-file>index.jsp</welcome-file>
  3. </welcome-file-list>
  4. <resource-ref>
  5. <description>DatabaseSource</description>
  6. <res-ref-name>jcptDataSourceJNDI</res-ref-name>
  7. <res-type>javax.sql.DataSource</res-type>
  8. <res-auth>Container</res-auth>
  9. </resource-ref>

3、将数据库驱动jar包放入tomcat的lib中

4、测试,地址引用

  1. <body>
  2. <%
  3. ContextinitContext=newInitialContext();
  4. ContextenvContext=(Context)initContext.lookup("java:/comp/env");
  5. DataSourceds=(DataSource)envContext.lookup("jcptDataSourceJNDI");
  6. Connectionconn=ds.getConnection();
  7. if(conn==null){
  8. out.println("连接失败!");
  9. }else{
  10. out.println("连接成功!");
  11. }
  12. Statementstmt=conn.createStatement();
  13. ResultSetrs=stmt.executeQuery("select*fromt_sys_dept");
  14. while(rs.next()){
  15. out.println("第一列:"+rs.getString(1));
  16. }
  17. %>
  18. </body>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值