Tomcat 7.0 关于mysql连接池的配置

来源:互联网 发布:中文域名的作用 编辑:程序博客网 时间:2024/06/08 19:56

1, 准备mysql的连接包mysql-connector-java-5.1.6-bin.jar,copy到WEB-INF\lib 或 Tomcat安装目录的lib文件夹

2,编辑 C:\tools\apache-tomcat-7.0.27\conf\context.xml,添加如下内容:

<Resource     name="jdbc/cti"        auth="Container"    driverClassName="com.mysql.jdbc.Driver"    maxActive="100"    maxIdle="40"    maxWait="12000"    username="username"    password="123456"    type="javax.sql.DataSource"     url="jdbc:mysql://localhost:3306/cti?useUnicode=true&characterEncoding=gbk&allow_zero_datetime=true" />

3,编辑C:\workspace\eclipse\cti_test\webroot\WEB-INF,在 <web-app></web-app>标签内,添加以下内容:

 <resource-ref>         <description>DB Connection</description>         <res-ref-name>jdbc/cti</res-ref-name>         <res-type>javax.sql.DataSource</res-type>         <res-auth>Container</res-auth>     </resource-ref>
4,获取Connection的方法

static public java.sql.Connection getConnection_cti() throws java.sql.SQLException, javax.naming.NamingException{    if (ds == null) {        Context ic = new InitialContext();        ds = (DataSource) ic.lookup("java:comp/env/jdbc/cti");    }    Connection con = ds.getConnection();    return con;}




原创粉丝点击