JNDI

JNDI的全称是Java命名与项目接口(Java Naming and Directoy Interface) ,通过指定一个资源名称,将该名称与某一资源或服务相关联。

使用JNDI的步骤:

a) 第一步:写Tomcat/conf/context.xml配置文件

<Resource name="jdbc/news" auth="Container"

type="javax.sql.DataSource" maxActive="100"

maxIdle="30" maxWait="10000" username="sa" password="123456"

driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"

url="jdbc:sqlserver://localhost:1433;DatabaseName=jsp"/>

b) 第二步:写web-info/web.xml

<resource-ref>

   <res-ref-name>jdbc/news</res-ref-name>

   <res-type>javax.sql.DataSource</res-type>

   <res-auth>Container</res-auth>

   </resource-ref>

注意:这个配置的值要与context.xml中的一致

c) 第三步:导入驱动程序包

d) 第四步:写代码获取数据源

Context ctx=new InitialContext();

DataSource ds=(DataSource)ctx.lookup("java:comp/env/jdbc/news");

原文地址:https://www.cnblogs.com/yang82/p/7367524.html