为什么80%的码农都做不了架构师?>>>
Tomcat本身不具备处理提供数据源的能力。借助于一些开源数据源实现,如:DBCP和C3P0等。
一、在http://commons.apache.org/可下载这些是Tomcat提供配置数据源所需的类库。
注意: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、把数据库JDBC驱动拷贝到%TOMCAT_HOME%/common/lib和
%TOMCAT_HOME%/webapps/yourweb/WEB-INF/lib下(我的web文件夹名字叫quickstart)
二、
1. SQL server2000
一、在http://commons.apache.org/可下载这些是Tomcat提供配置数据源所需的类库。
注意: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、把数据库JDBC驱动拷贝到%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"/>
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"/>
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"/>
三、按数据源使用范围来分数据源配置两个方法:
方法一:所用Web可使用(全局数据源)
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" />
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>之间添加以下内容
< 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 >
< 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)
这一步非常重要,如果没有这步就会出错,会出现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 >
< 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可用(局部数据源)
1.该方法不会造成系统混乱,只需修改%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 >
< 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> 之间添加以下内容
< 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]-->
< 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("数据库连接测试成功");
conn.close();
}
catch(SQLException e) {
out.println(e.toString());
}
%>
<% @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("数据库连接测试成功");
conn.close();
}
catch(SQLException e) {
out.println(e.toString());
}
%>
本文出自 “乔磊的博客 学习 进步” 博客,请务必保留此出处http://sucre.blog.51cto.com/1084905/360048