Eclipse 下配置MySql5.6的连接池,使用Tomcat7.0

目前找到的最简单的配置方法。

 
1.首先在eclipse中创建一个Dynamical Web Application,在WebContent文件夹下的META-INF文件夹中创建新的名为content.xml的文件,并加入以下代码:
<?xml version="1.0" encoding="UTF-8"?>
<Context>
    <!-- Specify a JDBC datasource -->
    <Resource name="jdbc/库名" auth="Container" type="javax.sql.DataSource"
              maxActive="100" maxIdle="30" maxWait="10000"
              username="javauser" password="javadude" driverClassName="com.mysql.jdbc.Driver"
              url="jdbc:mysql://localhost:3306/库名"/>
 
</Context>

  

2.在WEB-INF/web.xml中加入以下代码中红色部分,其中resource name要与1中对应:

<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
    version="2.4">
  <description>MySQL Test App</description>
  <resource-ref>
      <description>DB Connection</description>
      <res-ref-name>jdbc/数据库名</res-ref-name>
      <res-type>javax.sql.DataSource</res-type>
      <res-auth>Container</res-auth>
  </resource-ref>
</web-app>

  3.在src下创建java代码,获得连接(蓝色为不适用连接池时的数据库连接方式,也不需要1,2两步)

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

public class ConnectionManager
{
    private static DataSource dataSource;
    private static Connection conn;

 
    public static Connection getConnection() throws InstantiationException, IllegalAccessException
    {
        try
        {
           /*System.out.println("Connecting to database");
            String url = "jdbc:mysql://localhost:3306/";
            String dbName ="test"; //  name of Database.
            String uname = "root";
            String pwd = "";
 
            Class.forName("com.mysql.jdbc.Driver");
            */
        	
             Context initContext  = new InitialContext();
             Context envContext  = (Context)initContext.lookup("java:/comp/env");
             dataSource = (DataSource)envContext.lookup("jdbc/库名");
            
            try
            {
               //conn = DriverManager.getConnection(url+dbName,uname,pwd);	
               conn = dataSource.getConnection();
            }
            catch (SQLException ex)
            {
                ex.printStackTrace();
            }
        }
        catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        return conn;
    }
 
}

  

 
 

Eclipse 下配置MySql5.6的连接池,使用Tomcat7.0

2013-06-24 19:00阅读(670)评论(0)
目前找到的最简单的配置方法。
 
1.首先在eclipse中创建一个Dynamical Web Application,在WebContent文件夹下的META-INF文件夹中创建新的名为content.xml的文件,并加入以下代码:
 
<?xml version="1.0" encoding="UTF-8"?>
<Context>
    <!-- Specify a JDBC datasource -->
    <Resource name="jdbc/TestDB" auth="Container" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
username="javauser" password="javadude" driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/test"/>
 
</Context>
 
2.在WEB-INF/web.xml中加入以下代码中红色部分,其中resource name要与1中对应:
   
 
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
    version="2.4">
  <description>MySQL Test App</description>
  <resource-ref>
      <description>DB Connection</description>
      <res-ref-name>jdbc/TestDB</res-ref-name>
      <res-type>javax.sql.DataSource</res-type>
      <res-auth>Container</res-auth>
  </resource-ref>
</web-app>
3.在src下创建java代码,获得连接(蓝色为不适用连接池时的数据库连接方式,也不需要1,2两步)
原文地址:https://www.cnblogs.com/achengmu/p/8067937.html