wojkxlq 发表于 2016-10-20 09:15:36

Tomcat5.5 配置mysql数据库连接池

环境:Tomcat5.5.23 Eclipse3.2.2 MyEclipse 5.1.1 GA mysql4.0.16 一、在Server.xml中配置 <Resource name="jdbc/myown" auth="Container" type="javax.sql.DataSource" maxActive="10" maxIdle="3" maxWait="10000" username="root" password="" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/test" />放到 </GlobalNamingResources>中。 二、F:\soft\apache-tomcat-5.5.23\conf\Catalina\localhost目录中新建一个工程名.xml文件,F:\soft\apache-tomcat-5.5.23为tomcat安装目录。在工程名.xml中写下面的内容:<Context> <Resource name="jdbc/myown" auth="Container" type="javax.sql.DataSource" maxActive="10" maxIdle="3" maxWait="10000" username="root" password="" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/test" /> </Context> 三、添加数据库驱动jar文件到F:\soft\apache-tomcat-5.5.23\common\lib与工程web目录\lib下. 四、写jsp页面测试:<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>
<%@ page import="javax.sql.*"%>
<%@ page import="javax.naming.*"%>
<html>
<body>
<%
DataSource ds = null;
Connection conn = null;
ResultSet rs = null;
Statement stmt = null;
InitialContext ctx = null;
try {
ctx = new InitialContext();
ds = (DataSource) ctx.lookup("java:comp/env/jdbc/myown");
conn = ds.getConnection();
stmt = conn.createStatement();
rs = stmt.executeQuery("select * from users");
while (rs.next()) {
out.println(rs.getString(1) + "");
out.println(rs.getString(2) + "");
}
} catch (Exception ex) {
ex.printStackTrace();
} finally {
try {
if (rs != null)
rs.close();
if (stmt != null)
stmt.close();
if (conn != null)
conn.close();
if (ctx != null)
ctx.close();
} catch (SQLException e) {
System.out.println("SQL关闭异常");
e.printStackTrace();
} catch (NamingException e) {
System.out.println("命名空间关闭异常");
e.printStackTrace();
}
}
%>
</body>
</html>
页: [1]
查看完整版本: Tomcat5.5 配置mysql数据库连接池