在Tomcat中配配置数据源汇总
生活随笔
收集整理的這篇文章主要介紹了
在Tomcat中配配置数据源汇总
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
為什么80%的碼農(nóng)都做不了架構師?>>> ??
Tomcat本身不具備處理提供數(shù)據(jù)源的能力。借助于一些開源數(shù)據(jù)源實現(xiàn),如:DBCP和C3P0等。一、在http://commons.apache.org/可下載這些是Tomcat提供配置數(shù)據(jù)源所需的類庫。
注意:Tomcat5.5以上標準版本自帶dbcp,放在$Tomcat\common\lib目錄;
下載三個文件后,將三個文件解壓到Tomcat/common/lib,
1.The DBCP Component
commons-dbcp-1.2.1.jar
http://commons.apache.org/dbcp/
2.The Pool Component 1.2.jar
commons-pool-1.2.jar
http://commons.apache.org/pool/
3.Commons Collections 3.1
commons-collections-3.1.jar
http://commons.apache.org/collections/
There are several examples? of using DBCP available.
http://svn.apache.org/viewvc/commons/proper/dbcp/trunk/doc/
4、把數(shù)據(jù)庫JDBC驅(qū)動拷貝到%TOMCAT_HOME%/common/lib和
?????????????????????????? %TOMCAT_HOME%/webapps/yourweb/WEB-INF/lib下(我的web文件夾名字叫quickstart)
二、
? ? 1.? SQL server2000
<Resource
name="jdbc/quickstart"
type="javax.sql.DataSource"
password="123456"
driverClassName="com.microsoft.jdbc.sqlserver.SQLServerDriver"
maxIdle="2"
maxWait="5000"
username="sa"
url="jdbc:microsoft:sqlserver://localhost;DatabaseName=quickstart" maxActive="4"/>
2.MySQL
<Resource????
???????????????? name="jdbc/hnport"
????????????????type="javax.sql.DataSource"
????????????????password="sharker"
????????????????driverClassName="com.mysql.jdbc.Driver"
????????????????maxIdle="2"
????????????????maxWait="5000"
????????????????username="root"
????????????????url="jdbc:mysql://localhost:3306/hnport"
????????????????maxActive="4"/>
三、按數(shù)據(jù)源使用范圍來分數(shù)據(jù)源配置兩個方法:
方法一:所用Web可使用(全局數(shù)據(jù)源)
?
? 1、修改%TOMCAT_HOME%/conf/server.xml文件,在<GlobalNamingResources></GlobalNamingResources>之間加入如下代碼:
< Resource????
???????????????? name ="jdbc/hnport"
???????????????? type ="javax.sql.DataSource"
???????????????? password ="sharker"
???????????????? driverClassName ="com.mysql.jdbc.Driver"
???????????????? maxIdle ="2"
???????????????? maxWait ="5000"
???????????????? username ="root"
???????????????? url ="jdbc:mysql://localhost:3306/hnport"
???????????????? maxActive ="4" />
? 2、修改%TOMCAT_HOME%/webapps/yourweb/WEB-INF下的web.xml文件,在<web-app></web-app>之間添加以下內(nèi)容
< resource-ref >
< description >mysql Connection </ description >
< res-ref-name >jdbc/hnport </ res-ref-name >
< res-type >javax.sql.DataSource </ res-type >
< res-auth >Container </ res-auth >
</ resource-ref >
3、在%TOMCAT_HOME%/conf/Catalina/localhost下新建一個與你web文件夾同名的xml文件(我的是quickstart.xml)
這一步非常重要,如果沒有這步就會出錯,會出現(xiàn)org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC driver of class '' for connect URL 'null'
<? xml version ="1.0" encoding ="UTF-8" ?>
< Context path ="/hnport" reloadable ="true" docBase ="d:\hnport\WebRoot" workDir ="d:\hnport\work" >
???? < Resource name ="jdbc/hnport"
???? type ="javax.sql.DataSource"
???? password ="sharker"
???? driverClassName ="com.mysql.jdbc.Driver"
???? maxIdle ="2"
???? maxWait ="5000"
???? username ="root"
???? url ="jdbc:mysql://localhost:3306/hnport"
???? maxActive ="4" />
</ Context >
方法二:只有某個Web可用(局部數(shù)據(jù)源)
??????? 1.該方法不會造成系統(tǒng)混亂,只需修改%TOMCAT_CAT%\conf\Cattalin\localhost下的Web配置文件。如:hnport.xml
<? xml version ="1.0" encoding ="UTF-8" ?>
< Context path ="/hnport" reloadable ="true" docBase ="d:\hnport\WebRoot" workDir ="d:\hnport\work" >
???? < Resource name ="jdbc/hnport"
???? type ="javax.sql.DataSource"
???? password ="sharker"
???? driverClassName ="com.mysql.jdbc.Driver"
???? maxIdle ="2"
???? maxWait ="5000"
???? username ="root"
???? url ="jdbc:mysql://localhost:3306/hnport"
???? maxActive ="4" />
</ Context >
2 、修改 %TOMCAT_HOME%/webapps/yourweb/WEB-INF 下的 web.xml 文件 , 在 <web-app> </web-app> 之間添加以下內(nèi)容
< resource-ref >
< description >mysql Connection </ description >
< res-ref-name >jdbc/hnport </ res-ref-name >
< res-type >javax.sql.DataSource </ res-type >
< res-auth >Container </ res-auth >
</ resource-ref >
<!-- [if !supportLineBreakNewLine]-->
<!-- [endif]-->
四.測試
<% @page contentType="text/html;charset=big5"%>????
<% @page import="java.sql.*"%>????
<% @page import="javax.sql.*" %>????
<% @page import="javax.naming.*" %>????
<%????
????????try {????
????????????????Context initContext = new InitialContext();????
????????????????Context envContext????=????
????????????????????????(Context)initContext.lookup("java:/comp/env");????
????????????????DataSource ds =????
????????????????????????(DataSource)envContext.lookup("jdbc/hnport");????
????????????????Connection conn = ds.getConnection();????
????
????????????????if(!conn.isClosed())????
????????????????????????
????????????????????????out.println("數(shù)據(jù)庫連接測試成功");????
????????????????conn.close();????
????????}????
????????catch(SQLException e) {????
????????????????out.println(e.toString());????
????????}
%>
?
本文出自 “喬磊的博客 學習 進步” 博客,請務必保留此出處http://sucre.blog.51cto.com/1084905/360048
轉(zhuǎn)載于:https://my.oschina.net/sucre/blog/296253
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎總結
以上是生活随笔為你收集整理的在Tomcat中配配置数据源汇总的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: EasyUI-右键菜单变灰不可用效果
- 下一篇: 【SICP练习】22 练习1.28