tomcat7.0.42如何设置mysql数据库连接池

转载自   tomcat7.0.42如何设置mysql数据库连接池

如何在tomcat7.0.42中设置mysql数据库连接池????eclipse如何绑定tomcat??按网上教程总不成功!怎么办

server.xml

<Resource name="jdbc/test" auth="Container" type="javax.sql.DataSource" factory="org.apache.tomcat.jdbc.pool.DataSourceFactory" testWhileIdle="true" testOnBorrow="true" testOnReturn="false" validationInterval="30000" timeBetweenEvictionRunsMillis="30000" maxActive="100" minIdle="10" maxWait="10000" initialSize="10" removeAbandonedTimeout="60" removeAbandoned="true" logAbandoned="false" minEvictableIdleTimeMillis="30000" jmxEnabled="true" jdbcInterceptors="org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer"  username="root" password="123" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/test"/>

<Host appBase="webapps" autoDeploy="true" name="localhost" unpackWARs="true">
<Context docBase="blog3" path="/blog3" reloadable="true" source="org.eclipse.jst.jee.server:blog3"/>
</Host>

context.xml

<ResourceLink global="jdbc/test" name="jdbc/test" type="javax.sql.DataSource" />
Java代码:

public class DBconn {
	private static String datasoucename = "java:comp/env/jdbc/test";
	Context context = null;
	DataSource ds = null;
	Connection conn = null;

	public Connection getconn() {
		return initConnection();
	}

	public DataSource getDataSource() {
		try {
			context = new InitialContext();
		} catch (NamingException e) {
			System.err.println("连接池上下文不存在! " + e.getMessage());
		}
		try {
			ds = (DataSource) context.lookup(datasoucename);
		} catch (NamingException e) {
			System.err.println("数据源没发现! " + e.getMessage());
		}
		return ds;
	}

	private Connection initConnection() {
		try {
			context = new InitialContext();
		} catch (NamingException e) {
			System.err.println("连接池上下文不存在! " + e.getMessage());
		}
		try {
			ds = (DataSource) context.lookup(datasoucename);
		} catch (NamingException e) {
			System.err.println("数据源没发现! " + e.getMessage());
		}
		try {
			conn = ds.getConnection();
		} catch (SQLException e) {
			System.err.println("获取连接失败! " + e.getMessage());
		}
		return conn;
	}

	public void freeConnection() {
		try {
			conn.close();
		} catch (Exception e) {
			System.err.println("释放连接出错! ");
			e.printStackTrace();
		}
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值