由于直接编辑server.xml配置连接池很容易出现错误,我估计是tomcat版本问题,没个版本可能配置有点点差别,所以我建议使用在http://localhost/admin/中进行数据源的配置,下载tomcat的admin包,安装过程如下
下载下来进行解压,得到三个文件,和两个包,一个叫conf包,另外一个叫做server包。
接着进行安装:那三个单独的文件不用管;把conf\Catalina\localhost\下面的一个配置文件admin.xml放在你tomcat安装目录下面的Tomcat 5.5\conf\Catalina\localhost\这个路径下,即跟host-manage.xml,manager.xml放在一起;再把server\webapps\下面的admin包拷贝到你的tomcat安装目录下的Tomcat 5.5\server\webapps\这个路径下面跟host-manager,manager包放在一块。
在conf\Catalina\localhost\下要建立一个跟自己项目名字一样的.xml文件,内容为
登录成功后,会出现Tomcat 的web应用管理界面(Tomcat Web Server Administration Tool)。
然后在左边的目录树点击“Data Sources”项,右边可以看到JNDI配置的页面。
在右上角的下拉框中选择“Create New Data Source”,接下来填写配置信息:
JNDI Name: jdbc/feng /*连接池的名称*/
Data Source URL: jdbc:mysql://localhost:3306/bbs /*bbs为数据库的名称*/
JDBC Driver Class: com.mysql.jdbc.Driver
User Name: root
Password: feng
Max. Active Connections: 可用默认值
Max. Idle Connections: 可用默认值
Max. Wait for Connection: 可用默认值
Validation Query: 可不填写。
然后,按下“save”按钮保存。
再按下“Commit Changes”按钮将配置更改提交完成。
下面这步比较重要,不加就会出现错误
在<Context></Context>的描述中加上本web应用对全局连接池的引用说明,如下文字:
类似下面
内容可以参考下面
应用示例
下载下来进行解压,得到三个文件,和两个包,一个叫conf包,另外一个叫做server包。
接着进行安装:那三个单独的文件不用管;把conf\Catalina\localhost\下面的一个配置文件admin.xml放在你tomcat安装目录下面的Tomcat 5.5\conf\Catalina\localhost\这个路径下,即跟host-manage.xml,manager.xml放在一起;再把server\webapps\下面的admin包拷贝到你的tomcat安装目录下的Tomcat 5.5\server\webapps\这个路径下面跟host-manager,manager包放在一块。
在conf\Catalina\localhost\下要建立一个跟自己项目名字一样的.xml文件,内容为
- <?xml version="1.0" encoding="UTF-8"?>
- <Context>
- <Resource
- name="jdbc/oracle"
- type="javax.sql.DataSource"
- driverClassName="oracle.jdbc.driver.OracleDriver"
- password="limeng"
- maxIdle="2"
- maxWait="5000"
- username="limeng"
- url="jdbc:oracle:thin:@192.168.1.134:1521:zhpt"
- maxActive="4"/>
- <ResourceLink
- global="jdbc/feng"
- name="jdbc/feng"
- type="javax.sql.DataSource"/>
- </Context>
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Resource
name="jdbc/oracle"
type="javax.sql.DataSource"
driverClassName="oracle.jdbc.driver.OracleDriver"
password="limeng"
maxIdle="2"
maxWait="5000"
username="limeng"
url="jdbc:oracle:thin:@192.168.1.134:1521:zhpt"
maxActive="4"/>
<ResourceLink
global="jdbc/feng"
name="jdbc/feng"
type="javax.sql.DataSource"/>
</Context>
安装搞定,重新启动tomcat,在地址栏输入http://localhost/admin/,在出现的截面里面输入你安装tomcat的时候你输入的用户名和密码。
登录成功后,会出现Tomcat 的web应用管理界面(Tomcat Web Server Administration Tool)。
然后在左边的目录树点击“Data Sources”项,右边可以看到JNDI配置的页面。
在右上角的下拉框中选择“Create New Data Source”,接下来填写配置信息:
JNDI Name: jdbc/feng /*连接池的名称*/
Data Source URL: jdbc:mysql://localhost:3306/bbs /*bbs为数据库的名称*/
JDBC Driver Class: com.mysql.jdbc.Driver
User Name: root
Password: feng
Max. Active Connections: 可用默认值
Max. Idle Connections: 可用默认值
Max. Wait for Connection: 可用默认值
Validation Query: 可不填写。
然后,按下“save”按钮保存。
再按下“Commit Changes”按钮将配置更改提交完成。
下面这步比较重要,不加就会出现错误
在<Context></Context>的描述中加上本web应用对全局连接池的引用说明,如下文字:
- <ResourceLink global="jdbc/feng" name="jdbc/feng" type="javax.sql.DataSource"/>
<ResourceLink global="jdbc/feng" name="jdbc/feng" type="javax.sql.DataSource"/>
一般都是加在虚拟目录里
类似下面
- <Context
- crossContext="true"
- docBase="D:/feng"
- path="/feng"
- reloadable="true">
- <ResourceLink global="jdbc/feng" name="jdbc/feng" type="javax.sql.DataSource"/>
- </Context>
<Context
crossContext="true"
docBase="D:/feng"
path="/feng"
reloadable="true">
<ResourceLink global="jdbc/feng" name="jdbc/feng" type="javax.sql.DataSource"/>
</Context>
上面完成后,还需要配置对应虚拟目录WEB应用的web.xml
内容可以参考下面
- <web-app>
- <resource-ref>
- <description>MySQL DataSource example</description>
- <res-ref-name>jdbc/feng</res-ref-name>
- <res-type>javax.sql.DataSource</res-type>
- <res-auth>Container</res-auth>
- </resource-ref>
- </web-app>
<web-app>
<resource-ref>
<description>MySQL DataSource example</description>
<res-ref-name>jdbc/feng</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>
保存D:\feng\WEB-INF\web.xml 文件
应用示例
- <%@ page import="java.io.*,java.util.*,java.sql.*,javax.sql.*,javax.naming.*"%>
- <%@ page contentType="text/html; charset=GB2312"%>
- <html>
- <head><title>DataSourse Connection Test</title></head>
- <body>
- <%
- try{
- Connection con;
- Statement stmt;
- ResultSet rs;
- Context ctx = new InitialContext();
- DataSource ds=(DataSource)ctx.lookup("java:comp/env/jdbc/feng");
- con=ds.getConnection();
- stmt=con.createStatement();
- rs=stmt.executeQuery("select * from user");
- while(rs.next()){
- out.println(rs.getString(1));
- %>
- <br>
- <%
- out.print(rs.getString(2));
- }
- rs.close();
- stmt.close();
- con.close();
- }catch(Exception e){
- out.print(e.getMessage());
- }
- %>
- </body>
- </html>
<%@ page import="java.io.*,java.util.*,java.sql.*,javax.sql.*,javax.naming.*"%>
<%@ page contentType="text/html; charset=GB2312"%>
<html>
<head><title>DataSourse Connection Test</title></head>
<body>
<%
try{
Connection con;
Statement stmt;
ResultSet rs;
Context ctx = new InitialContext();
DataSource ds=(DataSource)ctx.lookup("java:comp/env/jdbc/feng");
con=ds.getConnection();
stmt=con.createStatement();
rs=stmt.executeQuery("select * from user");
while(rs.next()){
out.println(rs.getString(1));
%>
<br>
<%
out.print(rs.getString(2));
}
rs.close();
stmt.close();
con.close();
}catch(Exception e){
out.print(e.getMessage());
}
%>
</body>
</html>