更新Tomcat5.5+MySql数据库连接池配置方法

网上写了很多tomcat+mysql数据库连接池的配置方法,但在新的tomcat5.5中很多已不在适用.下面的方法是最简单方便的最新配置方法.

1> 下载驱动JDBC驱动http://dev.mysql.com/downloads/connector/j/5.0.html , 解压缩得到jar文件,如mysql-connector-java-5.0.4-bin.jar,复制到%tomcat%\common\lib下.

2>建立虚拟目录并配置连接池.

在%tomcat%\conf\Catalina\localhost\目录下建立一个与工程同名的xml文件.这里我的工程叫udateTesting,所以我建立叫udateTesting.xml文件,打开文件添加如下内容.

<context path="/udateTesting" docBase="D:\Tomcat 5.5\webapps\udateTesting" reloadable="true" crossContext="true" debug="0" >

<resource name="jdbc/mytest" auth="Container" type="javax.sql.DataSource" maxActive="100" maxIdle="30" maxWait="10000" username="liuwen" password="liuwen" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://127.0.0.1/test?useUnicode=true&amp;characterEncoding=GBK"/>

</context>

上面,您需要更改path为”/<工程名>“,docBase为”<该工程路径>“,username为数据库用户名,password为数据库密码,url为”jdbc:mysql://<数据库服务器ip>/<数据库名>?useUnicode=true&characterEncoding=GBK”.

配置完毕.

3>测试

在工程路径下新建一个jsp文件为test.jsp.

<%@page contentType="text/html;charset=gbk"%>
<%@page import="java.sql.*"%>
<%@page import="javax.sql.DataSource"%>
<%@page import="javax.naming.*"%>

<html>
<body>
<%try{ Context initCtx=new InitialContext();
 DataSource db = (DataSource)initCtx.lookup("java:comp/env/jdbc/mytest");
 Connection conn = db.getConnection();
 Statement stmt = conn.createStatement();
 ResultSet rs = stmt.executeQuery("select * FROM admin");
 out.println("User-list"+"<br />");
 while(rs.next()){
out.print(rs.getString(1)+" ");
out.print(rs.getString(2)+"<br />"); }
rs.close();
stmt.close();
conn.close();
}catch(Exception e){
out.print(e);}%>
</body>
</html>

注意更改为您自己的sql代码.

随机日志

发表评论

0 评论.

Leave a Reply



[ Ctrl + Enter ]

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word

CNXCT小组的博客 is Stephen Fry proof thanks to caching by WP Super Cache