今天做java项目和sqlserver的jndi连接,一直出现“Network error IOException: Connection refused: connect” ,经过试验才知道是我的sqlserver2000没有打sp4的补丁,参照:http://www.cnblogs.com/alamps/archive/2009/04/18/1438751.html
以下转自:http://hi.baidu.com/imake/blog/item/6ebef0de211ccf5095ee37e3.html 个人感觉tomcat6的数据源配置比tomcat5简单了很多。 这里我用mysql安装好后的空数据库test来做演示: 1.use test; 2.create table testdata ( id int not null auto_increment primary key, foo varchar(25), bar int); 3.insert into testdata values(null, 'hello', 12345); 4.上面3步是准备演示用数据库中数据的。 接下来配置context.xml文件。在这里:%TOMCAT6%\conf\context.xml 在文件里的<Context></Context>元素中加入如下内容: <Resource name="jdbc/TestDB" auth="Container" type="javax.sql.DataSource" maxActive="100" maxIdle="30" maxWait="10000" username="root" password="1234" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/test?autoReconnect=true"/> 表红的是用户名和密码。大家用的时候可能要改下。 5.然后打开%TOMCAT6%\webapps\ROOT\WEB-INF文件, 在<web-app></web-app>这对元素中加入如下内容: <resource-ref> <description>DB Connection</description> <res-ref-name>jdbc/TestDB</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref> 好了,这样就为ROOT项目配置好了数据源。大家为自己项目配置数据源时,就改相应项目下的WEB.XML文件就行了。 6.下面需要把MYSQL的JDBC包放到%TOMCAT6%\lib下。 7.开始测试:在%TOMCAT6%\webapps\ROOT下建立一个叫DS.jsp的文件,在里面输入如下内容: <%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <sql:query var="rs" dataSource="jdbc/TestDB"> select id, foo, bar from testdata </sql:query> <html> <head> <title>DB Test</title> </head> <body> <h2>Results</h2> <c:forEach var="row" items="${rs.rows}"> Foo ${row.foo}<br/> Bar ${row.bar}<br/> </c:forEach> </body> </html> 为了测试需要,大家需要从网上下载JSTL包。把下载下来的包放到%TOMCAT6%\lib。 主要是这两个文件:jstl.jar、standard.jar 在这里下载:http://jakarta.apache.org/site/downloads/downloads_taglibs-standard.cgi 当然也可用下面的文件来测试: <%@ page language="java" import="java.util.*" pageEncoding="GB2312"%> <%@ page import="java.sql.*"%> <%@ page import="javax.sql.DataSource" %> <%@ page import="javax.naming.*"%> <html> <head> <title>测试</title> </head> <body> <% out.print("测试MySQL连接池<br/>"); try{ Context initContext = new InitialContext(); Context envContext = (Context)initContext.lookup("java:/comp/env"); DataSource ds = (DataSource)envContext.lookup("jdbc/TestDB"); Connection conn = ds.getConnection(); out.print("MySQL connection pool runs perfectly!"); conn.close(); } catch(Exception ex){ out.println("连接错误:"); out.print(ex.getMessage()); } %> </body> </html> 好了,结束了。下面再附上配置oracle数据源的方法: 1.在Context.xml中加入: <Resource name="jdbc/myoracle" auth="Container" type="javax.sql.DataSource" driverClassName="oracle.jdbc.OracleDriver" url="jdbc:oracle:thin:@127.0.0.1:1521:mysid" username="scott" password="tiger" maxActive="20" maxIdle="10" maxWait="-1"/> 2.配置web.xml文件: <resource-ref> <description>Oracle Datasource example</description> <res-ref-name>jdbc/myoracle</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref> |
配置Tomcat 6数据源
最新推荐文章于 2016-10-18 13:15:54 发布