DBMNG数据库管理与应用

才能是来自独创性。独创性是思维、观察、理解和判断的一种独特的方式。
当前位置:首页 > MySQL > 驱动连接

Tomcat下配置MySQL局部数据库连接池的META-INF方式

只需在tomcat\webapps\mypreject\META-INF\context.xml的Context中增加:
<Context
 path="/mywebapp"

docBase="mywebapp"
debug="5"

reloadable="true"

crossContext="true" >
<Resource name="jdbc/mysqlds"
 auth="Container"
 type="javax.sql.DataSource"
 driverClassName="com.mysql.jdbc.Driver"
 url="jdbc:mysql://localhost:3306/spms?autoReconnect=true"
 username="root"
 password="test"
 maxActive="100"
 maxIdle="30"
 maxWait="10000"
/>
</Context>

参数说明:
name:

driveClassName:JDBC驱动类的完整的名称;
maxActive:同时能够从连接池中被分配的可用实例的最大数;
maxIdle:可以同时闲置在连接池中的连接的最大数;
maxWait:最大超时时间,以毫秒计;
password:用户密码;
url:到JDBC的URL连接;
user:用户名称;

 

测试:

//连接池的获取
Connection conn = null;
DataSource ds = null;
ResultSet rs =null;
Statement stmt = null;
Context initCtx = new InitialContext();
ds =(DataSource)initCtx.lookup("java:comp/env/jdbc/mysqlds");
if(ds!=null){
conn = ds.getConnection();
try{
stmt = conn.createStatement();
String sql ="select * from tree_table";
rs = stmt.executeQuery(sql);
while(rs.next()){

}
} catch(Exception ex){
ex.printStackTrace();
}finally{
conn.close();
rs.close();
stmt.close();
}
}

本站文章内容,部分来自于互联网,若侵犯了您的权益,请致邮件chuanghui423#sohu.com(请将#换为@)联系,我们会尽快核实后删除。
Copyright © 2006-2023 DBMNG.COM All Rights Reserved. Powered by DEVSOARTECH            豫ICP备11002312号-2

豫公网安备 41010502002439号