mysql数据库连接池JNDI
一、在META-INF下面创建context.xml 文件
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Resource
auth="Container" name="jdbc/mysql"
type="javax.sql.DataSource"
username="root"
password="123456"
url="jdbc:mysql://localhost:3306/his?serverTimezone=UTC"
driverClassName="com.mysql.cj.jdbc.Driver"
maxIdle="2"
maxWait="5000"
maxActive="4"
/>
</Context>
<!--
auth:认证方法,一般为Container
type:指明数据源的类型
maxIdle:最大空闲数量
maxWait:最大等待时间
maxActive:最大连接数量
-->
二、在Tomcat的lib文件夹中放入jdbc驱动包
三、使用
public static Connection getcon() {
try {
//上下文
Context context = new InitialContext();
//查找数据源
DataSource dataSource =(DataSource) context.lookup("java:comp/env/jdbc/mysql");
3、从数据源中获取连接
Connection conn= dataSource.getConnection();
return conn;
} catch (NamingException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}