连接池——tomcat自带连接池。

连接池:
tomcat连接池(jndi)
dbcp
c3p0
druid ali

tomcat连接池简单实现:

在tomcat的context.xml配置文件中添加(jnid)Java命名和目录接口:

配置如下

 1 <Resource name="jdbc" 
 2     auth="Container"
 3     type="javax.sql.DataSource" 
 4     maxActive="100" //一个数据库在此服务器上所能打开的最大连接数
 5     maxIdle="30"    //一个数据库在此服务器上维持的最小连接数
 6     maxWait="10000" //最大等待时间。10000毫秒
 7     username="root" 
 8     password=""
 9     driverClassName="com.mysql.jdbc.Driver"
10     url="jdbc:mysql://localhost/mydata?characterEncoding=UTF-8" />

在配置项目的web.xml

1 <resource-ref>
2         <res-ref-name>jdbc</res-ref-name>
3         <res-type>javax.sql.DataSource</res-type>
4         <res-auth>Container</res-auth>
5     </resource-ref>

现在可以再类中去获取连接了

1 //javax.naming.Context提供了查找JNDI 的接口
2 Context ctx = new InitialContext();
3 //java:comp/env/为前缀
4 DataSource dataSource = (DataSource) ctx.lookup("java:comp/env/jdbc");
5 Connection conn = dataSource.getConnection();
6 PreparedStatement ps = conn.prepareStatement("insert into t_users (name,jineng) values('华安','9527')");
7 ps.execute();
8 ps.close();
9 conn.close();
原文地址:https://www.cnblogs.com/lingdu9527/p/11019432.html