300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > Tomcat5.5 配置mysql数据库连接池

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

时间:2021-09-22 20:21:57

相关推荐

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>

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。