一、在項目中的配置:
.在WebRoot/META-INF下建context.xml文件,注意必須在該目錄下:
<?xml version="1.0" encoding="UTF-8"?>
<Context reloadable="true" crossContext="true">
<Resource
name="jdbc/項目名" ---------------必須為項目名,否則會有異常
auth="Container"---------------------該項為不變項
type="javax.sql.DataSource"---------該項為不變項
driverClassName="com.mysql.jdbc.Driver"------------數據庫驅動名
url="jdbc:mysql://localhost:3306/haotian?autoReconnect=true"---url
username="root"---------------------------------------用戶名
password="root"----------------------------------------密碼
maxActive="10" ----------------------------最大連接數
maxIdle="5" -------------------------------最大空閑連接數
maxWait="-1"/> ------------------最大等待毫秒數,-1為無限等待
</Context>
在Tomcat6.0以后就不需要在web.xml中配置了:
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/blog</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
二、配置過程中遇到的異常及解決方法:
(1)org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC driver of class '' for connect URL 'null'
解決方法:連接池的名字是name="jdbc/項目名"
(2)org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot load JDBC driver class 'com.mysql.jdbc.Driver'
解決方法:必須把mysql的驅動包,拷貝到tomcat/lib目錄下
(3)javax.naming.NameNotFoundException: Name jdbc is not bound in this Context
這個異常可能是使用tomcat6版本以下,需要在項目的web.xml配置:
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/blog</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
以上是個人在開發過程中遇到的異常,在參考網友提供解決及個人實踐總結所得。