JNDI:java命名与目录接口
将某一个对象(资源),以配置文件(Tomcat/conf/context.xml)的形式注入,
<Environment name="jndiName" value="23" type="java.lang.String"/>
连接池:
常见的连接池:Tomcat-dbcp、dbcp、c3p0、druid
可以用(java.sql.DataSource)管理连接池
配置Tomcat-dbcp:的过程
1、类似JNDI,在Tomcat的安装目录下的Tomcat/conf/context.xml配置数据源
<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<!-- The contents of this file will be loaded for each web application -->
<Context>
<!-- Default set of monitored resources. If one of these changes, the -->
<!-- web application will be reloaded. -->
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<WatchedResource>WEB-INF/tomcat-web.xml</WatchedResource>
<WatchedResource>${catalina.base}/conf/web.xml</WatchedResource>
<!-- Uncomment this to disable session persistence across Tomcat restarts -->
<!--
<Manager pathname="" />
-->
<Environment name="jndiName" value="23" type="java.lang.String"/>
<Resource name="student"
auth="Container"
type="javax.sql.DataSource"
username="root"
password="123456"
maxIdle="30"
maxWait="10000"
maxActive="100"
driverClassName="com.mysql.cj.jdbc.Driver"
url="jdbc:mysql://localhost:3306/demodatabase?serverTimezone=UTC" />
</Context>
2、在项目的工程目录的web.xml引用数据源
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!--指定数据源-->
<resource-ref>
<!-- 用这个数据源-->
<res-ref-name>student</res-ref-name>
<!-- 数据源类型-->
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>
3、在项目中使用