連接池——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();


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM